【问题标题】:How to send a DM to a specific member with discord.js? [closed]如何使用 discord.js 向特定成员发送 DM? [关闭]
【发布时间】:2022-02-05 01:23:31
【问题描述】:

我正在使用 discord.js 制作一个机器人,其中包含我想向特定成员发送消息的功能。

如何向特定成员发送消息(使用识别码,例如“346176733549953029”)而不是使用向该人发送消息的 message.author.send("the text I want to send")谁输入命令?

我需要类似的东西:

message.user.get("346176733549953029").send("test I want to send");

最后应该是让该成员接收来自机器人的私人消息,其中包含消息......

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以通过用户 ID 从客户端的用户缓存 Client.users 中检索用户。但是,不能保证每个用户都始终被缓存,因此您可以使用 Client.fetchUser() 从 Discord 中获取用户。请记住,它返回一个Promise

    您可以使用User.send()(或GuildMember.send())向用户发送消息。

    把它放在一起......

    // Async context needed for 'await' keyword.
    // Assuming 'client' is the instance of your Discord Client.
    
    try {
      // Retrieve the user from the client's cache.
      // If they haven't been cached yet, fetch them.
      const userID = '189855563893571595';
      const user = client.users.get(userID) || await client.fetchUser(userID);
    
      // Send the user a DM.
      await user.send('Hello.');
    } catch(err) {
      console.error(err);
    }
    

    请注意,由于用户的特定隐私设置,可能无法发送 DM。在这种情况下,您需要捕获被拒绝的承诺。

    【讨论】:

    • 再次感谢您的帮助,您再次找到了解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2018-07-11
    • 2021-02-24
    • 2020-10-08
    • 1970-01-01
    • 2021-12-24
    • 2022-01-18
    相关资源
    最近更新 更多