discord.js 检查 DM 是否通过并发送消息(如果通过)

时间:2021-04-12 20:21:32

标签: javascript discord discord.js bots

我正在执行一个命令,在 dms 中对人进行 rickroll,目前它会发送消息“无法对用户进行 rickroll”,如果它无法向该人发送消息,但不会发送一条消息,表明它已成功 rickroll 用户,因为我不知道知道怎么做

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

问题在于它捕获错误并发送消息“我无法对用户进行滚动”,但如果它成功向用户发送消息,我希望它发送“成功对用户进行滚动”,但我不知道如何这样做。

1 个答案:

答案 0 :(得分:2)

如果我没记错的话,您可以在“承诺”兑现时简单地添加一个 then()。这与 async/await 函数的工作方式有关。如需更多信息,您可以在线查看各种其他资源,只需在谷歌上快速搜索即可。

要将 then() 方法添加到您的代码中,我建议:

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