How Do I Make A Custom Discord Bot @ Someone That A Person @ed In The Command?
When I type !best it comes up with my username which also does it if I @ someone else with the same command like !best @example and comes up with @nottheexample if message.content.
Solution 1:
To mention a user you have to define it beforehand. You do this as follows:
user = message.mentions[0]
To mention the user you can either use f
-strings or format
.
Based on the code above here is an example:
@client.event # Or whatever you useasyncdefon_message(message):
user = message.mentions[0]
if message.content.startswith('!best'):
await message.channel.send("Hello, {}".format(user.mention))
Please note that the code only works if you then also specify a user
. However, you can add more if
or else
statements if you want to handle it differently.
Solution 2:
message.author.mention
always mentions the author of the message
you can solve this in multiple ways
- Just send whatever comes behind
!best
if message.content.startswith('!best'):
args = message.content.split('!best ')
iflen(args) > 1:
await message.channel.send(args[1])
else:
await message.channel.send(message.author.mention)
- same as number 1, but add some check if the thing behind
!best
is a real member - seeUtility Functions
in the docs
member = discord.utils.find(lambda m: m.name == args[1], message.guild.members)
member = discord.utils.get(message.guild.members, name=agrs[1])
- Use Commands - I really recommend this one
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()asyncdefbest(ctx, member: discord.Member = None):
if member isNone:
member = ctx.author
await ctx.send(member.mention)
Post a Comment for "How Do I Make A Custom Discord Bot @ Someone That A Person @ed In The Command?"