【问题标题】:Discord Bot how get get senderDiscord Bot 如何获取发件人
【发布时间】:2018-05-10 14:42:33
【问题描述】:

我正在用 Python 制作一个不和谐的机器人,我想要一个命令,例如有人打字

User: !hello
Bot: Hello User#1234

我在网上找遍了,都没有找到。我已经尝试了很多东西,但都没有奏效

【问题讨论】:

  • 请展示您尝试过的内容。如果我们不知道这个“一切”是什么,告诉我们您已经尝试了所有方法并没有什么帮助。
  • 你在 python 中使用了什么 discord api?

标签: python bots discord sender


【解决方案1】:

如果您使用discord.py 库,则消息有一个作者变量,您可以使用message.author 访问该变量,这将返回UserMember 对象。 您可以使用message.author.name(这将返回用户)获取这些的显示名称,而message.author 将返回用户#1234 discord.py 库也有一个很好的文档here

【讨论】:

    【解决方案2】:

    如果您使用的是discord.py,请查看以下示例脚本: https://github.com/Rapptz/discord.py/blob/async/examples/reply.py

    @client.event
    async def on_message(message):
        if message.content.startswith('!hello'):
            msg = 'Hello {0.author.mention}'.format(message)
            await client.send_message(message.channel, msg)
    

    如果您想添加更多命令,最好使用@*.command() 装饰器,请参阅the other examples。基本上,上面示例的装饰器版本应该如下所示:

    bot = commands.Bot(command_prefix='!')
    
    @bot.command(pass_context=True)
    async def hello(ctx):
        """Answers a !hello command"""
        msg = 'Hello {0.author.mention}'.format(ctx.message)
        await bot.say(msg)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2020-08-17
      • 2020-10-04
      相关资源
      最近更新 更多