反应角色不协调

时间:2020-07-02 12:22:35

标签: python discord.py

@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    if message_id == 728213540858757152:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "loud_sound":
            role = discord.utils.get(message_id.guild.roles, name="DUYURU")
        elif payload.emoji.name == "bee":
            role = discord.utils.get(guild.roles, name="REKLAM")
        else:
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            if member is not None:
                await member.add_roles(role)
                print(f"{member} {role} aldı!")
            else:
                print("Member not found")
        else:
            print("Role not found")

@client.event
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id
    if message_id == 728213540858757152:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "loud_sound":
            role = discord.utils.get(guild.roles, name="DUYURU")
        elif payload.emoji.name == "bee":
            role = discord.utils.get(guild.roles, name="REKLAM")
        else:
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            if member is not None:
                await member.remove_roles(role)
                print(f"{member} {role} aldı!")
            else:
                print("Member not found")
        else:
            print("Role not found")

我试图在我的discord.py机器人中放置响应角色,但是它不起作用。它给了我错误:Role not found。但是我已经在角色列表中了。我尝试使用我制作的另一个与角色相关的代码,该代码可以很好地工作,但也没有用。我曾尝试将message_id更改为channel,但这也不起作用

1 个答案:

答案 0 :(得分:0)

根据docs进行了一些更改 on_reaction_add有两个参数reaction, user,这些参数会将您的事件监听器更改为async def on_reaction_add(reaction, user)

第二次获取对象的ID是通过执行object.id完成的,因此您可以执行guild = reaction.message.guild.id

第三,您可以使用bot.get_guild(id)来获取行会,但是您不必这样做,因为您可以从reaction.message.guild那里获得。