在 Discord.py 中获取消息作者

新手上路,请多包涵

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

 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

原文由 EchoTheAlpha 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 932
2 个回答

以下适用于 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')

原文由 Sean Breckenridge 发布,翻译遵循 CC BY-SA 4.0 许可协议

或者你可以:

 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}')

原文由 Mbuxiq 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题