Skip to content

5. The events

RedsTom edited this page May 15, 2021 · 2 revisions

Register the events.

With BotServer API, you can register events as fast as possible.

You just have to create a class, and add the @EventBus and @SelfRegisteringListener as a class annotation. The constructor will be auto-called by the injector, so you can use the @Inject annotation into it.

The events provided by BotServer are located into the org.redstom.botapi.events.types package. The events provided by Javacord are listed on the documentation of Javacord.

If you need your own variables in this class, and so, use it in the class constructor, you can manually register the class as an event bus by removing the @SelfRegisteringListener annotation, and calling the IEventManager#register(Object). The instance of IEventManager is provided in the server injected variable (refer to the Auto-Injector wiki page).

Using the self registering listener :

@EventBus
@SelfRegisteringListener
public class Listener {

    @EventReceiver(MessageCreateEvent.class)
    public void onMessageCreate(MessageCreateEvent event, @Inject("server") IServer server) {
        server.getLogger().info("Message received");
    }

}

By registering the listener manually :

@BotPlugin(
  id = "MyAddonId",
  name = "My Addon",
  author = "Me"
)
public class Main {
    public void load(@Inject("server") IServer server) {
        server.getEventManager().register(new Listener());
    }

    public void unload() {
    }
}

@EventBus
class Listener {

    @EventReceiver(MessageCreateEvent.class)
    public void onMessageCreate(MessageCreateEvent event, @Inject("server") IServer server) {
        server.getLogger().info("Message received");
    }

}
Clone this wiki locally