【问题标题】:Multiple if/else conditions (discord bot)多个 if/else 条件(不和谐机器人)
【发布时间】:2021-04-19 08:37:55
【问题描述】:

我正在尝试创建一个简单的不和谐机器人,目前正在使用 nodeJS。 我正在创建只有特定用户才能使用的特定命令,并且每当无权使用此类命令的人可以得到回复“您没有权限”。 (我希望你能明白。抱歉措辞不好)。

这是我当前的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '!';

client.once('ready', () => {
    console.log('Bot is online');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
    

    if(command === 'ban' && message.author.id === "123456789"){
    message.channel.send('suspended');
    }
    else{message.channel.send('no permission.')
    ;}
    if(command === 'chat' && message.author.id === "123456789"){
    message.channel.send('chat-restricted');
    }
    else{message.channel.send('no permission.')
    ;}
    if(command === 'coins' && message.author.id === "123456789"){
    message.channel.send('balance updated.');
    }
    else{message.channel.send('no permission.')
    ;}
    
    if(command === 'coins 2' && message.author.id === "123456789"){
    message.channel.send('balance updated.');
    }
    else{message.channel.send('no permission.')
    ;}
});

但实际情况是,每当有人使用命令时,是或否条件将显示 4 次,因为有 4 个命令。

因此,如果用户尝试使用 !ban 命令,输出将是

no permission
no permission
no permission
no permission

我很确定我在 if/else 条件下搞砸了一些东西,但我不确定它是什么.. 非常感谢您的帮助,我很抱歉措辞不好..

【问题讨论】:

    标签: javascript node.js if-statement discord discord.js


    【解决方案1】:

    你可以把你的代码改成这样。

    const Discord = require('discord.js');
    const client = new Discord.Client();
    const prefix = '!';
    
    client.once('ready', () => {
        console.log('Bot is online');
    });
    
    client.on('message', message =>{
        if(!message.content.startsWith(prefix) || message.author.bot) return;
        const args = message.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();
        
        if (message.author.id === '123456789') {
          switch (command) {
            case 'ban':
              message.channel.send('suspended');
              break;
            case 'chat':
              message.channel.send('chat-restricted');
              break;
            case 'coins':
              message.channel.send('balance updated.');
              break;
            case 'coins 2':
              message.channel.send('balance updated.');
              break;
            default:
              message.channel.send('unknown command!');
              break;
          }
        } else {
          message.channel.send('no permission.');
        }
    });
    

    想法是这样的。

    let command = 'ban';
    let message = {
      author: {
        id: -1,
      },
    };
    
    if (message.author.id == '123456789') {
      switch (command) {
        case 'ban':
          // send suspended
        break;
        case 'chat':
          // send chat-restricted
        break;
        // ...
      }
    } else {
      // send no permission
    }
    

    或者如果你出于某种原因想要使用 if else 条件。

    const Discord = require('discord.js');
    const client = new Discord.Client();
    const prefix = '!';
    
    client.once('ready', () => {
        console.log('Bot is online');
    });
    
    client.on('message', message =>{
        if(!message.content.startsWith(prefix) || message.author.bot) return;
        const args = message.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();
        
        if (command === 'ban' && message.author.id === "123456789") {
            message.channel.send('suspended');
        } else if (command === 'chat' && message.author.id === "123456789") {
            message.channel.send('chat-restricted');
        } else if (command === 'coins' && message.author.id === "123456789") {
            message.channel.send('balance updated.');
        } else if (command === 'coins 2' && message.author.id === "123456789") {
            message.channel.send('balance updated.');
        } else if (message.author.id !== "123456789") {
            message.channel.send('no permission.');
        } else {
            message.channel.send('Unknown command!');
        }
    });
    

    【讨论】:

      【解决方案2】:

      由于您分别拥有所有条件,它们将一一执行。您应该在第一个子句之后使用else if 来绑定所有子句。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

      您还可以对您解释的用例使用 switch case 语句。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

          if(command === 'ban' && message.author.id === "123456789"){
              message.channel.send('suspended');
          }
          else if(command === 'chat' && message.author.id === "123456789"){
              message.channel.send('chat-restricted');
          }
          else if(command === 'coins' && message.author.id === "123456789"){
              message.channel.send('balance updated.');
          }
          else if(command === 'coins 2' && message.author.id === "123456789"){
              message.channel.send('balance updated.');
          }
          else{message.channel.send('no permission.')
          ;}
      

      您还可以通过首先检查 message.author.id 来简化 if 子句,然后继续检查执行的命令,如下所示。

      if (message.author.id === "123456789"){
         if(command === "coins 2"){
            ...
         else if(command === "chat"){
            ...(command specific execution)
         }
         ... (rest of the commands)
      }else{
         message.channel.send("No permission.")
      }
      
      

      【讨论】:

        猜你喜欢
        • 2022-01-26
        • 2021-11-25
        • 2022-11-02
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-03
        • 2020-09-23
        相关资源
        最近更新 更多