0

我正在用 discord.py 制作一个不和谐的机器人,当用户使用特定命令时,我想要一个特定的用户。

from discord import DMChannel

client = discord.Client()
client = commands.Bot(command_prefix=']')

@client.command(name='dmsend', pass_context=True)
async def dmsend(ctx, user_id):    
  user = await client.fetch_user("71123221123")
  await DMChannel.send(user, "Put the message here")

当我发出命令]dmsend时,什么也没有发生。而且我也尝试过dmsend。但是什么也没发生。

4

2 回答 2

3

我注意到的几件事:

你定义client了两次,那只会出错。

首先)删除client = discord.Client(),你不再需要它了。

如果要向特定用户 ID 发送消息,则不能将其括在引号中。此外,您应该小心fetch,因为这样会向 API 发送请求,并且它们是有限的。

二)更改await client.fetch_user("71123221123")为以下内容:

await client.get_user(71123221123) # No fetch

如果你有user你想要的消息去,你不需要创建另一个DMChannel.

三)做成await DMChannel.send()如下:

await user.send("YourMessageHere")

您可能还需要启用membersIntent,这里有一些很好的帖子:

打开 Intents 后的完整代码可能是:

intents = discord.Intents.all()

client = commands.Bot(command_prefix=']', intents=intents)

@client.command(name='dmsend', pass_context=True)
async def dmsend(ctx):
    user = await client.get_user(71123221123)
    await user.send("This is a test")

client.run("YourTokenHere")
于 2021-09-30T11:44:04.957 回答
0

采用await user.send("message")

于 2021-09-30T08:04:11.450 回答