简体   繁体   English

如何制作自定义不和谐机器人@某人,某人在命令中@ed?

[英]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.但是,如果您想以不同的方式处理它,您可以添加更多ifelse语句。

message.author.mention always mentions the author of the message message.author.mention总是提到消息的作者

you can solve this in multiple ways你可以通过多种方式解决这个问题

  1. Just send whatever comes behind !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)
  1. same as number 1, but add some check if the thing behind !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])
  1. Use Commands - I really recommend this one使用命令- 我真的推荐这个
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.

相关问题 如何做到这一点,以便我的 discord 机器人只接受启动命令的人的输入 - How do I make it so my discord bot only take inputs from the person that started the command 如何使不和谐的机器人根据人员在命令中输入的内容来创建角色 - How can I make a discord bot create a role based on what the person types in the command 我将如何做到这一点,以便个人是唯一可以使用 discord 机器人运行命令的人? - How would I make it so that an individual person is the only one who can run a command with a discord bot? 如何让不和谐的机器人在命令中提及我和我提及的人 - How to get discord bot to mention me and the person i mention in the command 如何为 Heroku 上的 Discord 机器人执行 Python 重新加载命令? - How do I make a Python reload command for the Discord bot on Heroku? 如何让我的 Discord.py 机器人计算一个人的消息? - How do I make, so my Discord.py bot counts a person's messages? 我如何让我的 discord 机器人将某人添加到 json 文件中(如果他们还没有的话) - how do i make my discord bot add someone to the json file if they are not already there 当有人进入我的 discord 服务器时,如何让机器人说些什么 - How do I make a bot say something when someone enter my discord server 如何让 discord 机器人给某人一个角色 - How to make a discord bot give a role to someone 我如何做出 discord 机器人回答? - How do i make a discord bot answer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM