Divide05
Divide05

Reputation: 27

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

Upvotes: 0

Views: 242

Answers (2)

Mr squirrel 51
Mr squirrel 51

Reputation: 21

if you want to mention the user you just type

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

Upvotes: 2

moinierer3000
moinierer3000

Reputation: 1979

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.

Upvotes: 2

Related Questions