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!
- 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");
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}`);
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
fs.readdir('./events/', (err, files) => {
if (err) console.log(err);
files.forEach(file => {
let eventFunc = require(`./events/${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 calledevents
. - 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)) return;
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 calledcommands
- Inside that file put the following!
module.exports = {
config: {
aliases: ["pong", "pingpong"]
},
run: () => {
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!