[Tutorial] Beginner discord bot (With command handler)

javascript
html
#1

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 and another file called keepOnline.js


    3:
  • Inside index.js we are going to have your Handlers!
const Discord = require("discord.js")
const client = new Discord.Client();
const fs = require("fs");
require(__dirname + '/app/keepAlive.js')
//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)
  • Inside the keepOnline.js we are going to have express keeping the webpage up for Uptime Robot to send a ping too!
var express = require('express');
var app = express();
var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});
app.use(express.static('public'));
app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});
  • Create a file called ìndex.html in a folder called views.
  • Inside the index.html file simply put the word “Ok” (If you want to make this used as a webpage then you can!)

4: Message Event

  • Create a file called message.js in a folder called events.
  • Now we will use our message event code!
let 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(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);

  let commandfile = client.commands.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 = async(client, message, args) => {
message.channel.send("Pong :) ")

}

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!

2 Likes

#2

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

1 Like