Discord.py Mention User By Name
I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name='mention') @commands.has_role(OwnerCommands) async def mention(ctx, *, member: disc
Solution 1:
It is rather straight forward member.mention
@bot.command(name='mention')@commands.has_role(OwnerCommands)asyncdefmention(ctx, *, member: discord.Member):
await ctx.message.delete()
await ctx.send(member.mention)
Solution 2:
We can skip the manual work and use discord's Member Model directly. The mention
on the Member object returns us the string to allow us to mention them directly.
@bot.command(name='mention')
@commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
await ctx.message.delete()
await ctx.send(member.mention)
Solution 3:
Solution 4:
Another solution if you don't want to mention the member twice by using discord.py MemberConverter extension, And also don't forget to activate the members privileged indents from the discord developer portal.
resources: https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MemberConverterHow to convert a string to a user discord.py
import discord
from discord.ext import commands
from discord.ext.commands import MemberConverter
intents = discord.Intents(members = True)
client = commands.Bot(command_prefix="$", intents=intents)
@client.command()asyncdefmention(ctx, member):
converter = MemberConverter()
member = await converter.convert(ctx, (member))
await ctx.send(member.mention)
Post a Comment for "Discord.py Mention User By Name"