meshcore.js
Guides

Run something on a schedule

A job on an interval or a cron expression, started with the bot.

Every six hours the bot floods an advert, so the mesh keeps a route to it. Every evening at eight it posts how many nodes it heard.

jobs/advert.ts
export default new JobBuilder()
  .setName('advert')
  .setInterval(6 * 3600)
  .setRunOnStart()
  .setHandler((client) => client.radio.sendSelfAdvert(true));
jobs/evening-report.ts
export const evening = new JobBuilder()
  .setName('evening-report')
  .setCron('0 20 * * *', { timezone: 'Europe/Paris' })
  .setHandler(async (client) => {
    const today = Date.now() - 86_400_000;
    const heard = client.contacts.cache.filter((contact) => contact.lastSeen.getTime() > today).size;
    await client.channels.get('#club')?.send(`📡 ${heard} nodes heard today`);
  });

What happens

A job takes either setInterval(seconds) or setCron(expression, { timezone }). Files under jobs/ are loaded at login. Jobs start when the bot becomes ready and stop with destroy().

setRunOnStart() also runs the job once at startup. A scheduled run is skipped while the radio is disconnected.

A handler that throws is reported through the error event and the job keeps its schedule. From a DM, /jobs run|pause|resume <name> controls it.

Go further

On this page