【问题标题】:Get message author in Discord.py在 Discord.py 中获取消息作者
【发布时间】:2020-06-19 00:59:34
【问题描述】:

我正在尝试为我和我的朋友制作一个有趣的机器人。我想要一个命令来说明作者的用户名是什么,带或不带标签。我尝试查找如何执行此操作,但没有一个适用于我的代码当前设置的方式。

import discord
client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('$WhoAmI'):
        ##gets author.
        await message.channel.send('You are', username)

client.run('token')

我希望这是有道理的,我看到的所有代码都使用ctx@client.command

【问题讨论】:

  • 你试过discord.Message.author.name吗?
  • 另一个问题是它只是与程序的常规流程内联,你的if应该在on_message@client.event中,见here
  • @SeanBreckenridge 我编辑并修复了它。我的脚本是正确的,我只是在发布我的问题时忘记包含它,感谢您告诉我!
  • @Daniil 这不是消息的实例,这只是消息对象。
  • @Daniil 我出错了,Class 'Message' has no 'author' member

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

以下适用于discord.py v1.3.3

message.channel.send 不像print,它不接受多个参数并从中创建一个字符串。使用str.format 创建一个字符串并将其发送回频道。

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('$WhoAmI'):
        await message.channel.send('You are {}'.format(message.author.name))

client.run('token')

【讨论】:

    【解决方案2】:

    或者你可以:

    import discord
    from discord import commands
    
    client = commands.Bot(case_insensitive=True, command_prefix='$')
    
    @client.command()
    async def whoAmI(ctx):
       await ctx.send(f'You are {ctx.message.author}')
    

    【讨论】:

      【解决方案3】:

      如果你想 ping 用户:

      import discord
      client = discord.Client()
      
      @client.event
      async def on_message(message):
          if message.author == client.user:
              return
          if message.content.startswith('$WhoAmI'):
              await message.channel.send('You are', message.author.mention)
      
      client.run('token')
      

      【讨论】:

        猜你喜欢
        • 2020-08-22
        • 1970-01-01
        • 2016-12-10
        • 2019-01-12
        • 2019-04-21
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        • 2021-01-28
        相关资源
        最近更新 更多