Simple Robot | 米游社大别野组件 v0.2.0 Help

使用 Spring Boot

安装

首先你需要选择使用一个 simboot-core-spring-boot-starter 的 3.x 版本(下文以 simbot_version 代表之)。

implementation("love.forte.simbot.boot:simboot-core-spring-boot-starter:$simbot_version")
implementation 'love.forte.simbot.boot:simboot-core-spring-boot-starter:$simbot_version'
<dependency> <groupId>love.forte.simbot.boot</groupId> <!-- Maven 需要添加 `-jvm` 后缀来选择使用 JVM 平台 --> <artifactId>simboot-core-spring-boot-starter</artifactId> <version>${simbot_version}</version> </dependency>

然后安装大别野组件的core模块依赖。

implementation("love.forte.simbot.component:simbot-component-miyoushe-villa-core:0.2.0")
implementation 'love.forte.simbot.component:simbot-component-miyoushe-villa-core:0.2.0'
<dependency> <groupId>love.forte.simbot.component</groupId> <!-- Maven 需要添加 `-jvm` 后缀来选择使用 JVM 平台 --> <artifactId>simbot-component-miyoushe-villa-core</artifactId> <version>0.2.0</version> </dependency>

然后选择一个合适的 Ktor 引擎。

Ktor引擎

你可以前往 Ktor文档 处选择一个对应所用平台下合适的 Client Engine。 这里会根据不同平台提供几个示例,你可以选择其他可用目标。


CIO 是一个比较通用的引擎。 在不知道选什么的情况下,可以考虑使用它。

runtimeOnly("io.ktor:ktor-client-cio-jvm:$ktor_version")
runtimeOnly 'io.ktor:ktor-client-cio-jvm:$ktor_version'
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-cio-jvm</artifactId> <version>${ktor_version}</version> <scope>runtime</scope> </dependency>

如果你打算使用 Java11+,也可以选择 Java 引擎。


runtimeOnly("io.ktor:ktor-client-java:$ktor_version")

runtimeOnly 'io.ktor:ktor-client-java:$ktor_version'

<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-java-jvm</artifactId> <version>${ktor_version}</version> <scope>runtime</scope> </dependency>


JavaScript 平台下可以选择 Js 引擎。

implementation("io.ktor:ktor-client-js:$ktor_version")
implementation 'io.ktor:ktor-client-js:$ktor_version'

native 平台目标下,可能需要根据不同的平台类型选择不同的引擎。


可以选择 WinHttp 引擎。

implementation("io.ktor:ktor-client-winhttp:$ktor_version")
implementation 'io.ktor:ktor-client-winhttp:$ktor_version'

Linux 下依旧可以选择 CIO 引擎。

implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation 'io.ktor:ktor-client-cio:$ktor_version'

可以选择 Darwin 引擎。

implementation("io.ktor:ktor-client-darwin:$ktor_version")
implementation 'io.ktor:ktor-client-darwin:$ktor_version'

BOT配置

接下来,在项目资源文件目录下的 simbot-bots 文件夹中创建一个用于配置bot的配置文件 xxx.bot.json (文件名随意,扩展名应为 .bot.bot.json), 而配置文件的内容则参考章节 BOT配置文件

${PROJECT_SRC}/main/resources/simbot-bots/xxx.bot.json

启动类

像每一个 Spring Boot 应用一样,你需要一个启动类,并通过标注 @EnableSimbot 来启用 simbot :

@EnableSimbot @SpringBootApplication class App fun main(vararg args: String) { runApplication<App>(args = args) }
@EnableSimbot @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }

监听事件

接下来就是逻辑代码所在的地方了,编写一个监听函数并监听一个事件。

此处我们监听 ChannelMessageEvent ,也就是 子频道的消息事件

假设:要求 bot 必须 被AT ,并且说一句 你好 ,此时 bot 会引用用户发送的消息并回复 你也好! ,类似于:

用户:

@BOT 你好

BOT:

> 用户: @BOT 你好

你也好!

import love.forte.simboot.annotation.ContentTrim import love.forte.simboot.annotation.Filter import love.forte.simboot.annotation.Listener import love.forte.simbot.event.ChannelMessageEvent @Component class ExampleListener { @Listener @Filter(value = "你好", targets = Filter.Targets(atBot = true)) @ContentTrim // 当匹配被at时,将'at'这个特殊消息移除后,剩余的文本消息大概率存在前后空格,通过此注解在匹配的时候忽略前后空格 suspend fun onChannelMessage(event: ChannelMessageEvent) { // 将要监听的事件类型放在参数里,即代表监听此类型的消息 event.reply("你也好!") } }
import love.forte.simboot.annotation.ContentTrim import love.forte.simboot.annotation.Filter import love.forte.simboot.annotation.Listener import love.forte.simbot.event.ChannelMessageEvent @Component public class ExampleListener { @Listener @Filter(value = "你好", targets = @Filter.Targets(atBot = true)) @ContentTrim // 当匹配被at时,将'at'这个特殊消息移除后,剩余的文本消息大概率存在前后空格,通过此注解在匹配的时候忽略前后空格 public void onChannelMessage(ChannelMessageEvent event) { // 将要监听的事件类型放在参数里,即代表监听此类型的消息 // Java中的阻塞式API event.replyBlocking("你也好!"); } }
import love.forte.simboot.annotation.ContentTrim import love.forte.simboot.annotation.Filter import love.forte.simboot.annotation.Listener import love.forte.simbot.event.ChannelMessageEvent @Component public class ExampleListener { @Listener @Filter(value = "你好", targets = @Filter.Targets(atBot = true)) @ContentTrim // 当匹配被at时,将'at'这个特殊消息移除后,剩余的文本消息大概率存在前后空格,通过此注解在匹配的时候忽略前后空格 public CompletableFuture<?> onChannelMessage(ChannelMessageEvent event) { // 将要监听的事件类型放在参数里,即代表监听此类型的消息 // 将 CompletableFuture 作为返回值,simbot会以非阻塞的形式处理它 return event.replyAsync("你也好!"); } }
import love.forte.simboot.annotation.ContentTrim import love.forte.simboot.annotation.Filter import love.forte.simboot.annotation.Listener import love.forte.simbot.event.ChannelMessageEvent @Component public class ExampleListener { @Listener @Filter(value = "你好", targets = @Filter.Targets(atBot = true)) @ContentTrim // 当匹配被at时,将'at'这个特殊消息移除后,剩余的文本消息大概率存在前后空格,通过此注解在匹配的时候忽略前后空格 public Mono<?> onChannelMessage(ChannelMessageEvent event) { // 将要监听的事件类型放在参数里,即代表监听此类型的消息 // 将 Mono 等响应式类型作为返回值,simbot会以非阻塞的形式处理它 return Mono.fromCompletionStage(event.replyAsync("你也好!")); } }

启动

接下来,启动程序并在你的测试别野中@它试试看吧。

当然,如果遇到了预期外的问题也不要慌,积极反馈问题才能使我们变得更好,可以前往 Issues 反馈问题或在 社区 提出疑问。

Last modified: 05 January 2024