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?
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)
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)
@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
!
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.
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?
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)
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)
@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
!
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.