首页 > 解决方案 > 向消息中指定的特定用户发送消息,discord.py

问题描述

在我的 discord.py 代码中,我试图设置一个简单的发送消息给消息作者指定的用户。

    if message.content.startswith('!hello'):
  print("Hello DIRECT MESSAGE")
  msg = "Why hello there"
  print (message.author)
  await client.send_message(message.author, msg)

if message.content.startswith('!poke'):
  print("Poke DIRECT MESSAGE")
  a = message.content[6:]
  print(a)
  msg = "Hi there"
  await client.send_message(a, msg)

!hello 命令工作正常,但我不知道如何在收到用户 ID 作为字符串后将用户 ID 作为发送消息命令的对象。

标签: pythondiscorddiscord.py

解决方案


使用discord.utils.get.

假设这发生在服务器中,命令调用者和目标都是: target = discord.utils.get(message.server.members, id=a)


推荐阅读