简体   繁体   中英

discord.js checking if a DM went through and sending a message if it did or not

im making a command that rickrolls people in dms and currently it sends the message "could not rickroll the user" if it could not message that person, but does not send a message that it successfully rickrolled the user because i could not figure out how to do that

const { DiscordAPIError } = require('discord.js');
const Discord = require('discord.js');
const BaseCommand = require('../../utils/structures/BaseCommand');

module.exports = class RickrollCommand extends BaseCommand {
  constructor() {
    super('rickroll', 'fun', []);
  }

  async run(client, message, args) {
    const mentionedMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    if (!args[0]) return message.channel.send('You need to mention a member to rickroll.');
    if (!mentionedMember) return message.channel.send('The user mentioned is not in the server.');

    const rickrollEmbed = new Discord.MessageEmbed()
    .setTitle("You've Been Rickrolled!")
    .setThumbnail('https://i1.wp.com/my-thai.org/wp-content/uploads/rick-astley.png?resize=984%2C675&ssl=1')
    .setFooter('Rick astley sends his regards')
    .setColor('#FFA500');

      await mentionedMember.send('https://tenor.com/view/dance-moves-dancing-singer-groovy-gif-17029825')
    await mentionedMember.send(
      rickrollEmbed
    ).catch(async err => message.channel.send("i was unable to rickroll the user."));
  }
}

the problem is that it catches the error and sends the message "i was unable to rickroll the user" but if it does successfully message the user i want it to send "Successfully rickrolled the user" and i couldnt figure out how to do that.

If I'm not mistaken you can simply add a then() when your "promise" is fulfilled. This ties into how async/await functions work. For more info you could check out various other resources online, just a quick google search will do.

To add the then() method into your code, I'd suggest:

//THEN METHOD HERE
.then(() => {
    message.channel.send("Successfully rickrolled the user.");
})
//CATCH METHOD HERE TO CHECK FOR REJECTED PROMISES
.catch(err => { //no need to use the async
    message.channel.send('I was unable to rickroll the user.');
});

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