简体   繁体   中英

Sending DMs to specific people: Discord.js

I'm having some issues sending a PM to a specific person. I know how to send a DM to the author of the message. However I am looking to trying to send a DM directly to a specific person.

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.');
    }
  }
}

I can't quite seem to figure out how discord even handles DMs in the first place. Going through the documents in discord.js and discord,js-commando doesn;t seem to help much.

Discord handles DMs through the User object (what GuildMember extends), as you can tell from the documentation . From here, it implements a TextBasedChannel, or the DM channel you're talking about. To send a message to a user, you would do something along these lines:

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

Or, if you want to use a GuildMember ...

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

There's a few special things about DMs on Discord.js, though.

  1. If you're sharding, all DMs will go to Shard 1. ( Source )
  2. You will always have permission to send embeds.
  3. The user can have DMs disabled.

To detect a DM message, I like to do this with my message event:

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

This works because if a DM message comes in, the guild object will be null. Also, please keep in mind that when sending a DM, there is no way for the bot to check if the user has DMs disabled. This means it's really important to catch all DM messages for failure. Here's an example.

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

As per request, here's an example of using everything above in 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```";
    });
});

Good luck, and happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM