xXSkillexZ
xXSkillexZ

Reputation: 105

Discord.py | Undefined variable 'user' Error

@commands.command()
    async def test(self, ctx, user_id : int):
        test= discord.Embed(title=f'A moderation action has been performed!', description='', color=0x90fd05)
        test.add_field(name='User Affected:', value={user.name}, inline=True)
        test.add_field(name='User ID:', value=f'`{user_id}`', inline=True)
        test.add_field(name='Moderator Name:', value=f'`{ctx.author}`', inline=True)
        test.add_field(name='Moderator ID:', value=f'`{ctx.author.id}`', inline=True)
        test.add_field(name='Action Performed:', value='`UnBan`', inline=True)\
        #test.timestamp = datetime.datetime.utcnow()
        test.set_author(name=f'{ctx.guild}', icon_url=ctx.guild.icon_url)
        test.set_thumbnail(url=user.avatar_url)
        await ctx.channel.send(embed=test)

I keep getting this error for the lines:

test.add_field(name='User ID:', value=f'`{user_id}`', inline=True)

and

test.set_thumbnail(url=user.avatar_url)

why is that? Should I need to add anything in the second line? I searched on google and found out the use of it is like I did it but it does not work...

Upvotes: 1

Views: 440

Answers (2)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

user_id is not the same as user, you also didn't define user, if you want to get a discord.Member instance you can either use the MemberConverter or Guild.get_member

Using MemberConverter

async def test(self, ctx, user: discord.Member): # This will work with mentions, names, ID's, nicknames..
    print(type(user)) # <class 'discord.member.Member`>
    # ...

Using Guild.get_member

async def test(self, ctx, user_id: int):
    user = ctx.guild.get_member(user_id)
    print(type(user)) # <class 'discord.member.Member`>
    # ...

Reference:

Upvotes: 1

Nurqm
Nurqm

Reputation: 4743

You haven't defined the user variable. In order to define it, you can get the discord.Member object by using guild.get_member(id). Then you can use user variable.

@commands.command()
async def test(self, ctx, user_id : int):
    user = ctx.guild.get_member(user_id)
    test= discord.Embed(title=f'A moderation action has been performed!', description='', color=0x90fd05)
    ...

Upvotes: 2

Related Questions