問題描述
如何在對不同消息做出反應時讓不和諧機器人刪除以前的角色? (How to make discord bot remove the previous role when reacting to a different message?)
我已經讓我的不和諧機器人在反應和不反應方面給予和刪除角色。我該如何做到這一點,以便當用戶對錶情符號做出反應而之前已經對另一個表情做出反應時,前一個會被刪除?這樣角色就不會疊加...?
這是我添加角色的代碼:
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
message_id = payload.message_id
if message_id == 810784018953666580:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, self.client.guilds)
if payload.emoji.name == 'grey_B19FF9':
role = guild.get_role(810471074500182036)
elif payload.emoji.name == 'skyblue_11A7BB':
role = guild.get_role(810471062449291296)
else:
role = discord.utils.get(guild.roles, name=payload.emoji.name)
if role is not None:
member = payload.member
if member is not None:
await member.add_roles(role)
print("Done.")
else:
print("Member not found.")
else:
print("Role not found")
這是用於刪除角色的:
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
message_id = payload.message_id
if message_id == 810784018953666580:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, self.client.guilds)
if payload.emoji.name == 'grey_B19FF9':
role = guild.get_role(810471074500182036)
elif payload.emoji.name == 'skyblue_11A7BB':
role = guild.get_role(810471062449291296)
else:
role = discord.utils.get(guild.roles, name=payload.emoji.name)
if role is not None:
guild = await self.client.fetch_guild(payload.guild_id)
member = await guild.fetch_member(payload.user_id)
if member is not None:
await member.remove_roles(role)
print("Done.")
else:
print("Member not found.")
else:
print("Role not found")
任何幫助將不勝感激!
參考解法
方法 1:
What happens when this code runs ? I think the method remove_roles
takes a list of roles so you should turn that role
argument into a list before removing, same goes for add_roles
You should also look into member.roles to check the roles of a member, depending on what is inside that list, you should have enough information to be able to remove the role that you don't want
(by CrashtestEnigma、Nathan Marotte)