'Int' 对象没有属性 'id' Discord.py 命令错误

'Int' object has no atribute 'id' error on Discord.py command

我正在做一个简单的 moderation 机器人,其中一个命令用于使用 Discord.py 操作某人(将他们的角色提升为 moderator)。

这是有问题的命令,它在一个齿轮内(命令是 discord.ext.commands):

commands.command(name='mod', hidden = True)
#just the mods, the bot role and the server creator can use this command, hence why the decorator below:
@commands.has_any_role("role1","role2", "role3")
async def mod(self, ctx, member:discord.Member = None):
    try:
        if member == None:
            await ctx.send('no argument given')      
        elif member == ctx.message.author:
            await ctx.send('You already are moderator')
        else:
            await discord.Member.add_roles(392763334052544522, atomic=True)
    except Exception as e:
        await ctx.send(f"There was an error while trying to elevate {member}. Exception: {e}") 
        print("\n" + f"There was an error while {ctx.message.author} tried to elevate {member}. Exception: {e}") 

机器人本身加载完美。当尝试 运行 !mod @username#1234 这会显示在终端上,因为我在命令

上设置了异常捕获

There was an error while (Mydiscorduser) tried to elevate (anotherdiscorduser). Exception: 'int' object has no attribute 'id'

您需要获取表示角色的 role 对象并传递该对象而不是 id。

role = ctx.guild.get_role(392763334052544522)
await member.add_roles(role, atomic=True)

你基本上需要一些东西来给你 discord.Memberdiscord.Role 的实例,所以你必须为 discord.Member 实例做 member 因为你已经有了它在参数转换器和 ctx.guild.get_role(392763334052544522) 中,所以它将是 await member.add_roles(ctx.guild.get_role(392763334052544522), atomic=True).