不和谐的机器人不会分配角色

时间:2020-09-19 09:38:00

标签: python discord bots discord.py

我正在遵循Anson开发人员指南(here's a link),并且完全按照他的编码进行编码,除了警告Parameter 'payload' value is not used 之外,我没有其他错误,并且我已经确保:

  1. 机器人具有适当的权限来分配角色
  2. 该机器人在线
  3. 表情符号和角色名称相同

此处是供参考的代码:

import discord
client = discord.Client()


@client.event
async def on_message(message):
    if message.author == client.user:
        return


@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id

    if message_id == '756794977148993597':
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)

        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("done")
            else:
                print("member not found")
        else:
            print("role not found")
 
client.run('REDACTED')
 

我已经尝试了很多故障排除方法,将不胜感激!

1 个答案:

答案 0 :(得分:1)

在您的情况下,请勿使用utils.find

@client.event
async def on_raw_reaction_add(payload):
    if payload.message_id == 756794977148993597:
        guild = client.get_guild(payload.guild_id)
        role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            member = guild.get_member(payload.user_id)
            if member is not None:
                await member.add_roles(role)
                print("done")
            else:
                print("member not found")
        else:
            print("role not found")