discord.py - payload.member returns none on on_raw_reaction_remove() but works with on_raw_reaction_add()
问题描述
so im making a discord bot and I have an error:
Ignoring exception in on_raw_reaction_remove Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "main.py", line 41, in on_raw_reaction_remove await payload.member.remove_roles(role) AttributeError: 'NoneType' object has no attribute 'remove_roles'
on_raw_reaction_remove() code:
@client.event
async def on_raw_reaction_remove(payload):
if payload.message_id == 865402961585111111:
if str(payload.emoji) == "✅":
guild = client.get_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name='Announcements')
await payload.member.remove_roles(role)
on_raw_reaction_add() code:
@client.event
async def on_raw_reaction_add(payload):
if payload.message_id == 865402961585111111:
if str(payload.emoji) == "✅":
guild = client.get_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name='Announcements')
await payload.member.add_roles(role)
The strange thing is, with on_raw_reaction_add() it returns a valid user.
解决方案
I changed your event a bit and added some checks to make sure everything is correct.
There were always problems with finding the role, so you need to do things a little differently.
Take a look at the following code:
@client.event
async def on_raw_reaction_remove(payload):
guild = client.get_guild(payload.guild_id)
print("Guild checked.")
member = discord.utils.get(guild.members, id=payload.user_id)
print("Member checked.")
if payload.message_id == 865402961585111111:
print("Got message.")
if str(payload.emoji) == "✅":
print("Checked for the reaction.")
role = discord.utils.get(guild.roles, name='Announcements')
print("Got the role.")
else:
role = discord.utils.get(guild.roles, name=payload.emoji)
if role is not None:
await member.remove_roles(role)
print("Removed the role")
If the code is not self-explanatory, I'll be happy to add some explanations.
EDIT: After looking at the full code the problem seems to be with the Intents. You named them correctly but did not import them.
To import them you use:
client = commands.Bot(command_prefix="YourPrefix", intents=intents1)
推荐阅读
- javascript - 我的组件数据表有问题
- typescript - Firebase admin.database().ref().set() 在 Firebase 函数中失败
- android - 如何抽象化我的 GSON 模型?**ClassCastException**
- python - 附加不存在的行数据时,来自 pd.Crosstab 的 Multiindex 问题
- reactjs - React Big Calendar:通过状态错误呈现事件
- flutter - 使用 HLS 流时,flutter video_player 是否会自动使用自适应比特率?
- mysql - MySQL - 查找查询的最佳索引
- java - 仅设置 Android 主屏幕壁纸
- amazon-web-services - AWS SCP 标签执行器失败
- django - Django 中的 UpdateView 不是从数据库的 datetimefield 加载时间,它是脆的形式吗?