Simple Robot v4.7.0 Help

走马观花

构建Application

val application = launchSimpleApplication { // 配置... }
var applicationAsync = Applications.launchApplicationAsync( Simple.INSTANCE, appConfigurer -> { // 配置 ... }); // 异步结果可转化为 CompletableFuture var future = applicationAsync.asFuture(); // ...
var application = Applications.launchApplicationBlocking( Simple.INSTANCE, appConfigurer -> { // 配置 ... }); // ...
var applicationAsync = Applications.launchApplicationAsync( Simple.INSTANCE, appConfigurer -> { // 配置 ... }); // 异步结果可转化为 CompletableFuture, // 进而转化为响应式类型 var future = applicationAsync.asFuture(); var mono = Mono.fromFuture(future); // ...

在Spring中,你不需要关系 Application 的构建。

@SpringBootApplication @EnableSimbot // Enable simbot class Main fun main(args: Array<String>) { runApplication<Main>(*args) }
@SpringBootApplication @EnableSimbot // Enable simbot public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } }

事件监听

任意事件

application.listeners { register { // this: EventListenerContext println("收到事件: $event") EventResult.of(42) } // 不需要返回 EventResult 的扩展函数 process { // this: EventListenerContext println("收到事件: $event") } }
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.async(context -> { System.out.println("收到事件: " + context.getEvent()); return CompletableFuture.completedFuture( EventResult.of(42) ); }) );
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.block(context -> { System.out.println("收到事件: " + context.getEvent()); return EventResult.of(42); }) );
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.nonBlock(context -> { System.out.println("收到事件: " + context.getEvent()); // EventResult.of 包裹任意响应式或可收集类型, // 也支持 CompletableFuture return EventResult.of(Mono.just("Value")); }) );
@Component open class MyHandles { @Listener suspend fun onEvent(event: Event) { println("收到事件: $event") } }
@Component public class MyHandles { @Listener public void onEvent(Event event) { System.out.println("收到事件: " + event) } }
@Component public class MyHandles { @Listener public CompletableFuture<?> onEvent(Event event) { System.out.println("收到事件: " + event) return CompletableFuture.completedFuture(42); } }
@Component public class MyHandles { @Listener public Mono<?> onEvent(Event event) { System.out.println("收到事件: " + event) return Mono.just(42); } }

指定类型事件

这里以 ChatGroupMessageEvent (聊天群消息事件) 为例。

application.listeners { listen<ChatGroupMessageEvent> { event -> // this: EventListenerContext println("收到事件: $event") EventResult.empty() } // 不需要返回 EventResult 的扩展函数 process<ChatGroupMessageEvent> { event -> // this: EventListenerContext println("收到事件: $event") } }
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.async( ChatGroupMessageEvent.class, (context, event) -> { System.out.println("收到事件: " + event); return CompletableFuture.completedFuture( EventResult.of(42) ); } ) );
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.block( ChatGroupMessageEvent.class, (context, event) -> { System.out.println("收到事件: " + context.getEvent()); return EventResult.of(42); } ) );
var eventListenerRegistrar = application.getEventDispatcher(); eventListenerRegistrar.register( EventListeners.nonBlock( ChatGroupMessageEvent.class, (context, event) -> { System.out.println("收到事件: " + context.getEvent()); // EventResult.of 包裹任意响应式或可收集类型, // 也支持 CompletableFuture return EventResult.of(Mono.just(42)); } ) );
@Component class MyHandles1 { @Listener suspend fun onEvent(event: ChatGroupMessageEvent) { println("收到事件: $event") } }
@Component public class MyHandles { @Listener public void onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) } }
@Component public class MyHandles { @Listener public CompletableFuture<?> onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) return CompletableFuture.completedFuture(42); } }
@Component public class MyHandles { @Listener public Mono<?> onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) return Mono.just(42); } }

注解式条件过滤

@Component class MyHandles1 { @Listener @Filter("你好") // 匹配消息中的纯文本内容 suspend fun onEvent(event: ChatGroupMessageEvent) { println("收到事件: $event") } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public CompletableFuture<?> onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) return CompletableFuture.completedFuture(42); } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public void onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public Mono<?> onEvent(ChatGroupMessageEvent event) { System.out.println("收到事件: " + event) return Mono.just(42); } }

信息获取

@Component class MyHandles1 { @Listener @Filter("你好") suspend fun onEvent(event: ChatGroupMessageEvent) { println(event.id) println(event.time) // 消息内容,消息相关的事件中会有 println(event.messageContent) // 消息内容中的纯文本内容 println(event.messageContent.plainText) // 消息内容中的消息链 println(event.messageContent.messages) // 事件主体,这里是‘聊天群消息事件’中的‘聊天群’ val group = event.content() println(group.id) println(group.name) // ... } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public void onEvent(ChatGroupMessageEvent event) { System.out.println(event.getId()); System.out.println(event.getTime()); // 消息内容,消息相关的事件中会有 var messageContent = event.getMessageContent(); System.out.println(messageContent); // 消息内容中的纯文本内容 System.out.println(messageContent.getPlainText()); // 消息内容中的消息链 System.out.println(messageContent.getMessages()); // 事件主体,这里是‘聊天群消息事件’中的‘聊天群’ event.getContentAsync().thenAccept(group -> { System.out.println(group.getId()); System.out.println(group.getName()); }); // 异步处理倒也并非必须返回Future // 如果上述逻辑都并非阻塞,也不打算让后续处理函数等待它们完成 // 那么就可以不用返回 } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public void onEvent(ChatGroupMessageEvent event) { System.out.println(event.getId()); System.out.println(event.getTime()); // 消息内容,消息相关的事件中会有 var messageContent = event.getMessageContent(); System.out.println(messageContent); // 消息内容中的纯文本内容 System.out.println(messageContent.getPlainText()); // 消息内容中的消息链 System.out.println(messageContent.getMessages()); // 事件主体,这里是‘聊天群消息事件’中的‘聊天群’ var group = event.getContent(); System.out.println(group.getId()); System.out.println(group.getName()); } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public void onEvent(ChatGroupMessageEvent event) { System.out.println(event.getId()); System.out.println(event.getTime()); // 消息内容,消息相关的事件中会有 var messageContent = event.getMessageContent(); System.out.println(messageContent); // 消息内容中的纯文本内容 System.out.println(messageContent.getPlainText()); // 消息内容中的消息链 System.out.println(messageContent.getMessages()); // 事件主体,这里是‘聊天群消息事件’中的‘聊天群’ event.getContentReserve() .transform(SuspendReserves.mono()) // 异步中处理 .subscribe(group -> { System.out.println(group.getId()); System.out.println(group.getName()); }); // 响应式处理倒也并非必须返回响应式或可收集结果 // 如果上述逻辑都并非阻塞,也不打算让后续处理函数等待它们完成 // 那么就可以不用返回 } }

回复/发送消息

@Component class MyHandles1 { @Listener @Filter("你好") suspend fun onEvent(event: ChatGroupMessageEvent) { // 回复"你也好"成功后, // 再向群内直接发送"你好呀" event.reply("你也好") event.content().send("你好呀") } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public CompletableFuture<?> onEvent(ChatGroupMessageEvent event) { // 回复"你也好"成功后, // 再向群内直接发送"你好呀" return event.replyAsync("你也好") .thenCompose(r -> event.getContentAsync()) .thenCompose(group -> group.sendAsync("你好呀")); } }
@Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public void onEvent(ChatGroupMessageEvent event) { // 回复"你也好"成功后, // 再向群内直接发送"你好呀" event.replyBlocking("你也好"); event.getContent().sendBlocking("你好呀"); } }
import static love.forte.simbot.suspendrunner.reserve.SuspendReserves.mono; @Component public class MyHandles { @Listener @Filter("你好") // 匹配消息中的纯文本内容 public Mono<Void> onEvent(ChatGroupMessageEvent event) { // 回复"你也好"成功后, // 再向群内直接发送"你好呀" return event.replyReserve("你也好") .transform(mono()) .then(event.getContentReserve() .transform(mono()) .flatMap(group -> group.sendReserve("你好呀") .transform(mono()) ) ).then(); } }

组件协同

安装多个组件,并监听它们的事件。

这里以 QGGuildEvent (QQ频道事件) 和 OneBotEvent (OneBot协议事件) 为例。

@Component class MyHandles1 { @Listener suspend fun onQGEvent(event: QGGuildEvent) { println("QQ频道: $event"); } @Listener suspend fun onOBEvent(event: OneBotEvent) { println("OB协议: $event"); } }
@Component public class MyHandles { @Listener public void onQGEvent(QGGuildEvent event) { System.out.println("QQ频道: " + event); } @Listener public void onOBEvent(OneBotEvent event) { System.out.println("OB协议: " + event); } }
Last modified: 15 November 2024