Discord bot 使用用户名响应用户

时间:2021-05-09 04:07:54

标签: python discord discord.py

我的机器人有代码可以让你向它问好。目前,它看起来像这样

if message.content.startswith('$hello'):
   await message.channel.send('Hello (message.author.name)!')

当你输入消息 "$hello" 很明显,它会以 "Hello (message.author.name)!" 来响应 我如何让机器人实际上用用户的名字来响应。我可以使用:

if message.content.startswith('$hello'):
   if message.author.name= myname
      await message.channel.send(myname 'Hello!')

但是我必须为服务器中的每个用户都这样做,这会花费太长时间。有没有其他办法?谢谢!

1 个答案:

答案 0 :(得分:1)

message.author.name 不应该用引号引起来。所以你可以这样做:

if message.content.startswith('$hello'):
   await message.channel.send('Hello ' + message.author.name + '!')

if message.content.startswith('$hello'):
   await message.channel.send(f'Hello {message.author.name}!')