在 Discord.py 中获取消息作者

Ech*_*pha 5 python python-3.x 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')
Run Code Online (Sandbox Code Playgroud)

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

Sea*_*dge 5

以下适用于discord.pyv1.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')
Run Code Online (Sandbox Code Playgroud)