如何使用 Discord.py dm 特定用户?

罗文杜·塔穆迪塔

我正在使用 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但什么也没发生。

多米尼克

我注意到的一些事情:

你定义了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")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章