Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mention a user in discord.py?

I'm trying to code a simple bot using discord.py, so I started with the fun commands to get familiar with the library.

import discord

client = discord.Client()

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!hug'):
        await message.channel.send(f"hugs {message.author.mention}")

    if message.content.startswith('!best'):
        user_id = "201909896357216256"
        user_mention = ???  # How to convert user_id into a mention
        await message.channel.send(f'{user_mention} is the best')
like image 213
Itachi Sama Avatar asked May 14 '17 13:05

Itachi Sama


People also ask

How do I mention a user in Discord Python?

Discord uses a special syntax to embed mentions in a message. For user mentions, it is the user's ID with <@ at the start and > at the end, like this: <@86890631690977280> . If they have a nickname, there will also be a ! after the @ . Role mentions and channel mentions work similarly.

How do you mention a username on Discord?

Instead, tapping the user's icon brings up their profile (previously tap and hold), and tapping their username will mention them in chat.

How do you get a bot to mention a role?

From any text channel, simply type @(The name of the role) , select it, and hit enter. You can also mention roles by ID. This is useful for programmers who write bots for Discord, or create Webhooks, because if the name of the role changes, the mention still works.


1 Answers

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)
like image 71
Itachi Sama Avatar answered Oct 30 '22 20:10

Itachi Sama