Creating a service

Services

To create a service, simply import the Service class from your binding library of choice, and define a class extending the service.

const { Service } = require("your-gears-binding-library")

class MyService extends Service {
  
}

Now you've defined a service called "MyService". In order to actually use the service, you'll need to add it to the bot, which can be done like so:

const bot = new Bot({
  adapter, services: [MyService]
})

warning

Do not instantiate the service using the "new" keyword when passing it to the bot, simply pass the class. This is because services are instantiated automatically by the bot's ServiceManager

Now you can access the service via the ServiceManager on Context within Middleware.

Service hooks

During a service's lifecycle, certain hooks are called depending on the state of the bot. These are necessary for things like timers and events.

Check out the Service class in the docs for a detailed list of each hook and what it does.
Let's use the initialization hook to set some initial state.

class MyService extends Service {
  serviceDidInitialize() {
    this.count = 0
  }
}

This will set a new property called "count" on the service right after calling .start() on the bot.

Introduction to services
Using the service