角色在响应问题上添加两次

时间:2019-05-01 11:23:13

标签: python-3.x discord.py discord.py-rewrite

我当前正在创建一个public AdditionCalc() : base(() => new Version(ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString)) { } 事件,如果用户向邮件添加响应,它将给他们一个角色。我遇到一个小问题:当用户添加表情符号on_raw_reaction_add时,它会添加两个角色,而不是特定反应的角色。

无论具体的表情符号ID如何,这种情况都会发生。

帮助表示赞赏。

这是我的代码:

if payload.emoji.id !=

1 个答案:

答案 0 :(得分:0)

这是一种更通用的方法。我们维护反应名称到角色名称的映射,然后只要有人做出反应,我们就会在字典中查找他们的反应并获得相关的角色:

emoji_role_map = {
    "1\N{COMBINING ENCLOSING KEYCAP}": "LoL",  # This is the default :one:
    "my_custom_emoji": "WoW"
}

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload): 
    botroom = self.bot.get_channel(572943295039406101)
    if not payload.guild_id:
        # In this case, the reaction was added in a DM channel with the bot
        return 
    if payload.message_id != 573104280299372556: # ID of the message you want reactions added to.
        return
    guild = self.bot.get_guild(payload.guild_id)  # You need the guild to get the member who reacted
    member = guild.get_member(payload.user_id)  # Now you have the key part, the member who should receive the role
    role_name = emoji_role_map.get(payload.emoji.name)
    if role_name:  # None if not found
        role = discord.utils.get(guild.roles, name=role)
        await member.add_roles(role, reason='Reaction role') 

这有两个优点:您可以从任何地方加载该地图,因此可以将角色授予逻辑与实际的表情符号和角色分离,现在可以在多个服务器上进行设置,并且只要名称相同,现在您可以使用默认表情符号来获得角色,而您无法使用系统。

相关问题