crazyc4t's blog

How to code your own discord bot!

Patrick Star Bot 📕

GitHub License GitHub last commit

This is my first time programming a bot on discord with discord.js! This bot is about having my classes at my fingertips, so it gives links with special commands that facilitate the process. You are free to clone the code and use it the way you want! You can edit it and make it your own!

Demo

Commands

Prefix: 9

The prefix means that is the initial character that is used to communicate with the bot!

CommandFunctionality
9infoInformation about the project
9helpShows all the commands available
9mailSends a gmail link
9idukaySends a idukay link (learning platform from my school)
9dayIt shows the classes of the day

Want to edit it for yourself? Follow along!

The code itself is documented so go check it out here!

let’s do this step by step!

Requirements

And some knowledge on javascript!

Guide yourself with the official discord.js docs!

Before we begin

We need to clone this repo with this command:

1git clone https://github.com/crazyc4t/PatrickStar.git

And then to install the discord js library with a simple npm command!

1npm install discord.js

create a new application in the developer portal of discord, over here!, once you go there create a new app and invite it to your server!

After that, open the project folder in your favourite text editor and let’s start!

Logging into discord

Now in your folder project where you have your bot.js file, and create a config.json that is where we are going to store sensitive data so we can ignore it in a .gitignore file.

Then in your config.json should look something like this:

1{
2  "prefix": "X",
3
4  "token": "X",
5
6  "channelid": "X",
7
8  "guildid": "X"
9}

What you are going to store:

Now we will start with the javascript Code:

 1// require the discord.js and cron module
 2const Discord = require("discord.js");
 3const cron = require("cron");
 4
 5// create a new Discord client
 6const client = new Discord.Client();
 7
 8// remote config file where my data is stored
 9const { prefix, token, channelid, guildid } = require("./config.json");
10
11// When client is ready, run this:
12// This will only run one time after logging in
13client.once("ready", () => {
14  console.log(`Online as ${client.user.tag}`);
15  client.user.setActivity("9help");
16  const kalamardo = client.guilds.cache.get(guildid);
17  const channel = kalamardo.channels.cache.get(channelid);

First we start by declaring the variables of the modules we are going to need, then we create the discord client with the variable of client and that is what we are going to use to refer to discord.

Then we create the variables from the config.json to use them in the javascript file. The client.once is a discord.js function that is going to execute something just once when the bot is once ready, as in this bot we use it to tell us that is running with the client.user.tag that is the name of our bot, and we declare the variables of the channel where we want to send the messages.

Setting up Cron

Cron is a npm library that allows us to run something on scheadule, we will use it to notify us everytime we have class!

to install it we need to execute:

npm install cron

Now we are going to create every cron job to run when the bot once log in:

 1const notificationclass = `You have class! GO NOW ${idukay}`;
 2
 3const classbreak = "You have a break right now, patience and be calm, enjoy!";
 4// send a message if it is the time specified.
 5let classnotification1 = new cron.CronJob("00 00 13 * * 1-5", () => {
 6  channel.send(notificationclass);
 7});
 8
 9let classnotification2 = new cron.CronJob("00 35 13 * * 1-5", () => {
10  channel.send(notificationclass);
11});
12
13let classnotification3 = new cron.CronJob("00 10 14 * * 1-5", () => {
14  channel.send(notificationclass);
15});
16
17let classnotification4 = new cron.CronJob("00 45 14 * * 1-5", () => {
18  channel.send(classbreak);
19});
20
21let classnotification5 = new cron.CronJob("00 00 15 * * 1-5", () => {
22  channel.send(notificationclass);
23});
24
25let classnotification6 = new cron.CronJob("00 35 15 * * 1-5", () => {
26  channel.send(notificationclass);
27});
28
29let classnotification7 = new cron.CronJob("00 10 16 * * 1-5", () => {
30  channel.send(notificationclass);
31});
32
33let classnotification8 = new cron.CronJob("00 45 16 * * 1-5", () => {
34  channel.send(classbreak);
35});
36
37let classnotification9 = new cron.CronJob("00 00 17 * * 1-5", () => {
38  channel.send(notificationclass);
39});
40
41let classnotification10 = new cron.CronJob("00 35 17 * * 1-5", () => {
42  channel.send(
43    `You have ended your classes for today! enjoy the rest of your day`
44  );
45});
46
47// When you want to start it, use:
48classnotification1.start();
49classnotification2.start();
50classnotification3.start();
51classnotification4.start();
52classnotification5.start();
53classnotification6.start();
54classnotification7.start();
55classnotification8.start();
56classnotification9.start();
57classnotification10.start();
58});
59
60// login to Discord with token
61client.login(token);

As you see the cronjob format of time is:

So right now is where you want to edit to your school scheadule and your own messages! Try it to be useful (like the zoom link of the class for example) so it can save you some time! After that, we login to discord with the client.login(token); function.

Create reply comments

 1client.on("message", (message) => {
 2  if (message.content === `${prefix}help`) {
 3    // reply in the same channel from it was sent
 4    message.channel.send(help());
 5
 6    // you concatenate messages with the else if block
 7  } else if (message.content === `${prefix}calendar`) {
 8  message.channel.send(`There you go! ${calendar}`);
 9  } else if (message.content === `${prefix}idukay`) {
10  message.channel.send(`Voila! ${idukay}`);
11  } else if (message.content === `${prefix}mail`) {
12  message.channel.send(`Sent! ${gmail}`);
13  } else if (message.content === `${prefix}day`) {
14  message.channel.send(`Today's scheadule: ${days[today - 1]}`);
15  } else if (message.content === `${prefix}info`) {
16  message.channel.send(
17    `This was made by Said Neder\nGithub: ${github}\nDiscord: ${discordid}`
18   );
19  }
20});

First we use the client.on() function to listen to new messages to do something about it, which in this case is to reply to the messages.

And then we use a big if/else block to control the flow of the program, there is where we decide the messages that we are going to listen, to reply them.

Finally running the bot!

And to see your final project use this commnad: node bot.js Since this runs in the back end, and we use node for that, and enjoy your new bot!

But it will only run if you are running the task, if you want to have it live 24/7 we have a extra step!

Setting up the bot live on heroku! (optional)

We will go straight to the chase my boi!

First we need to create in our file a git repo, we do that with:

git init

Then, we create a Procfile inside of our folder project with this text:

worker: npm start

And that means that the heroku server is going to execute the code in a form of a worker, not as a webpage.

And now we add everything to git:

git add .

And then we commit the files added:

git commit -m "deployment"

Now we need to create a heroku account and create a new app over here!

After that we download the heroku cli in our system, when that is done we login into the cli with the command

heroku login And after that we just push it to heroku!

git push heroku main

When that is done we go into the dashboard > resources and deactivate the web dyno by just clicking edit and switching off the slide, and now we turn on the worker that should appear, now it should be left like this

image example

And after that you are done! Hope that you enjoyed the guide!

#Javascript