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

标签 javascript node.js discord.js

我在向特定人员发送 PM 时遇到了一些问题。我知道如何向消息的作者发送 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 channel 。要向用户发送消息,您需要执行以下操作:

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。

要检测 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```";
    }
}

根据要求,这是在突击队中使用上述所有内容的示例:

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```";
    });
});

祝你好运,编码愉快!

关于javascript - 向特定人员发送 DM : Discord. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495205/

相关文章:

javascript - NodeJS 项目没有添加到对象的索引上?

javascript - "gameActivity" key 在开始时更改但应该停止

javascript - 如何检查在 Javascript 中作为参数传递的回调函数是否有参数?

javascript - 如何向指定 channel 发送消息?

node.js - 音乐不和谐 js 机器人问题

javascript - 在 RxJS 中组合不同类型的 Observable,但只发出来自其中之一的值

JavaScript forEach : mutate elements

javascript - Bootstrap Modal 关闭时专注于输入

javascript - 在Webpack中,如何将HTML字符串当作文件一样使用?

node.js - 如何正确 `close` node.js 服务器?