Events
Twitch4J uses Events4J to dispatch events and handle them, you can also build your own event handler to handle them however you like. You can use those events to build chat bots or similar tools.
Publish Event
Publish a event that should be processed somewhere:
- The event object needs to implement the IEvent interface.
- The event will be passed to all registered handlers.
- Java
- Kotlin
- Groovy
eventManager.publish(object);
eventManager.publish(`object`)
eventManager.publish(object)
Handle / Process Events
Twitch4J uses the SimpleHandler
by default which works on all platforms.
Handlers:
- SimpleHandler
- ReactorHandler
- SpringHandler
You can register listeners directly on the eventManager, the call is forwarded and registered on the current defaultEventHandler
set in the eventManager instance.
Switch the default event handler
If you want all your events to be processed by a specific eventHandler, then check out the following pages on all available eventHandlers:
Register your event listeners in a generic way
Only the consumers registered with eventManager.onEvent
will use the defaultEventHandler
.
- Java
- Kotlin
- Groovy
// register handler
IDisposable handlerReg = twitchClient.getEventManager().onEvent(ChannelMessageEvent.class, event -> {
System.out.println "[" + event.getChannel().getName() + "]["+event.getPermissions().toString()+"] " + event.getUser().getName() + ": " + event.getMessage());
});
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose();
// register handler
val handlerReg = twitchClient.eventManager.onEvent(ChannelMessageEvent::class.java) { event ->
println("[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message}")
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose()
// register handler
def handlerReg = twitchClient.eventManager.onEvent(ChannelMessageEvent) { event ->
System.out.println "[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message}"
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose()
This is the recommended method to register listeners, as you can switch between the different EventHandlers by chaning a single line of code.
Simple Handler
The default event handler can register handlers
with annotations or provide lambdas as consumers.
Lambda / Consumer Example
- Java
- Kotlin
- Groovy
// register handler
IDisposable handlerReg = twitchClient.getEventManager().getEventHandler(SimpleEventHandler.class).onEvent(ChannelMessageEvent.class, event -> {
System.out.println("[" + event.getChannel().getName() + "]["+event.getPermissions().toString()+"] " + event.getUser().getName() + ": " + event.getMessage());
});
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose();
// register handler
val handlerReg = twitchClient.eventManager.getEventHandler(SimpleEventHandler::class.java).onEvent(ChannelMessageEvent::class.java) { event ->
println("[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message}")
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose()
// register handler
def handlerReg = twitchClient.eventManager.getEventHandler(SimpleEventHandler).onEvent ChannelMessageEvent, { event ->
System.out.println "[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message} "
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose()
Annotation Example
- Java
- Kotlin
- Groovy
public class MyEventHandler {
// the type of the 1st argument is relevant, you can pick any method name you want
@EventSubscriber
public void printChannelMessage(ChannelMessageEvent event) {
System.out.println("[" + event.getChannel().getName() + "]["+event.getPermissions().toString()+"] " + event.getUser().getName() + ": " + event.getMessage());
}
}
// register your handler class
MyEventHandler myEventHandler = new MyEventHandler();
eventManager.getEventHandler(SimpleEventHandler.class).registerListener(myEventHandler);
class MyEventHandler {
// the type of the 1st argument is relevant, you can pick any method name you want
@EventSubscriber
fun printChannelMessage(ChannelMessageEvent event) {
println("[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message}")
}
}
// register your handler class
val myEventHandler = MyEventHandler();
eventManager.getEventHandler(SimpleEventHandler::class.java).registerListener(myEventHandler);
class MyEventHandler {
// the type of the 1st argument is relevant, you can pick any method name you want
@EventSubscriber
def printChannelMessage(ChannelMessageEvent event) {
System.out.println "[${event.channel.name}][${event.permissions}] ${event.user.name}: ${event.message}"
}
}
// register your handler class
def myEventHandler = new MyEventHandler();
eventManager.getEventHandler(SimpleEventHandler).registerListener(myEventHandler);
Cheat Sheet
Key | Value |
---|---|
Handler Execution | sync |
Android | compatible |
Reactor Handler
If desired you can also use the ReactorHandler
to use reactive streams to process events (keep in mind that this will not work with android).
To enable reactor add the ReactorHandler
dependency, it will be discovered and registered automatically.
- Gradle
- Gradle - Kotlin DSL
- Maven
dependencies {
implementation 'com.github.philippheuer.events4j:events4j-handler-reactor:0.12.2'
}
dependencies {
implementation("com.github.philippheuer.events4j:events4j-handler-reactor:0.12.2")
}
<dependency>
<groupId>com.github.philippheuer.events4j</groupId>
<artifactId>events4j-handler-reactor</artifactId>
<version>0.12.2</version>
</dependency>
- Java
- Kotlin
- Groovy
// register handler
Disposable handlerReg = twitchClient.getEventManager().getEventHandler(ReactorEventHandler.class).onEvent(ChannelMessageEvent.class, event -> {
System.out.println("[" + event.getChannel().getName() + "]["+event.getPermissions().toString()+"] " + event.getUser().getName() + ": " + event.getMessage());
});
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose();
// register handler
val handlerReg = twitchClient.eventManager.getEventHandler(ReactorEventHandler::class.java).onEvent(ChannelMessageEvent::class.java) { event ->
println("[${event.channel.name}]${event.permissions}] ${event.user.name}: ${event.message}")
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose();
// register handler
def handlerReg = twitchClient.eventManager.getEventHandler(ReactorEventHandler).onEvent ChannelMessageEvent, { event ->
System.out.println "[${event.channel.name}]${event.permissions}] ${event.user.name}: ${event.message}"
}
// cancel handler (don't call the method for new events of the required type anymore)
handlerReg.dispose();
Cheat Sheet
Key | Value |
---|---|
Handler Execution | async |
Android | incompatible |
Spring Handler
If desired you can also use the SpringHandler
to handle all events as Spring Application Events.
To enable reactor add the SpringHandler
dependency, it will be discovered and registered automatically.
- Gradle
- Gradle - Kotlin DSL
- Maven
dependencies {
implementation 'com.github.philippheuer.events4j:events4j-handler-spring:0.12.2'
}
dependencies {
implementation("com.github.philippheuer.events4j:events4j-handler-spring:0.12.2")
}
<dependency>
<groupId>com.github.philippheuer.events4j</groupId>
<artifactId>events4j-handler-spring</artifactId>
<version>0.12.2</version>
</dependency>
Event Catalog
This will become a list of all available events.
Channel:
- ChannelMessageEvent
- ChannelChangeGameEvent
- ChannelChangeTitleEvent
- ChannelGoLiveEvent
- ChannelGoOfflineEvent
User:
Generic:
And many more, this is a very incomplete list
Please visit our javadocs to find a more complete list of events: