Creating Listeners - TheSourceCodeLLC/Source GitHub Wiki

Creating Listeners

Source v5 declares two EventSystems, one for events generated by modules, and one for events generated by JDA; These systems are visible through the source object in every SourceModule, source.sourceEventSystem and source.jdaEventSystem respectively.

  1. Create a class that will subscribe to events:
class MySubscriber(
  private val myModule: MyModule
) : EventSubscriber {
  override fun subscribe(
    jdaEvents: EventSystem<GenericEvent>,
    sourceEvents: EventSystem<SourceEvent>
  ) {

  }
}
  1. Create a function in the subscriber to listen to a specific event:
class MySubscriber(
  private val myModule: MyModule
) : EventSubscriber {
  override fun subscribe(
    jdaEvents: EventSystem<GenericEvent>,
    sourceEvents: EventSystem<SourceEvent>
  ) {

  }

  private fun onSomeEvent(event: ReadyEvent) {

  }
}
  1. Subscribe to the event using the relevant EventSystem:
class MySubscriber(
  private val myModule: MyModule
) : EventSubscriber {
  override fun subscribe(
    jdaEvents: EventSystem<GenericEvent>,
    sourceEvents: EventSystem<SourceEvent>
  ) {
    jdaEvents.listen(myModule, this::onSomeEvent)
  }

  private fun onSomeEvent(event: ReadyEvent) {

  }
}
  1. Register the EventSubscriber in your module:
class MyModule : SourceModule() {
  override fun onEnable() {
    subscribeEvents(
      MySubscriber(this)
    )
  }
}