【问题标题】:Pythion Discord Bot @'ing peoplePython Discord Bot @'in people
【发布时间】:2026-02-20 11:20:05
【问题描述】:

当我运行/insult @name 之类的命令时,我需要机器人@ 是命令参数中的人并发送图像。剩下的大部分我都可以做,但我似乎想不通@mention这个人。

【问题讨论】:

标签: python discord discord.py bots


【解决方案1】:

要在命令中提及用户,您可以使用member: discord.Member。这有助于您在命令本身中获取成员对象。您可以在此处查看有关如何使用 discord.Member 对象的更多信息。有关如何在命令中使用它的示例也可以在文档中找到,查看:Discord Converters

您可以在下面查看如何合并这些,包括默认的 None 变量以避免在 ctx.author 未提及成员时控制台中出现错误。

@client.command() # or bot.command(), or whatever you're using
async def insult(ctx, member:discord.Member=None):
    if member == None: # Happens if ctx.author does not mention a member..
        member = ctx.author # ..so by default the member will be ctx.author
    # You can use member.mention to mention/ ping/ @ the person assigned as member
    await ctx.send(f"Be insulted {member.mention}!")
    # A not as good way to do it would be:
    await ctx.send(f"Be insulted <@{member.id}>!")
    # both methods work the same way, but member.mention is recommended

【讨论】:

    最近更新 更多