python-3.x - AttributeError:'NoneType' 对象没有属性 'id' - on_raw_reaction_add 中的错误
问题描述
所以我最近在我的机器人中遇到了这个我以前没有遇到过的问题......
错误 -
Traceback (most recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/container/cogs/mod.py", line 100, in on_raw_reaction_add
await payload.member.add_roles(role, reason="Reaction Roles", atomic=True)
File "/home/container/.local/lib/python3.9/site-packages/discord/member.py", line 764, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
我的代码是:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.member.bot:
pass
else:
with open('reactrole.json') as react_file:
data = json.load(react_file)
for x in data:
if x['emoji'] == payload.emoji.name:
role = discord.utils.get(self.client.get_guild(
payload.guild_id).roles, id=x['role_id'])
await payload.member.add_roles(role)
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
with open('reactrole.json') as react_file:
data = json.load(react_file)
for x in data:
if x['emoji'] == payload.emoji.name:
role = discord.utils.get(self.client.get_guild(
payload.guild_id).roles, id=x['role_id'])
await self.client.get_guild(payload.guild_id).get_member(payload.user_id).remove_roles(role)
我刚刚检查了文档并添加了这些额外的参数,例如原因和原子。
如果您知道解决此问题的方法,请回答。提前致谢。
解决方案
discord.utils.get
不知何故找不到您获得的角色,这导致discord.utils.get
返回 None 对象。因此,问题在于reactrole.json
文件中的数据。
如果没有其他信息(如 reactrole.json 文件和服务器上的角色),这一切都可以得到帮助。
推荐阅读
- javascript - 我有 2 个 mongoose 模式从中获取数据,然后将其作为响应发送,但始终在数据解析之前发送响应
- encryption - 如何使用 Google Cloud Build 和 Google Cloud KMS 从构建时解密的文件中读取变量
- java - 如何在 Eclipse 中按类别设置 JUnit 运行配置?
- ios - 选择选项卡时如何刷新或重新加载 WKwebview
- python - 从用户输入应用比较符号
- c# - 相同代码的等待和不同的线程ID
- excel - 聚合通配符搜索
- java - 如何使用数组中的坐标在谷歌地图上绘制多边形
- javascript - Ajax - 我提交后无法显示帖子
- button - (如何)我可以更改 F# Fable Elmish 中禁用按钮的字体颜色吗?