【问题标题】:Finding author of a message查找消息的作者
【发布时间】:2018-03-18 13:43:09
【问题描述】:

如果有人写“?name (arg)”,我希望我的机器人说出消息的作者+“,你的名字是”+ arg。 我找不到该消息的作者

@client.command()
async def name(their_name):
    await client.say("{0}, your name is {1}.".format("""author of the message goes here""", their_name))

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    要获取消息作者的姓名,您可以使用:

    message.author.id
    

    【讨论】:

    • 我试过这段代码,它说没有定义消息,所以我在代码顶部定义了消息,现在它说 str 没有属性作者。
    【解决方案2】:

    要获取消息作者的姓名,您需要使用context

    @client.command(pass_context=True)
    async def name(ctx):
        username = ctx.message.author.name
        ...
    

    【讨论】:

    • 等等 ctx.author.name 不也有效吗?我尝试使用我的机器人,它返回了正确的值。
    • 两者都有效。我只是使用此表单,因为我确定messagectx 的一部分,因为我个人在我的机器人中使用股票discord.py,而不是ext.commands,它提供了command() 装饰器和默认的ctx 对象。
    【解决方案3】:

    您也可以使用User.display_name,它会尝试使用用户服务器特定的昵称,但如果找不到,则会回退到通用用户名:

    @client.command(pass_context=True)
    async def name(ctx):
        username = ctx.message.author.display_name
    

    【讨论】:

      【解决方案4】:

      您也可以使用.mention,因此如果您将其发送到频道,它将标记作者

      @client.command(pass_context=True)
      async def name(ctx):
          username = ctx.message.author.mention
      

      【讨论】:

        猜你喜欢
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        相关资源
        最近更新 更多