How do I mention a user in discord.py?
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(message.channel, ' : %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
Discord.py 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
await message.channel.send(
f"sending <@{user_id}> to court!"
)
mention user from string(name#discriminator) discord.py
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")
ALSO:
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 discord.py
Another way:
sido = await bot.fetch_user(439800066312503297)
await ctx.send(sido.mention)
You need intents for this:
https://discordpy.readthedocs.io/en/stable/intents.html
discord.py Mention user by name
Nevermind, I found a way. I simply turned on Privileged indents in the Discord Developers page.
Discord.py - 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!
@bot.command()
async def tell(ctx, *, tell):
x = re.search('<@(.*)>', tell)
# User mentions are usually in the form of
# <@USERID> or <@!USERID>
if not x:
await ctx.send(tell)
return
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)
@bot.command()
async def tell(ctx, *, text):
if ctx.message.mentions:
await ctx.send("Mentioning other's members not allowed")
return
await ctx.send(text)
[Image for Method 1]
[Image for Method 2]
[Image for how Bots see mentions (id blocked for privacy reasons)]
Other links:
- Find string between two substrings - SO
- Python Regex - W3Schools
- Discord.py Documentation
Related Topics
How to Restart a Program Based on User Input
How to Call a Django Function on Button Click
Discord Bot Messaging a User With a Specific User Id
How to Delete the Words Between Two Delimiters
How to Increase Dpi With Opencv
Python Pandas Dataframe Get All Combinations of Column Values
How to Find Rows of One Dataframe in Another Dataframe
Finding Out Who Got the Highest Mark Among the Students
How to Convert Datetime by Removing Nanoseconds
Django: Check Whether an Object Already Exists Before Adding
How to Concatenate Multiple Column Values into a Single Column in Pandas Dataframe
Importing Modules from Parent Folder