Switch cases are a nice feature that can be used without the message.flags
feature, and can be used like this.
// This will look for the first argument. If your command was 'hi', and you did 'hi send', it would send 'Hi!' to the channel.switch (args[0]) {case 'send': {// This will send in the channel that the command was run in.message.channel.send('Hi!');break;}case 'dm': {// This will DM the message author..message.author.send('Hi!');}}
However, you can use the switch case with message.flags
.
// This will look for the first argument beginning with -, the flag. If your command was 'hi', and you did 'hi -send', it would send 'Hi!' to the channel.switch (message.flags[0]) {case 'send': {// This will send in the channel that the command was run in.message.channel.send('Hi!');break;}case 'dm': {// This will DM the message author.message.author.send('Hi!');}}
Let's say you're trying to make a command to add or take a role to/from a user. This can be consolidated into one command using message.flags
. This command is based off of the Guidebot framework, and is called role.
exports.run = async (client, message, args, level) => { // eslint-disable-line no-unused-varsswitch (message.flags[0]) {// This would be 'role -add'.case 'add': {// Check if the message mentions a user.if (message.mentions.members.size === 0) return message.reply('Please mention a user to give the role to.');const member = message.mentions.members.first();// This is the name of the role. For example, if you do 'role -add @York#2400 The Idiot Himself', the name of the role would be 'The Idiot Himself'.const name = args.slice(1).join(' ');// Find the role on the guild.const role = message.guild.roles.cache.find(r => r.name === name);// End the command if the bot cannot find the role on the server.if (!role) return message.reply('I can\'t seem to find that role.');try {await member.roles.add(role);await message.channel.send(`I've added the ${name} role to ${member.dsiplayName}.`);} catch (e) {console.log(e);}break;}case 'remove': {// Check if the message mentions a user.if (message.mentions.members.size === 0) return message.reply('Please mention a user to take the role from.');const member = message.mentions.members.first();// This is the name of the role. For example, if you do 'role -remove @York#2400 The Idiot Himself', the name of the role would be 'The Idiot Himself'.const name = args.slice(1).join(' ');// Find the role on the guild.const role = message.guild.roles.cache.find(r => r.name === name);// End the command if the bot cannot find the role on the server.if (!role) return message.reply('I can\'t seem to find that role.');try {await member.roles.remove(role);await message.channel.send(`I've removed the ${name} role from ${member.displayName}.`);} catch (e) {console.log(e);}}}};exports.conf = {enabled: true,guildOnly: true,aliases: [],permLevel: 'Moderator'};exports.help = {name: 'role',category: 'Moderation',description: 'It can give or take a role to/from a user.',usage: 'role <-add|-remove> <role name>'};