获取 Discord.py 中的消息作者

Get message author in Discord.py

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

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

或者您可以:

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

如果要 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')