简体   繁体   中英

Multiple if/else conditions (discord bot)

I'm trying to create a simple discord bot, currently using nodeJS. I'm creating specific commands that only specific users can use and whenever someone who does not have permission to use such command can get a reply "You don't have permission". (I hope you get the idea. sorry for the bad wording).

This is my current code:

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

But what happens is, whenever someone uses a command, the yes or no condition will show 4 times, because there are 4 commands.

So if a user tried to use the !ban command the output would be

no permission
no permission
no permission
no permission

I'm pretty sure i messed up something in my if/else conditions but im not sure what it is.. Help is highly appreciated, i'm sorry for the bad wording of things..

As you have all the conditions separately they will execute one by one. You should use else if after the first clause to tie all the clauses. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

You can also use a switch case statement for the use-case you explained. 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.')
    ;}

You can also simplify your if clause by checking the message.author.id first and then proceed to check which command is executed like the following.

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

You can change your code into something like this.

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

The idea is this.

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
}

Or if you want to use if else conditions for some reason.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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