向特定人员发送 DM:Discord.js

Sending DMs to specific people: Discord.js

我在向特定人员发送私信时遇到了一些问题。我知道如何向消息的作者发送 DM。但是,我希望尝试直接将 DM 发送给特定的人。

async run(message, args) {
    if (args == 'mock') {
      console.log(message.author);
      message.send(Bloodmorphed,
        '1. *No bad mana mocks (funki manas)* \n' +
        '2. Minimum 500k smite must be used at all times \n' +
        '3. No causing the game to lag using skills on attack/hit/struck \n' +
        '4. Must use delerium on attack/attack or attack/hit \n' +
        '5. No use of stash is allowed \n' +
        '6. No 2nd character to view is allowed \n' +
        '7. Matches should only have the two duelist and the host in the game \n' +
        '8. No stopping your attack once you start unless the opponent and host agree \n' +
        '9. 10 minute limit \n' +
        '10. Dueling area should be cleared by the host prior to the duel \n' +
        '11. Must use Nv Valkyrie or Dopplezon \n' +
        '12. Duels last until someone dies \n' +
        '13. Any death after joining the game will count as a loss \n' +
        '14. Each player will have a chance to be 2nd joiner and 3rd joiner. Higher ranked player will be 2nd joiner first. If both are un-ranked, the challenged will be 2nd joiner first \n' +
        '15. Duels must be in a neutral game \n' +
        '16. No mercs / summoned units allowed \n');
    } else if (args == 'legit') {
      message.send('Legit rules test');
    } else {
      message.reply('Error: The command you have entered is correct. Use !help for help on commands.');
    }
  }
}

我似乎不太明白 discord 一开始是如何处理 DM 的。浏览 discord.js 和 discord 中的文档,js-commando 似乎没有多大帮助。

Discord 通过用户对象(GuildMember 扩展的对象)处理 DM,如您所知 from the documentation。从这里开始,它实现了一个 TextBasedChannel,或者您正在谈论的 DM 频道。要向用户发送消息,您需要按照以下方式执行操作:

async run(message, args) => {
    message.author.send("Hello, this is a DM!");
}

或者,如果您想使用 GuildMember...

async run(message, args) => {
    message.member.user.send("Hello, this is a DM!");
}

不过 Discord.js 上的 DM 有一些特别之处。

  1. 如果您正在分片,所有 DM 都将转到分片 1。(Source)
  2. 您将始终有权发送嵌入。
  3. 用户可以禁用直接消息。

要检测 DM 消息,我喜欢用我的消息事件执行此操作:

bot.on("message", async m => {
    m.isDM = (m.guild ? false : true);
});

这是可行的,因为如果收到 DM 消息,公会对象将为空。另外,请记住,在发送 DM 时,机器人无法检查用户是否禁用了 DM。这意味着捕获所有失败的 DM 消息非常重要。这是一个例子。

async run(message, args) => {
    message.author.send("Hello, I'm a DM that handles if users don't have permission!").catch(e => {
        message.channel.send("There was an internal error attempting to send you a message.\n" + "```js\n" + e.stack + "\n```";
    }
}

根据要求,这里有一个在 commando 中使用上述所有内容的示例:

client.on("commandRun", cmd => {
    cmd.message.message.isDM = (m.guild ? false : true);
    cmd.message.message.author.send("Hello, I'm a DM that handles if users don't have permission!").catch(e => {
        cmd.message.message.channel.send("There was an internal error attempting to send you a message.\n" + "```js\n" + e.stack + "\n```";
    });
});

祝你好运,编码愉快!