Beginner discord bot (With command handler)

Hello, glitch community!
This is just a starter bot that you can create to get your discord bot development on the road!

  • This will be javascript and will be using the discord.js dependency.



1: Ok, so the first step is going to be creating your project!

  • Go to https://glitch.com/
  • Click on “New project” on the top right!
    image
  • Select “hello-express”

2: Step 2 is going to be getting all the needed files created!

  • Delete all current files. (Excluding package.json & .env)
  • Create a file called index.js

3:

  • Inside index.js we are going to have your Handlers!
const Discord = require("discord.js"),
      client = new Discord.Client(),
      fs = require("fs");
//Command Handler
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.events = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {
    if (err) return console.log(err);
    files.forEach(file => {
        if (!file.endsWith(".js")) return;
        let props = require(`./commands/${file}`);
        console.log("Successfully loaded " + file)
        let commandName = file.split(".")[0];
        client.commands.set(commandName, props);
    });
});
    //Events "handler"
    fs.readdir('./events/', (err, files) => {
        if (err) console.log(err);
        files.forEach(file => {
            let eventFunc = require(`./events/${file}`);
            console.log("Successfully loaded " + file)
            let eventName = file.split(".")[0];
            client.on(eventName, (...args) => eventFunc.run(client, ...args));
        });
});

client.on("ready", () => console.log("Online!"));
client.login(process.env.TOKEN)

4: Message Event

  • Create a file called message.js in a folder called events.
  • Now we will use our message event code!
const prefix = "Your desired prefix here!"  

 exports.run = async(client, message) => {
  if (message.author.bot) return;
  if (message.content.startsWith(prefix)) {
    
 let messageArray = message.content.split(" "),
     cmd = messageArray[0],
     args = messageArray.slice(1),
     commandfile = client.commands.get(cmd.slice(prefix.length)) || client.aliases.get(cmd.slice(prefix.length));
  
if(!commandfile) return;    
    commandfile.run(client,message,args);             
  }

5: First command!

  • Create a file called ping.js in a folder called commands
  • Inside that file put the following!
exports.run = (client, message, args) => {
  message.channel.send("Pong :)");
}
exports.config = {
  aliases: ["pong", "pingpong"]
}

6: Finishing touches:

  • Install the dependencies: “discord.js”, “fs” in your package.json.
  • Change the “Start” script in package.json to “node index.js”
  • Go to your .env file and put in "TOKEN=Your bots token

If you encounter any problems then let me know!

10 Likes

Just to let you know, the fs module is installed by default by Node as it’s part of Node’s core.

3 Likes

I know this is old but, what are you using client.aliases for?

1 Like

I think this was meant to be for aliases on a command, but he didn’t implement it (yet)

2 Likes

“yet” - I love how you said that, and who knows :wink: - Maybe you are correct!

Edit: Been too long since my last edit on the initial message hahah, cannot edit it!

1 Like

If you don’t add aliases soon i’ll be forced to make aliases work myself :eyes:

If you do need help for aliases, I will be happy to talk to you on discord about this a bit further! Callum#6052

thanks haha but I already know how to add aliases and such, will still add you on discord :+1:

1 Like

Thanks for the information

1 Like

My bot will not go online

1 Like

Do any errors come up in the logs pane?

How would I add aliases and more events like welcome,status,etc. in different files?

Good keep going :smiley:

1 Like

I have made a few changes and fixes to the tutorial and it luckily works with discord.js v12 aswell!

I have updated the tutorial, aliases SHOULD work now with it.

2 Likes

I think you posted your complete logs, I think only the first error is necessary.

ok but do u know what is wrong ?

const prefix = !
should be
const prefix = “!”

2 Likes

Moved to #uncategorized since this violates the TOS. @glitch_support, please editing the main post or deleting this thread.

What do you mean by “violating the TOS”? It still counts as a #tutorials, we could just ping @Callum-OKane to edit the main post and remove the part about pinging. There’s no need for Glitch Support and editing the main post and TOS violations :slightly_smiling_face:

I’m moving this back to #tutorials as I see no other problem other than to use UptimeRobot (even if someone used UptimeRobot, it won’t actually, it’ll just return a 403). After all, a tutorial is a tutorial.

1 Like