[英]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当我输入 !best 时,它会出现我的用户名,如果我@其他人使用相同的命令,例如 !best @example 并出现 @nottheexample
if message.content.startswith('!best'):
await message.channel.send(message.author.mention)
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
.要提及用户,您可以使用f
-strings 或format
。
Based on the code above here is an example:基于上面的代码,这里是一个例子:
@client.event # Or whatever you use
async def on_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
.请注意,该代码仅在您还指定了user
时才有效。 However, you can add more if
or else
statements if you want to handle it differently.但是,如果您想以不同的方式处理它,您可以添加更多if
或else
语句。
message.author.mention
always mentions the author of the message message.author.mention
总是提到消息的作者
you can solve this in multiple ways你可以通过多种方式解决这个问题
!best
只需发送后面的任何内容!best
if message.content.startswith('!best'):
args = message.content.split('!best ')
if len(args) > 1:
await message.channel.send(args[1])
else:
await message.channel.send(message.author.mention)
!best
is a real member - see Utility Functions
in the docs与数字 1 相同,但添加一些检查!best
背后的东西是否是真正的成员 - 请参阅文档中的Utility Functions
member = discord.utils.find(lambda m: m.name == args[1], message.guild.members)
member = discord.utils.get(message.guild.members, name=agrs[1])
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def best(ctx, member: discord.Member = None):
if member is None:
member = ctx.author
await ctx.send(member.mention)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.