Skip to content
Advertisement

How to tag a user using the discord.Member argument?

I have a discord bot with the command !sad. On execution the bot should reply with a random string from sad_list.

sad_list = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

@client.command()
async def sad(ctx, user: discord.Member=None):
    print("running 'sad'")
    
    if user is None:
        await ctx.send(f"{sad_list[randint(0, len(sad_list))]} <3")

I want the option to have the bot tag someone if you execute the command with the user: discord.Member argument.

Preferred output:

@user#0000 [positive message] <3

as opposed to only

[Positive message] <3

The following has already been tried:

if user is None:
        await ctx.send(f"{ctx.message.author} {sad_list[randint(0, len(sad_list))]} <3")
    else:
        await ctx.send(f"{user} {sad_list[randint(0, len(sad_list))]} <3")

However this simply writes:

user#0000 [positive comment] <3

As opposed to actually mentioning them

Advertisement

Answer

Use user.mention to mention users

await ctx.send(f"{user.mention} {sad_list[randint(0, len(sad_list))]} <3")

https://discordpy.readthedocs.io/en/master/api.html?highlight=user%20mention#discord.User.mention

For future references, you could also mention users like this: <@!user_id>, replace user_id with the actual ID of the user. You can use this in cases where user.mention is not practical.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement