【问题标题】:How can I dm a specific User by using Discord.py?如何使用 Discord.py 对特定用户进行 dm?
【发布时间】:2021-11-21 21:29:12
【问题描述】:

我正在使用 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.fetch_user("71123221123") 正在做你认为它正在做的事情吗?由于添加了意图,您可能需要先更新用户缓存,然后才能执行此类操作。

标签: python discord.py dm


【解决方案1】:

使用await user.send("message")

【讨论】:

    【解决方案2】:

    我注意到的一些事情:

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

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

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

    【讨论】:

    • 那么我如何管理client.run ("token")
    • 只是为了做对:您接受了我的回答,但因为您还有其他问题而未接受?这是一种奇怪的处理方式。 client.run("YourToken") 必须在代码的末尾,即使这不是您的问题的一部分。我只是提供了相关代码。
    • 哦,对不起,兄弟,但是当我这样做时,它会给我一个错误..
    • 错误是什么?
    • error is too long to type this is error [error] (ghostbin.com/OHkqK)
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2021-12-24
    • 2020-12-27
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多