clan1500
clan1500

Reputation: 11

How to convert User object to Discord Member object when having the ID

We're creating a command that prints out the NICKNAME of when pinging someone. I've looked through the discord.py documentations and tried it myself but I did not manage to get it to work.

Code:

@client.command(aliases = ['xp','exp','showexp'])
async def showxp(ctx,user=None):
  
    if user == None:
      name = ctx.message.author.nick

else:
      
    
      user = user.replace("<","")
      user = user.replace(">","")
      user = user.replace("@","")
      user = user.replace("!","")
      user_ID = int(user)

Upvotes: 1

Views: 97

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

That's a really bad code, you should regex for that, but alternatively you can use MemberConverter, works by simply typehinting the user argument

async def showxp(ctx, user: discord.Member=None):
    if user is None:
        user = ctx.author # So it's never a NoneType

    print(type(user)) # <class 'discord.member.Member'> | exactly what you wanted

Upvotes: 1

Related Questions