Code Your Own Discord Bot - Advanced Command Handler (2021)

Alesh the Dev · 318K views · 2020 · 10:12

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering Discord.js questions about managing many commands, as a pointer to setting up a proper command handler instead of long if/else chains.

Recommended by 2 people across 2 different months, 2020 to 2021.

What it is

A roughly ten minute tutorial from the channel Alesh the Dev on building an advanced command handler for a Discord bot with Discord.js and Node.js. It walks through splitting commands into separate files and loading them, as a step up from the basic handler approach.

What people said

Recommends ashwyn · 2020-08 · Stack Overflow · on “Trying to embed hyperlinks for a discord bot with javascript”

I want to create a discord bot that when someone types -ping it then gives out a embeded hyperlink. current code i have is: "embed.js" const Discord = require('discord.js'); const client = new Discord.Client(); const prefix = '-'; const fs = require('fs'); client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js')); for(const file of commandFiles){ const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.once('ready', () => { console.log('embed is online!'); }); client.on('message', message =>{ if(!message.content.startsWith(prefix) || message.author.bot) return; const args = me

Recommends Umbel_ · 2021-04 · Stack Overflow · on “Discord.js Multiple Commands”

as a temporary solution, you can use "else if()" like so: const Discord = require('discord.js'); const client = new Discord.Client(); client.on('message', message => { if(message.content === '!command1') { message.channel.send('working!'); } else if(message.content === '!command2') { message.channel.send('working!'); } else if(message.content === '!command3') { message.channel.send('working'); } }); client.login('Your Token goes here'); a better solution is to set up a command handler. there are a lot on youtube, one is: [this video] hope this helped!

Cites Pandicon · 2020-09 · Stack Overflow · on “How can I put these commands into a command handler?”

I will show it to you on an easy example: Your main file might look like this (just add the thing you don't have there): const Discord = require('discord.js'); const client = new Discord.Client(); const prefix = '&'; const version = '1.1'; const fs = require('fs'); client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js')); for(const file of commandFiles){ const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.on('message', message =>{ if(!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const com