meshcore.js
Guides

React to the mesh

Run code when a node appears, an advert is heard, or the bot becomes ready.

A new contact appears in the radio's table. The bot sends it a welcome message.

events/welcome.ts
export default new EventBuilder().setEvent('contactAdd').setHandler(async (_client, contact) => {
  if (contact.type === 'chat') await contact.send('👋 Welcome to the club mesh. Send /help to see what I can do');
});
events/heard.ts
export const heard = new EventBuilder().setEvent('advert').setHandler((client, advert) => {
  client.logger.info(`heard ${advert.name}${advert.contact ? '' : ' (not in contacts)'}`);
});

What happens

Files under events/ are loaded at login. setEvent() picks one of the client's events. The handler's arguments are typed from it. A contactAdd handler receives a Contact, an advert handler an Advert.

Handlers of the same event run in registration order. One that throws is reported through the error event and does not stop the others.

setOnce() runs the handler a single time, for example on ready.

Go further

On this page