TheDEVIX
TheDEVIX

Reputation: 1

How to specify an interval of 6 hours in Date JavaScript. Discord.JS

I'm making a kind of custom calendar for my play server role, I managed to make it using Date JS, but now I need to make the bot send a message with a new month every 6 hours and not every second.

Code:

client.once(Events.ClientReady, () => {
    console.log('Ready!');
    const channel = client.channels.cache.find(channel => channel.id === '1067802071476215818');
    
    let date = new Date(2000, 60, 1);
    const options = { year: 'numeric', month: 'long', day: 'numeric' };
    for (i = 0; i < 10000; i++) {
        const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Календарь на сервере HoG World')
        .setDescription(date.toLocaleDateString('ru-RU', options));
        channel.send({ embeds: [exampleEmbed] });
      date.setMonth(date.getMonth() + 1);
    }

});

Upvotes: 0

Views: 69

Answers (2)

ncls.
ncls.

Reputation: 123

(I edited RareHyperion's answer to work with schedules)

First of all I suggest using the package node-schedule so install that.

Then you can do the following in your code:

const schedule = require('node-schedule');

client.once(Events.ClientReady, () => {
    console.log('Ready!');
    const channel = client.channels.cache.find(channel => channel.id === '1067802071476215818');
    
    let date = new Date(2000, 60, 1);
    const options = { year: 'numeric', month: 'long', day: 'numeric' };
    
    const sendMonth = () => {
        const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Календарь на сервере HoG World')
        .setDescription(date.toLocaleDateString('ru-RU', options));
        channel.send({ embeds: [exampleEmbed] });
        date.setMonth(date.getMonth() + 1);
    }
    
    // version without offset (posts at hour 0, 6, 12, 18)
    const job = schedule.scheduleJob('0 */6 * * *', sendMonth);

    // version with offset (posts at hour 3, 9, 15, 21)
    const job = schedule.scheduleJob('0 3-21/6 * * *', sendMonth);
});

I also suggest using Crontab.guru to experiment with cron schedule expressions and easily understand what a specific expression is doing.

Upvotes: 1

RareHyperIon
RareHyperIon

Reputation: 11

To send a message with a new month every 6 hours, you can use the setInterval method. Here is the updated code that should work:

client.once(Events.ClientReady, () => {
    console.log('Ready!');
    const channel = client.channels.cache.find(channel => channel.id === '1067802071476215818');
    
    let date = new Date(2000, 60, 1);
    const options = { year: 'numeric', month: 'long', day: 'numeric' };
    
    const sendMonth = () => {
        const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Календарь на сервере HoG World')
        .setDescription(date.toLocaleDateString('ru-RU', options));
        channel.send({ embeds: [exampleEmbed] });
        date.setMonth(date.getMonth() + 1);
    }
    
    sendMonth();
    setInterval(sendMonth, 6 * 60 * 60 * 1000); // Send a new month every 6 hours

});

I hope that helps! Let me know if you have any questions or need further assistance.

Upvotes: 0

Related Questions