簡體   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