Closed supleiades closed 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が存在しないためです。
memberオブジェクトが取得できていないため、botという属性が存在しないと言われている
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>
dmにメッセージを送信する処理(自己紹介)でこのエラーが発生しているようだ on_raw_reaction_addはギルド内もBOTのDMにも反応するが、今回エラーを起こしたコードはdmのオブジェクトに対応していないためエラーが発生していた
・メッセージのタイプがDMならreturnする処理を一番最初に追加する ・対象コードを確認しつつ、dmのメッセージに反応する必要がないかを確認した上でdmに反応しないように修正する
ちなみに、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>
再現したエラーと起動からの標準出力
--------------------
起動中...
BOT NAME : プールの監視員
BOT ID : 618077392686153742
--------------------
[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>
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "/home/centos/.anyenv/envs/pyenv/versions/3.8.6/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/centos/staging/discord.CodeWarehouse/tmp-on_reaction.py", line 19, in on_raw_reaction_add
if payload.member.bot:
AttributeError: 'NoneType' object has no attribute 'bot'
再現したコード
from discord.ext import commands
import discord
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))
追加するコード(+デバッグ付き)
event_channel = self.bot.get_channel(payload.channel_id)
reaction_member = await self.bot.fetch_user(payload.user_id)
if isinstance(event_channel, discord.DMChannel):
print(f"[DEBUG] {self.__class__.__name__} : (reaction: {reaction_member.name}/{payload.emoji.name}) BOT間のDMメッセージのためreturnします") # noqa : E501
return
view_TimesChannel
times
rolesmanager
payload.memberにせず
member = await self.bot.fetch_user(payload.user_id)
member.bot
にするでよいかも。DMのチャンネルかどうかの判定よりここでは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' 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'