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

2024-09-29 22:18:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用discord.py创建一个discord bot,当用户使用特定命令时,我希望创建一个特定用户

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时,什么也没有发生。我也试过了。但什么也没发生


Tags: 用户frompyimport命令clientbotawait
2条回答

我注意到了几件事:

您定义了client两次,这只会出错

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

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

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

await client.get_user(71123221123) # No fetch

如果您有希望消息转到的user,则不需要创建另一个DMChannel

Third)await DMChannel.send()转换为以下内容:

await user.send("YourMessageHere")

您可能还需要启用members意图,下面是一些关于这方面的好文章:

打开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")

使用await user.send("message")

相关问题 更多 >

    热门问题