与 nextcord 的反应作用
Reaction Role with nextcord
在我的 Nextcord 机器人上,我想添加一个反应角色系统。我试过这段代码:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
message_id = payload.message_id
if message_id == 930812411097796618:
guild_id = payload.guild_id
guild = nextcord.utils.find(lambda g: g.id == guild_id, self.bot.guilds)
if payload.emoji.name == "tada":
role = nextcord.utils.get(guild.roles, id=896417683153752104)
elif payload.emoji.name == "nut_and_bolt":
role = nextcord.utils.get(guild.roles, id=896417735473528832)
elif payload.emoji.name == "microphone":
role = nextcord.utils.get(guild.roles, id=896417789458403368)
elif payload.emoji.name == "part_alternation_mark":
role = nextcord.utils.get(guild.roles, id=896417886724313128)
elif payload.emoji.name == "desktop":
role = nextcord.utils.get(guild.roles, id=896397761770053682)
elif payload.emoji.name == "screwdriver":
role = nextcord.utils.get(guild.roles, id=896397839784083496)
elif payload.emoji.name == "wrench":
role = nextcord.utils.get(guild.roles, id=896398445487726613)
elif payload.emoji.name == "elephant":
role = nextcord.utils.get(guild.roles, id=922184344238444596)
elif payload.emoji.name == "gem":
role = nextcord.utils.get(guild.roles, id=896398489376944138)
elif payload.emoji.name == "mobile_phone":
role = nextcord.utils.get(guild.roles, id=896398561179226133)
elif payload.emoji.name == "bird":
role = nextcord.utils.get(guild.roles, id=896398605873729547)
elif payload.emoji.name == "floppy_disk":
role = nextcord.utils.get(guild.roles, id=896398741605584936)
elif payload.emoji.name == "hash":
role = nextcord.utils.get(guild.roles, id=896398802204913664)
elif payload.emoji.name == "snake":
role = nextcord.utils.get(guild.roles, id=896398888817283113)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
if role is not None:
member = nextcord.utils.find(lambda m: m.id == payload.user_id, guild.members)
if member is not None:
await member.add_roles(role)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="None")
await channel.send(embed=embed)
我遵循了 this YouTube 教程(我确实将 discord
更改为 nextcord
),但它似乎不起作用。这是执行代码后的控制台:
我不知道这是什么意思,所以我无法解决我的问题。
请注意,当 payload.emoji.name 不等于列出的任何一个时,此代码为 运行:
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
然后您尝试 运行 以下代码,它引用名称“角色”:
if role is not None:
但是,如果 payload.emoji.name 不等于列出的任何一个,则角色不会被绑定,并且您会因尝试使用不存在的变量而出错。
解决方案
你可以通过在上面的块中添加一个return语句来解决这个问题,就像这样:
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
return
如果在尝试使用角色之前没有设置角色,这将停止该功能,因此它应该可以解决错误。
您还需要编辑表情符号的名称,否则 payload.emoji.name 将无法匹配任何表情符号,因为它们使用表情符号的 unicode 版本,而不是表情符号的 discord 名称。
例如,不要使用“nut_and_bolt”,而应使用“”。
payload.emoji.name
顺便说一句,您可以使用
改进您的代码
role = guild.get_role(role_id=your_role_id)
而不是
role = nextcord.utils.get(guild.roles, id=your_role_id)
同样,您可以使用
member = guild.get_member(user_id=payload.user_id)
而不是
member = nextcord.utils.find(lambda m: m.id == payload.user_id, guild.members)
最后,
guild = self.bot.get_guild(id=payload.guild_id)
而不是
guild = nextcord.utils.find(lambda g: g.id == guild_id, self.bot.guilds)
在我的 Nextcord 机器人上,我想添加一个反应角色系统。我试过这段代码:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
message_id = payload.message_id
if message_id == 930812411097796618:
guild_id = payload.guild_id
guild = nextcord.utils.find(lambda g: g.id == guild_id, self.bot.guilds)
if payload.emoji.name == "tada":
role = nextcord.utils.get(guild.roles, id=896417683153752104)
elif payload.emoji.name == "nut_and_bolt":
role = nextcord.utils.get(guild.roles, id=896417735473528832)
elif payload.emoji.name == "microphone":
role = nextcord.utils.get(guild.roles, id=896417789458403368)
elif payload.emoji.name == "part_alternation_mark":
role = nextcord.utils.get(guild.roles, id=896417886724313128)
elif payload.emoji.name == "desktop":
role = nextcord.utils.get(guild.roles, id=896397761770053682)
elif payload.emoji.name == "screwdriver":
role = nextcord.utils.get(guild.roles, id=896397839784083496)
elif payload.emoji.name == "wrench":
role = nextcord.utils.get(guild.roles, id=896398445487726613)
elif payload.emoji.name == "elephant":
role = nextcord.utils.get(guild.roles, id=922184344238444596)
elif payload.emoji.name == "gem":
role = nextcord.utils.get(guild.roles, id=896398489376944138)
elif payload.emoji.name == "mobile_phone":
role = nextcord.utils.get(guild.roles, id=896398561179226133)
elif payload.emoji.name == "bird":
role = nextcord.utils.get(guild.roles, id=896398605873729547)
elif payload.emoji.name == "floppy_disk":
role = nextcord.utils.get(guild.roles, id=896398741605584936)
elif payload.emoji.name == "hash":
role = nextcord.utils.get(guild.roles, id=896398802204913664)
elif payload.emoji.name == "snake":
role = nextcord.utils.get(guild.roles, id=896398888817283113)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
if role is not None:
member = nextcord.utils.find(lambda m: m.id == payload.user_id, guild.members)
if member is not None:
await member.add_roles(role)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="None")
await channel.send(embed=embed)
我遵循了 this YouTube 教程(我确实将 discord
更改为 nextcord
),但它似乎不起作用。这是执行代码后的控制台:
我不知道这是什么意思,所以我无法解决我的问题。
请注意,当 payload.emoji.name 不等于列出的任何一个时,此代码为 运行:
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
然后您尝试 运行 以下代码,它引用名称“角色”:
if role is not None:
但是,如果 payload.emoji.name 不等于列出的任何一个,则角色不会被绑定,并且您会因尝试使用不存在的变量而出错。
解决方案
你可以通过在上面的块中添加一个return语句来解决这个问题,就像这样:
else:
channel = self.bot.get_channel(930598926174781460)
embed = nextcord.Embed(title="An error occured")
await channel.send(embed=embed)
return
如果在尝试使用角色之前没有设置角色,这将停止该功能,因此它应该可以解决错误。
您还需要编辑表情符号的名称,否则 payload.emoji.name 将无法匹配任何表情符号,因为它们使用表情符号的 unicode 版本,而不是表情符号的 discord 名称。
例如,不要使用“nut_and_bolt”,而应使用“”。 payload.emoji.name
顺便说一句,您可以使用
改进您的代码role = guild.get_role(role_id=your_role_id)
而不是
role = nextcord.utils.get(guild.roles, id=your_role_id)
同样,您可以使用
member = guild.get_member(user_id=payload.user_id)
而不是
member = nextcord.utils.find(lambda m: m.id == payload.user_id, guild.members)
最后,
guild = self.bot.get_guild(id=payload.guild_id)
而不是
guild = nextcord.utils.find(lambda g: g.id == guild_id, self.bot.guilds)