【问题标题】:How to mention a sender of a command? discord.py如何提及命令的发送者?不和谐.py
【发布时间】:2019-01-29 10:19:35
【问题描述】:

我创建了一个超级简单的.report <person> 命令。我有它,所以当有人键入它时它将被发送到某个频道。我想做的是让它显示报告其他用户的用户的名称。我不知道该怎么做。有谁知道最好的方法吗?

@bot.command()
async def report(*, message):
    await bot.delete(message)
    await bot.send_message(bot.get_channel("479177111030988810"), message)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以使用ctx.author.mention 提及用户。 mention 属性(记录在here)返回一个字符串,您可以在send 的参数中使用该字符串。

    示例用法:

    @bot.command("hello", help="Learn about this bot")
    async def hello(ctx: discord.commands.Context):
        await ctx.send(f"Hi {ctx.author.mention}! I'm a bot!")
    

    【讨论】:

      【解决方案2】:

      您可以执行类似的操作,获取上下文 (ctx) 并从中获取消息的内容及其作者

      @bot.command(pass_context=True)
      async def report(ctx):
          await bot.delete_message(ctx.message)
          report = f"\"{ctx.message.content[8:]}\"  sent by {ctx.message.author}"
          await bot.send_message(bot.get_channel("479177111030988810"), report)
      

      【讨论】:

      • 这实际上是@提及用户吗?当我使用 {ctx.message.author} 时,它只将用户名打印为普通文本字符串(例如:BanjoFox#2731)
      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多