So I was recently approached by a friend to make him a discord bot that reminds people about meetings in a discord server. I thought it would be a good idea to share it with you.

Discord

What is Discord?

Discord is a chat platform that is used by many people to communicate with each other. It is very similar to Slack, but it is free and has a lot of features. It is also very popular among gamers.

As Discord has gotten more popular, people have started to use it for more than just chatting. For example, some people use it to organize meetings or events. This is where the problem comes in. People tend to forget about meetings, and they don’t show up. This is where the bot comes in.

So let’s start!

The Bot

For the bot, I will use discord.js. It is a very popular library for making discord bots. It is also very easy to use and I have made a few bots with it before. You can find them at my github 😉

So let’s start by creating a new folder and installing discord.js.

mkdir discord-reminder
cd discord-reminder
npm init -y
npm install discord.js

Now we can start by creating a new file called index.js. This is where we will write our code.

const Discord = require('discord.js');
const client = new Discord.Client();

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.login('token');

This is the basic code for a discord bot. It will log in to discord and print a message to the console when it is ready. You can find your token in the discord developer portal.

Now we will need to create a command to add a reminder. We will use the !remind command for this. We will also need to create a command to list all the reminders. We will use the !list command for this.

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    // Add reminder
  } else if (msg.content.startsWith("!list")) {
    // List reminders
  }
});

For us to get the exact time and date we will use something called a timestamp. A timestamp is a number that represents a specific time. It is measured in milliseconds since January 1, 1970. We can use the Date.now() function to get the current timestamp.

const timestamp = Date.now();

Now we will need to get the role that we will mention as well as the timestamp that the user wants to be reminded at.

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    const args = msg.content.split(" ");
    const timestamp = args[2];
    const role = args[1];
  } else if (msg.content.startsWith("!list")) {
    // List reminders
  }
});

Now we will need to create a reminder object that will contain the timestamp and the role that we want to mention. We will also need to push it to an array that will contain all the reminders.

let reminders = []

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    const args = msg.content.split(" ");
    const timestamp = args[2];
    const role = args[1];
    const reminder = {
      timestamp: timestamp,
      role: role
    };
    reminders.push(reminder);
  } else if (msg.content.startsWith("!list")) {
    // List reminders
  }
});

Now we will need to create a JSON file that will contain all the reminders. We will use the fs library for this.

const fs = require('fs');

let reminders = []

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    const args = msg.content.split(" ");
    const timestamp = args[2];
    const role = args[1];
    const reminder = {
      timestamp: timestamp,
      role: role
    };
    reminders.push(reminder);
    fs.writeFileSync('reminders.json', JSON.stringify(reminders));
  } else if (msg.content.startsWith("!list")) {
    // List reminders
  }
});

Now we will need to create a function that will check if any of the reminders have expired. We will use the setInterval function for this.


setInterval(() => {
  // Check if any reminders have expired
}, 1000);

We will need to loop through all the reminders in the JSON and check if any of them have expired. We will use the for loop for this.

Make sure the reminders.json file is in the same folder as the index.js file and looks like this:

{
  "Reminders": []
}

setInterval(() => {
    const reminders = require('./reminders.json');
    const now = new Date().getTime();
    for (const reminder of reminders.Reminders) {
        if (reminder.timestamp < Date.now()) {
        // Reminder has expired
        }
    }
}, 1000);

Now we will need to remove the expired reminder from the JSON file. We will use the splice function for this.


setInterval(() => {
    const reminders = require('./reminders.json');
    const now = new Date().getTime();
    for (const reminder of reminders.Reminders) {
        if (reminder.timestamp < Date.now()) {
        reminders.Reminders.splice(reminders.Reminders.indexOf(reminder), 1);
        }
    }
}, 1000);

Now we will need to send a message to the channel with the reminder. We will use the channel.send function for this with the channel id where we want to send the message.


setInterval(() => {
    const reminders = require('./reminders.json');
    const reminderchannel = "channelid";
    const now = new Date().getTime();
    for (const reminder of reminders.Reminders) {
        if (reminder.timestamp < Date.now()) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 5 minutes!`);
            reminders.Reminders.splice(reminders.Reminders.indexOf(reminder), 1);
        }
    }
}, 1000);

Ok now we want to remind the people 5 minutes, 15 minutes, 30 minutes, 1 hour and a day before the meeting. We can do that by adding boolean variables to the reminder object.


let reminders = []

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    const args = msg.content.split(" ");
    const timestamp = Date.now();
    const role = args[1];
    const reminder = {
      timestamp: timestamp,
      role: role,
      five: false,
      fifteen: false,
      thirty: false,
      hour: false,
      day: false
    };
    reminders.push(reminder);
    fs.writeFileSync('reminders.json', JSON.stringify(reminders));
  } else if (msg.content.startsWith("!list")) {
    // List reminders
  }
});

Now we will need to check if we need to send a reminder and set the boolean variable to true to also avoid sending the same reminder multiple times.

And it should look like this:


setInterval(() => {
    const reminders = require('./reminders.json');
    const reminderchannel = "channelid";
    const now = new Date().getTime();
    for (const reminder of reminders.Reminders) {
        if (reminderDate < now && !reminder.reminders[0].now) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting is Starting Now!`);
            reminder.reminders[0].now = true;
            // Remove the reminder from the list
            const index = reminders.Reminders.indexOf(reminder);
            if (index > -1) {
                reminders.Reminders.splice(index, 1);
            }
        }
        // Check for reminders happening in 5 minutes
        else if (reminderDate < now + 300000 && !reminder.reminders[0].five) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 5 minutes!`);
            reminder.reminders[0].five = true;

            // Set all others to true so they don't get sent
            reminder.reminders[0].fifteen = true;
            reminder.reminders[0].thirty = true;
            reminder.reminders[0].hour = true;
            reminder.reminders[0].day = true;
        }
        // Check for reminders happening in 15 minutes
        else if (reminderDate < now + 900000 && !reminder.reminders[0].fifteen) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 15 minutes!`);
            reminder.reminders[0].fifteen = true;

            // Set all others to true so they don't get sent
            reminder.reminders[0].thirty = true;
            reminder.reminders[0].hour = true;
            reminder.reminders[0].day = true;

        }

        // Check for reminders happening in 30 minutes
        else if (reminderDate < now + 1800000 && !reminder.reminders[0].thirty) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 30 minutes!`);
            reminder.reminders[0].thirty = true;

            // Set all others to true so they don't get sent
            reminder.reminders[0].hour = true;
            reminder.reminders[0].day = true;
        }

        // Check for reminders happening in an hour
        else if (reminderDate < now + 3600000 && !reminder.reminders[0].hour) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 1 hour!`);
            reminder.reminders[0].hour = true;

            // Set all others to true so they don't get sent
            reminder.reminders[0].day = true;
        }

        // Check for reminders happening in a day
        else if (reminderDate < now + 86400000 && !reminder.reminders[0].day) {
            client.channels.cache.get(reminderchannel).send(`<@&${reminder.roleid}> Meeting in 1 day!`);
            reminder.reminders[0].day = true;
        }
    }
    fs.writeFileSync('./reminders.json', JSON.stringify(reminders, null, 2)); // Write the updated reminders to the file 
}, 1000);

To show the reminders we can use the !list command. We will need to loop through the reminders and send a message to the channel with the reminders.

client.on("message", (msg) => {
  if (msg.content.startsWith("!remind")) {
    const args = msg.content.split(" ");
    const timestamp = args[2];
    const role = args[1];
    const reminder = {
      timestamp: timestamp,
      role: role,
      reminders: [
        {
          now: false,
          five: false,
          fifteen: false,
          thirty: false,
          hour: false,
          day: false
        }
      ]
    };
    reminders.push(reminder);
    fs.writeFileSync('reminders.json', JSON.stringify(reminders));
  } else if (msg.content.startsWith("!list")) {
    const reminders = require('./reminders.json');
    let message = "Reminders:\n";
    for (const reminder of reminders.Reminders) {
      message += `<@&${reminder.roleid}> at ${reminder.timestamp}\n`;
    }
    msg.channel.send(message);
  }
});

Now we can test it out by running the bot with node index.js and then running the command !remind @role 1677959779 and it should send a message to the channel.

This was an interesting project to work on as I have never worked with dates and timestamps before.

I hope you enjoyed this post and if you want to see the code for the bot you can find it here Discord-Reminder-Bot This version uses Discord’s new slash commands and buttons and is a bit more advanced than the one in this post.

Thanks so much for reading :D