Getting the userid of a user that was tagged

Question:

I am using discordpy to build a discord bot. One of the features that I want it to have, is to be able to give a random percentage about a user.

Example:

user1: !w @user2

bot: @user2 is x% y

I am unsure how to draw the userid of user2 from the message sent by user1. Does anyone have any suggestions?

Asked By: achechkovsky

||

Answers:

You’ve asked a question in the title and the description doesn’t match it ;-;

Here’s a basic code according to what you’ve asked in the description, customize it as you like 🙂

Make sure to replace ‘client’ with whatever you’ve used

@client.command()
async def cool(ctx, user: discord.Member = None):
    pctg = (random.randint(0, 100))
    if user == None:
        await ctx.send('Mention a user')
    else:
        await ctx.send(f"{user.name} is {pctg}% cool")

If you want the mentioned user’s ID as bot’s reply then use the following –

@client.command()
async def uid(ctx, user: discord.Member):
    uid = user.id
    await ctx.send(uid)

enter image description here

enter image description here

Answered By: Astrogeek

You can use the id attribute of discord.Member.

Example:

@bot.command()
def w(ctx, user: discord.Member):
    user_id = user.id
    print(user_id)

Answered By: Billy
@client.event
async def on_message(message):
    if message.content.startswith('w!cool '):
        if message.mentions == []:
            await message.channel.send("You should mention someone!n```nw!cool <mention>n```")
            return
        coolness_tosend=""
        for i in message.mentions:
            coolness_tosend=coolness_tosend+"<@" + str(i.id) + "> is " + str(random.choice(range(100))) + "% cool.n"
        await message.channel.send(coolness_tosend)

Links:

discord.Message.mentions in API reference

discord.Message in API reference

Do note, though, that w!cool alone without arguments won’t work. It prevents conflict with commands such as w!coolpic. Don’t forget to do an import random!

Answered By: Tigran's Tips
WCOOL_NO_MENTION_ERROR = "You should mention someone!n" 
                       + "```n" 
                       + "w!cool <mention> ...n" 
                       + "```"

@client.event
async def on_message(message):
    if message.content.startswith('w!cool '):
        if not message.mentions:
            return await message.channel.send(WCOOL_NO_MENTION_ERROR)
        
        # Create a string for the response
        response = str()
        
        for mention in message.mentions:
            percentage = random.choice(range(0, 100+1))
            response += f"<@{mention.id}> is {percentage}% cool."
            response += "n"
        
        await message.channel.send(response)
    elif message.content == "w!cool":
        return await message.channel.send(WCOOL_NO_MENTION_ERROR)

Based on my older answer from my other account but it’s a little bit more readable. It works pretty much the same way with some things improved (such as caring about the situation when nothing comes after w!cool)

Also if anyone is reading this, don’t use discord.py! It is unmaintained, so use a fork of it, such as Pycord.

Answered By: anonguy8
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.