Service

Description

Stateful business logic living outside of Middleware

Example

class IncrementService extends Service {
  serviceDidInitialize() {
    this.counter = 0
    this.increment = () => this.counter++
  }
}

const command = new CommandBuilder()
  .match(matchPrefixes("increment"))
  .use((context) => {
    const { manager } = context

    const service = manager.getService(IncrementService)
    service.increment()

    console.log("The counter is now", service.counter)
  })
  .done()

const bot = new Bot({
  services: [IncrementService],
  commands: [command],
})

// Input: "increment"
// Output: "The counter is now 1"

// Input: "increment"
// Output: "The counter is now 2"

Generics

M
Message
C
Client

Constructor

warning

Do not manually instantiate a service. Services are automatically instantiated by the ServiceManager
new Service(options, s)

Parameters

options:
ServiceOptions<M, C>

Properties

bot:
Bot<M, C>
manager:
ServiceManager<M, C>

Methods

serviceDidInitialize()Promise<void> | void

Hook called when the service has initialized, but the bot is not ready yet

serviceDidRestart()Promise<void> | void

Hook called when the service has started after stopping

serviceDidStart()Promise<void> | void

Hook called when the service has started and the bot is ready

serviceDidStop()Promise<void> | void

Hook called when the service has stopped and the bot has stopped