How do I mention a user using user's id 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)
If you're working on commands, you're best to use discord.py's built in command functions, your hug command will become:
import discord
from discord.ext import commands
@commands.command(pass_context=True)
async def hug(self, ctx):
await self.bot.say("hugs {}".format(ctx.message.author.mention()))
This is assuming you've done something like this at the start of your code:
def __init__(self):
self.bot = discord.Client(#blah)
From a User
object, use the attribute User.mention
to get a string that represents a mention for the user. To get a user object from their ID, you need Client.get_user_info(id)
. To get the a user from a username ('ZERO') and discriminator ('#6885') use the utility function discord.utils.get(iterable, **attrs)
. In context:
if message.content.startswith('!best'):
user = discord.utils.get(message.server.members, name = 'ZERO', discriminator = 6885)
# user = client.get_user_info(id) is used to get User from ID, but OP doesn't need that
await client.send_message(message.channel, user.mention + ' mentioned')