Carlos Mediavilla
Carlos Mediavilla

Reputation: 304

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

I'm doing an easy moderation bot, which one of the command is used to op someone (elevate their role to moderator) using Discord.py.

This is the command in question, which is inside a cog (commands being 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}") 

The bot itself loads perfectly. WHen trying to run !mod @username#1234 this is displayed on the terminal, due to the exception catching I set on the command

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

Upvotes: 1

Views: 1893

Answers (2)

The Rainbow
The Rainbow

Reputation: 98

You basically need something which gives you the instance of discord.Member and discord.Role, so you have to do member for the discord.Member instance because you already have it in the argument converter and ctx.guild.get_role(392763334052544522) so it would be await member.add_roles(ctx.guild.get_role(392763334052544522), atomic=True).

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60954

You need to get the role object representing the role and pass that instead of the id.

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

Upvotes: 1

Related Questions