mo9mo9study / discord.CodeWarehouse

3 stars 2 forks source link

on_raw_reaction_addのエラー #126

Closed supleiades closed 3 years ago

supleiades commented 3 years ago

Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/rolesmanager.py", line 37, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot' Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, *kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/view_TimesChannel.py", line 33, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot' Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(args, kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/times.py", line 92, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot' Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/rolesmanager.py", line 37, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot' Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, *kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/view_TimesChannel.py", line 33, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot' Ignoring exception in on_raw_reaction_add Traceback (most recent call last): File "/home/centos/repos/discord.CodeWarehouse/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(args, kwargs) File "/home/centos/repos/discord.CodeWarehouse/Cogs/Managements/times.py", line 92, in on_raw_reaction_add if payload.member.bot: AttributeError: 'NoneType' object has no attribute 'bot'

supleiades commented 3 years ago

await payload.member.add_roles(role) でいけるのですが、消すほうは、

            member = guild.get_member(payload.user_id)
            await member.remove_roles(role)

という感じでいったんmemberを取得する必要があります。

リアクションが付けられた場合はpayloadの中にmemberが入ってくるのですが、リアクションが消されたときはmemberが存在しないためです。

supleiades commented 3 years ago

memberオブジェクトが取得できていないため、botという属性が存在しないと言われている

supleiades commented 3 years ago

image

payloadの中身(add)

<RawReactionActionEvent message_id=889294741265600543 user_id=603567991132782592 channel_id=838702281481781309 guild_id=603582455756095488 emoji=<PartialEmoji animated=True name='ninStar' id=533582793100230656> event_type='REACTION_ADD' member=<Member id=603567991132782592 name='SuPleiades' discriminator='7704' bot=False nick=None guild=<Guild id=603582455756095488 name='もくもくOnline勉強会' shard_id=None chunked=True member_count=335>>>

payloadの中身(remove)

<RawReactionActionEvent message_id=889294741265600543 user_id=603567991132782592 channel_id=838702281481781309 guild_id=603582455756095488 emoji=<PartialEmoji animated=False name='ninStar' id=533582793100230656> event_type='REACTION_REMOVE' member=None>
supleiades commented 3 years ago

dmにメッセージを送信する処理(自己紹介)でこのエラーが発生しているようだ on_raw_reaction_addはギルド内もBOTのDMにも反応するが、今回エラーを起こしたコードはdmのオブジェクトに対応していないためエラーが発生していた

対応策

・メッセージのタイプがDMならreturnする処理を一番最初に追加する ・対象コードを確認しつつ、dmのメッセージに反応する必要がないかを確認した上でdmに反応しないように修正する

supleiades commented 3 years ago

ちなみに、dmのpayloadの中身

[DEBUG] (payload) <RawReactionActionEvent message_id=889316416149401680 user_id=618077392686153742 channel_id=754845237020590090 guild_id=None emoji=<PartialEmoji animated=False name='👍' id=None> event_type='REACTION_ADD' member=None>
supleiades commented 3 years ago

class tmp_reaction(commands.Cog): def init(self, bot): self.bot = bot

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
    # =======================================================================
    debug_guild = self.bot.get_guild(payload.guild_id)
    print(f"[DEBUG] (payload) {payload}")
    # =======================================================================
    if payload.member.bot:
        print("[INFO] reaction from bot")
        return
    print(f"{payload.member}: {payload.member.bot}")

@commands.Cog.listener()
async def on_ready(self):
    guild_id = 603582455756095488
    member_id = 603567991132782592
    member = self.bot.get_guild(guild_id).get_member(member_id)
    dm = await member.create_dm()
    embed = discord.Embed(title="題目", description="説明")
    send_embed= await dm.send(embed=embed)
    await send_embed.add_reaction("👍")

def setup(bot): return bot.add_cog(tmp_reaction(bot))

supleiades commented 3 years ago

追加するコード(+デバッグ付き)

supleiades commented 3 years ago
supleiades commented 3 years ago

payload.memberにせず

member = await self.bot.fetch_user(payload.user_id)
member.bot

にするでよいかも。DMのチャンネルかどうかの判定よりここではbotかどうかだけの判定にしぼろう

supleiades commented 3 years ago

起動時の出力を確認してエラーが発生しないことを確認