0
votes

I have made a Discord bot and I want it to be able to check the current date, and if current date is any of the users birthday, then it'll tag them and say happy birthday. I have tried using Node.js' Date, like

if(Date === "this or that")

but that didn't seem to work. So what I need help with is:

  • Checking the current date (like run a check each day or something)
  • send a message at that current date

I'll probably just hardcode all the birthdays in since we're not that many (I know that's not good practice, but I just want it working for now, I can polish it later)

If anyone could help me with that, that'd be great.

This is what my index.js file looks like (if that is relevant):

const discord = require("discord.js");
const config = require("./config.json");
const client = new discord.Client();
const prefix = '>';
const fs = require('fs');
const { time, debug } = require("console");
client.commands = new discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles)
{
    const command = require(`./commands/${file}`)
    client.commands.set(command.name, command);
}

client.once('ready', () => 
{
    console.log(`${client.user.username} is online`);
})

client.on('message', message =>
{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ + /);
    const command = args.shift().toLowerCase();

    if(command === 'ping')
    {
        client.commands.get('ping').execute(message, args);
    }else if(command === 'wololo')
    {
        client.commands.get('wololo').execute(message, args);        
    }
})

client.login('TOKEN')

Edit:

I realise I wrote the "if(date)" statement in "client.on('message',message =>{})", so that obviously wouldn't check unless you wrote a command at that specific time. What function would I use?

1

1 Answers

1
votes

You can check the full list of Discord.js events here.
There are no built in events in Discord.js that are fired based on scheduled time.

In order to do that, you could build a time system that fires events every single day based on a timeout out or interval. Or... you could use a library, like node-cron that do this for you.

I'll try to exemplify a way you could do this using the just said node-cron.

First, you'll have to require the node-cron package, thus you can use npm just as you did with discord.js itself. So paste and execute this command on your terminal:

npm install node-cron

Unfortunately, Discord.js doesn't offer a way for you to get the birthday date of the user. So, hard coding the birthdays (as you stated that it's okay) and also, for this example, the general channel Id that you'll send the message of the congratulations, you could something like this:

const Discord = require('discord.js');
const cron = require('node-cron');

const client = new Discord.Client();

// Hard Coded Storing Birthdays in a Map
// With the key beeing the user ID
// And the value a simplified date object
const birthdays = new Map();
birthdays.set('User 1 Birthday Id', { month: 'Jan', day: 26 });
birthdays.set('User 2 Birthday Id', { month: 'Jun', day: 13 });
birthdays.set('User 3 Birthday Id', { month: 'Aug', day: 17 });

const setSchedules = () => {
  // Fetch the general channel that you'll send the birthday message
  const general = client.channels.cache.get('Your General Channels Id Go Here');

  // For every birthday
  birthdays.forEach((birthday, userId) => {
    // Define the user object of the user Id
    const user = client.users.cache.get(userId);
    
    // Create a cron schedule
    cron.schedule(`* * ${birthday.day} ${birthday.month} *`, () => {
      general.send(`Today's ${user.toString()} birthday, congratulations!`);
    });
  });
};

client.on('ready', setSchedules);
client.login('Your Token Goes Here');

This is not optimized an intends to just give a general idea of how you can do it. Notice though that the client should be instantiated before you set the tasks, so I'd recommend doing it like this example and setting the schedule on ready.

Anyway, you could always use an already created and tested bot such as Birthday Bot.