所以,一切基本上都在标题里说了。我有这个代码:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
channel = await self.bot.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
user = message.guild.get_member(payload.user_id)
guild = self.bot.get_guild(payload.guild_id)
emoji = payload.emoji
if message.id == 849352038801211403:
role = discord.utils.get(guild.roles, name = "[Role]")
if emoji.id == 849345681277452328: await user.add_roles(user, Apex)
else:
await channel.send("Incorrect emoji")
await message.remove_reaction(emoji, user)
它说这个错误:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\mbuxiq\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\mbuxiq\Desktop\Code\Bot\Cogs\serverRelated.py", line 66, in on_raw_reaction_add
if emoji.id == 849345681277452328: await user.add_roles(user, Apex)
File "C:\Users\mbuxiq\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 676, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\mbuxiq\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 243, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
我不知道该怎么办。请帮我。谢谢!
答案 0 :(得分:2)
您的错误消息指出 Unknown Role
@ await user.add_roles(user, Apex)
!
首先,您提供的代码中没有声明 Apex
。确保它是你打电话的时候。但由于它没有抛出 NameError
我猜是。
其次,user
是 discord.Member
类型,因为您将其声明为
user = message.guild.get_member(payload.user_id)
但是 await discord.Member.add_roles()
只接受 discord.Role
作为参数不是成员!这就是你的错误的来源。您试图将成员分配为角色,这完全没有意义。只需从您的 user
函数调用中删除 add_roles
即可解决您的问题
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
channel = await self.bot.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
user = message.guild.get_member(payload.user_id)
guild = self.bot.get_guild(payload.guild_id)
emoji = payload.emoji
if message.id == 849352038801211403:
role = discord.utils.get(guild.roles, name = "[Role]")
if emoji.id == 849345681277452328:
await user.add_roles(Apex)
else:
await channel.send("Incorrect emoji")
await message.remove_reaction(emoji, user)