Search code examples
javascriptdiscorddiscord.js

discord.js run a message once a day but in a random time


I'm doing an event on my Discord server, and I need this to automatically run once a day, but at a random time. Can you guys give me an explanation or example of how I do it?

I have already seen node-cron, but apparently they just run at specific moments of the day.


Solution

  • I think the following would be a good idea to run once a day at a random time.

    const { setTimeout } = require("timers/promises");
    cron.schedule("0 0 * * *", async () => {
      await setTimeout(Math.floor(Math.random() * 86400000 /* 1 day mill-seconds */));
      // do stuff
    });