How to Mention a User in Discord.Py

How do I mention a user in

So I finally figured out how to do this after few days of trial and error hoping others would benefit from this and have less pain than I actually had..
The solution was ultimately easy..

  if message.content.startswith('!best'):
myid = '<@201909896357216256>'
await client.send_message(, ' : %s is the best ' % myid)

Discordpy mention user from string

Using discord.utils.get you can get a member object from their name and # and mention them afterwards using member.mention:

#members is your list of members (format is name#1111)
for str_member in members:
member = discord.utils.get(message.guild.members, name=str_member[:-5], discriminator=str_member[-4:])
#using member.mention you can mention the person now, or save the mention in a list or whatever you want to do how to mention someone that's mentioned in the command

You are sending a user object. To send a mention you have to do "<@{userid}" to mention

Here is what you need to do

f"sending <@{user_id}> to court!"

mention user from string(name#discriminator)

If you have both the username and discriminator, the best way to get that member would be like this:

member = message.guild.get_member_named("me#0000")

Make sure you have enabled intents in your bot (discord.Intents.all()) and in your application in the Discord Dev Portal

mention a member with

Another way:

sido = await bot.fetch_user(439800066312503297)
await ctx.send(sido.mention)

You need intents for this: Mention user by name

Nevermind, I found a way. I simply turned on Privileged indents in the Discord Developers page. - Handling user to mentioning other users

There are a number of ways to tackle this, and here I will show you two possible methods that may interest you. The first method is, as Anu said, to use Regex. You can check if anything starts with <@ and ends with >, as this is how most user and role mentions are laid out, and this is how the bot sees mentions. The second method is not as good as using regex, where you check if the ctx.message has any mentions in it via message.mentions. Do view both of these methods and some further explanation below.

# Method 1: Recommended and Regex
# Checks if 'tell' has mentions via regex
# I will link other questions on regex below to help you
import re # Don't forget to import this!

async def tell(ctx, *, tell):
x ='<@(.*)>', tell)
# User mentions are usually in the form of
# <@USERID> or <@!USERID>
if not x:
await ctx.send(tell)
await ctx.send("Don't mention other users")

# Method 2: Not recommended but working
# Check if the original command message has mentions in it via message.mentions
# Will backfire if the user uses a mention prefix (@Bot tell)
async def tell(ctx, *, text):
if ctx.message.mentions:
await ctx.send("Mentioning other's members not allowed")
await ctx.send(text)

[Image for Method 1]

Working Method 1

[Image for Method 2]

Working Method 2

[Image for how Bots see mentions (id blocked for privacy reasons)]

How bots see mentions

Other links:

  • Find string between two substrings - SO
  • Python Regex - W3Schools
  • Documentation

Related Topics

Leave a reply