创建 src/main/kotlin/Main.kt:
import kotlinx.coroutines.flow.collect
import love.forte.simbot.application.listeners
import love.forte.simbot.core.application.launchSimpleApplication
import love.forte.simbot.event.process
import love.forte.simbot.test.event.TestEvent
suspend fun main() {
val app = launchSimpleApplication()
try {
// 注册一个处理 TestEvent 的监听器
app.listeners {
process<TestEvent> {
println("收到测试事件")
}
}
// 推送测试事件并收集处理结果
app.eventDispatcher.push(TestEvent()).collect()
} finally {
app.cancel()
}
}
创建 src/main/java/Main.java:
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventProcessors;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.test.event.TestEvent;
public class Main {
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE);
try {
app.getEventDispatcher().register(EventListeners.block(TestEvent.class, (context, event) -> {
System.out.println("收到测试事件");
return EventResult.empty();
}));
EventProcessors.pushAndCollectToListBlocking(app.getEventDispatcher(), new TestEvent());
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventProcessors;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.test.event.TestEvent;
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE);
try {
app.getEventDispatcher().register(EventListeners.async(TestEvent.class, (context, event) -> {
System.out.println("收到测试事件");
return CompletableFuture.completedFuture(EventResult.empty());
}));
EventProcessors.pushAndCollectToListAsync(app.getEventDispatcher(), new TestEvent(), app).join();
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventProcessors;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.test.event.TestEvent;
public class Main {
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE);
try {
app.getEventDispatcher().register(EventListeners.nonBlock(TestEvent.class, (context, event) -> {
System.out.println("收到测试事件");
return EventResult.empty();
}));
EventProcessors.pushAndAsFlux(app.getEventDispatcher(), new TestEvent()).blockLast();
} finally {
app.cancel();
}
}
}