Creating Listeners - TheSourceCodeLLC/Source GitHub Wiki
Creating Listeners
Source v5 declares two EventSystem
s, 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.
- Create a class that will subscribe to events:
class MySubscriber(
private val myModule: MyModule
) : EventSubscriber {
override fun subscribe(
jdaEvents: EventSystem<GenericEvent>,
sourceEvents: EventSystem<SourceEvent>
) {
}
}
- 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) {
}
}
- 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) {
}
}
- Register the
EventSubscriber
in your module:
class MyModule : SourceModule() {
override fun onEnable() {
subscribeEvents(
MySubscriber(this)
)
}
}