我可以在try-catch方法中使用if语句吗?

acruukt9  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(570)

我在用电话 guild.fetchVanityData() 方法使用discord.js库在我的discord bot中获取公会的虚荣数据。我的 invite.js 命令抛出一个错误,表示 This guild does not have the VANITY_URL feature enabled. 我知道公会没有启用它,我正在向用户发送一条自定义消息,告诉他们这一点。因此,我不希望它抛出错误。我用的是一个 if 登记我的 message.js 如果执行的命令是invite命令(引发错误的命令),则使用 return 方法。这似乎不起作用,因为它仍然会抛出错误。
我整个机器人中唯一使用 try-catch 就在 message.js .
我的代码在下面,请帮助我。
message.js事件文件

module.exports = {
    name: 'message',
    execute(message, client) {
          const prefixes = ['!1','833376214609690674', "<@!833376214609690674>","<@833376214609690674>"]
      const prefix = prefixes.find(p => message.content.startsWith(p));
      if (!prefix || message.author.bot) return;

      const args = message.content.slice(prefix.length).trim().split(/ +/);
      const commandName = args.shift().toLowerCase();

      if (message.content === "<@833376214609690674>" || message.content === "<@!833376214609690674>" || message.content === "833376214609690674" || message.content === "!1") {
      const mCommand = client.commands.get('help')
      mCommand.execute(message, args)
                     }

      if (!client.commands.has(commandName)) return message.channel.send("Please provide a valid command.")

      const command = client.commands.get(commandName);

      try {
        command.execute(message, args, client);
     }
    catch (error) {
      if(command.name === "invite") {
       return
      }

     else {
     console.log(error)
     }

    }

    },
};

invite.js命令文件

const Discord = require('discord.js')

module.exports = {
    name: 'invite',
    category: "utility",
    description: 'Get the bot invite or server invite info.',
    execute: async (message, args) => {
      if (args[0] === 'bot') {

        const embed = new Discord.MessageEmbed()
        .setTitle('Bot Invite')
        .setDescription("Returns the bot's invite link.")
        .addFields({
          name: 'Invite',
          value: "[Click here](https://discord.com/api/oauth2/authorize?client_id=833376214609690674&permissions=4294442710&scope=bot%20applications.commands) to invite the bot to your server."
        })

        message.channel.send(embed)
      }
      else if (args[0] === 'server') {

          message.guild.fetchVanityData()
          if (!message.guild.vanityURLCode) {

            const embed = new Discord.MessageEmbed()
            .setTitle('Server Invite')
            .setDescription("Returns the server's Vanity URL if it has one.")
            .addFields({
              name: 'Vanity URL',
              value: 'This server does not have a Vanity URL.',
              inline: true
            },
            {
              name: 'Uses',
              value: 'Not Applicable',
              inline: true
            })
            .setTimestamp()

            return message.channel.send(embed)
          }

          const embed = new Discord.MessageEmbed()
          .setTitle('Server Invite')
          .setDescription("Returns the server's Vanity URL if it has one.")
          .addFields({
            name: 'Vanity URL',
            value: `https://discord.gg/${message.guild.vanityURLCode}`,
            inline: true
          },
          {
            name: 'Uses',
            value: `${message.guild.vanityURLUses}`,
            inline: true
          })

         message.channel.send(embed)

    }
}
}

我认为这是所有需要的代码,但如果您希望看到我的其他命令、文件等,可以转到以下repl:https://replit.com/@smsvra6/多机器人

ebdffaop

ebdffaop1#

根据discord.js文档, <Guild>.fetchVanityData 回报一个承诺。因此,您可以使用 <Promise>.then<Promise>.catch 语法。
即。:

message.guild.fetchVanityData()
.then(data => {
    // success, the server has a vanity url ;
})
.catch(error => {
   // error, the server does not have a vanity url.
});

不需要if语句。

相关问题