I'm coding a discord bot with discord.js, and one of the features is that when someone sends a message with the prefix (.exe) with a word right after (e.g. .exebot or .exeserverinv), the bot returns a specific message. The code works just fine, but I know that by just copy pasting the same code over 30 times and just changing the values isn't good optimization.
Is there any simple way that I can make it so that I can just use a loop or something for the command to repeat, but with different values?
Here's the non-optimized code:
command(client, 'command name', message => {
message.channel.send('bot message')
})
command(client, 'random', message =>{
message.channel.send('some text')
})
command(client, 'bot', message =>{
message.channel.send('random text')
})
command(client, 'testing', message =>{
message.channel.send('text')
})
command(client, 'test', message =>{
message.channel.send('example text')
})
command(client, 'second to last test', message =>{
message.channel.send('is almost there')
})
command(client, 'last test', message =>{
message.channel.send('is the very last one')
})
The command handler that this command is running from is:
const { prefix } = require('./config.json')
module.exports = (client, aliases, callback) => {
if (typeof aliases === 'string') {
aliases = [aliases]
}
client.on('message', message => {
const { content } = message;
aliases.forEach(alias => {
const command = `${prefix}${alias}`
if (content.startsWith(`${command} `) || content === command) {
console.log(`Running the command ${command}`)
callback(message)
}
})
})
}
Please advise.