繁体   English   中英

discord.js 检查 DM 是否通过并发送消息是否通过

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

我正在制定一个命令,在 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."));
  }
}

问题是它捕获了错误并发送消息“我无法 rickroll 用户”,但如果它成功地向用户发送消息,我希望它发送“成功 rickrolled 用户”,我不知道该怎么做。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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