From 4c2931d04e88936bf5e9a29c6cf42f587c7a1bcf Mon Sep 17 00:00:00 2001 From: yyuueexxiinngg Date: Tue, 17 Nov 2020 22:09:55 +0800 Subject: [PATCH] Rearrange project, move embedded branch into master. --- .github/workflows/gradle-ci.yml | 9 +- README.md | 26 +++- build.gradle.kts | 142 ++---------------- buildSrc/build.gradle.kts | 7 + buildSrc/src/main/kotlin/Versions.kt | 39 +++++ onebot-kotlin/build.gradle.kts | 46 ++++++ .../com/github/yyuueexxiinngg/onebot/Main.kt | 102 +++++++++++++ onebot-mirai/build.gradle.kts | 107 +++++++++++++ .../github/yyuueexxiinngg/onebot}/MiraiApi.kt | 14 +- .../yyuueexxiinngg/onebot}/PluginBase.kt | 22 +-- .../yyuueexxiinngg/onebot}/PluginSettings.kt | 2 +- .../github/yyuueexxiinngg/onebot}/Session.kt | 13 +- .../onebot}/data/common/BotEventDTO.kt | 2 +- .../onebot}/data/common/ContactDTO.kt | 2 +- .../yyuueexxiinngg/onebot}/data/common/DTO.kt | 2 +- .../onebot}/data/common/MessageDTO.kt | 6 +- .../onebot}/data/common/ResponseDTO.kt | 5 +- .../onebot}/util/CQMessgeParser.kt | 12 +- .../onebot}/util/EventFilter.kt | 4 +- .../yyuueexxiinngg/onebot}/util/FaceMap.kt | 2 +- .../yyuueexxiinngg/onebot}/util/HttpClient.kt | 4 +- .../yyuueexxiinngg/onebot}/util/ImgUtil.kt | 2 +- .../yyuueexxiinngg/onebot}/util/Json.kt | 4 +- .../yyuueexxiinngg/onebot}/util/Logger.kt | 6 +- .../yyuueexxiinngg/onebot}/util/Music.kt | 2 +- .../yyuueexxiinngg/onebot}/util/PokeMap.kt | 2 +- .../onebot}/util/RichMessageHelper.kt | 2 +- .../onebot}/web/HeartbeatScope.kt | 4 +- .../onebot}/web/http/HttpApiModule.kt | 14 +- .../onebot}/web/http/HttpApiServer.kt | 6 +- .../onebot}/web/http/ReportService.kt | 24 +-- .../onebot}/web/queue/CacheRequestQueue.kt | 2 +- .../onebot}/web/queue/CacheSourceQueue.kt | 2 +- .../onebot}/web/websocket/WebsocketActions.kt | 12 +- .../web/websocket/WebsocketReverseClient.kt | 20 +-- .../onebot}/web/websocket/WebsocketServer.kt | 42 +++--- .../src}/test/kotlin/mirai/RunMirai.kt | 0 settings.gradle.kts | 4 +- 38 files changed, 459 insertions(+), 257 deletions(-) create mode 100644 buildSrc/build.gradle.kts create mode 100644 buildSrc/src/main/kotlin/Versions.kt create mode 100644 onebot-kotlin/build.gradle.kts create mode 100644 onebot-kotlin/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Main.kt create mode 100644 onebot-mirai/build.gradle.kts rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/MiraiApi.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/PluginBase.kt (93%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/PluginSettings.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/Session.kt (87%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/data/common/BotEventDTO.kt (99%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/data/common/ContactDTO.kt (97%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/data/common/DTO.kt (94%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/data/common/MessageDTO.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/data/common/ResponseDTO.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/CQMessgeParser.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/EventFilter.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/FaceMap.kt (95%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/HttpClient.kt (97%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/ImgUtil.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/Json.kt (94%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/Logger.kt (89%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/Music.kt (99%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/PokeMap.kt (94%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/util/RichMessageHelper.kt (98%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/HeartbeatScope.kt (82%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/http/HttpApiModule.kt (97%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/http/HttpApiServer.kt (86%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/http/ReportService.kt (91%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/queue/CacheRequestQueue.kt (94%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/queue/CacheSourceQueue.kt (94%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/websocket/WebsocketActions.kt (78%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/websocket/WebsocketReverseClient.kt (96%) rename {src/main/kotlin/tech/mihoyo/mirai => onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot}/web/websocket/WebsocketServer.kt (86%) rename {src => onebot-mirai/src}/test/kotlin/mirai/RunMirai.kt (100%) diff --git a/.github/workflows/gradle-ci.yml b/.github/workflows/gradle-ci.yml index 6c77351..7b13cfe 100644 --- a/.github/workflows/gradle-ci.yml +++ b/.github/workflows/gradle-ci.yml @@ -38,4 +38,11 @@ jobs: - name: Upload articact uses: actions/upload-artifact@v2 with: - path: build/libs + name: onebot-mirai + path: onebot-mirai/build/libs/onebot-mirai-*-all.jar + + - name: Upload articact + uses: actions/upload-artifact@v2 + with: + name: onebot-kotlin + path: onebot-kotlin/build/libs/onebot-kotlin-*-all.jar \ No newline at end of file diff --git a/README.md b/README.md index 6b5b706..672d598 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,31 @@ __[OneBot Standard](https://github.com/howmanybots/onebot) implementation for mirai__ -### 自0.2.0起添加临时[Embedded分支版本](https://github.com/yyuueexxiinngg/cqhttp-mirai/tree/embedded), 与主分支单插件版并行, 支持发送语音, 请看[此说明中第一部分](https://github.com/yyuueexxiinngg/cqhttp-mirai/tree/embedded)进行配置 - -## 开始使用 +### 开始使用 0. 请首先运行[mirai-console](https://github.com/mamoe/mirai-console)相关客户端生成plugins文件夹 1. 将`onebot-mirai`生成的`jar包文件`放入`plugins`文件夹中 2. 运行`mirai-console`, 将在`config/OneBot`文件夹中自动生成样本配置文件`settings.yml` 3. 编辑`config/OneBot/settings.yml`配置文件, 按照以下配置给出的注释修改保存 4. 再次启动[mirai-console](https://github.com/mamoe/mirai-console)相关客户端 +# OneBot Kotlin - CQHTTP Mirai Embedded + +### 开始使用 +1. 运行Jar包: `java -jar onebot-kotlin-**.jar` +2. 编辑`config/OneBot/settings.yml`配置文件, 将以下配置给出的注释修改保存 +3. 重新运行 + +### 接收的参数 + +- `--account 123456789` 要自动登录的账号 +- `--password *******` 要自动登录账号的密码 +- `--args -- --xx`传入参数至`mirai-console`, 如`--args -- --help`将`--help`传入获取`mirai-console`提供的帮助信息 + +#### 读取的环境变量 + +- `onebot.account` 同`--account`参数, 但优先级低, 会被参数覆盖 +- `onebot.password` 同`--password`参数, 但优先级低, 会被参数覆盖 + ## 配置相关 ```yaml @@ -104,14 +120,12 @@ bots: reconnectInterval: 3000 ``` -## 计划 - +#### 实现 - [x] 反向Websocket客户端 - [x] HTTP上报服务 - [x] Websocket服务端 - [x] HTTP API -#### 实现
已实现CQ码 diff --git a/build.gradle.kts b/build.gradle.kts index bb2a7dd..4ca918c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,143 +1,21 @@ plugins { + java kotlin("jvm") version "1.4.0" kotlin("plugin.serialization") version "1.4.0" kotlin("kapt") version "1.4.0" - java id("com.github.johnrengelman.shadow") version "6.0.0" id("com.github.gmazzo.buildconfig") version "2.0.2" } -val projectVersion = "0.3.0" -version = projectVersion -group = "yyuueexxiinngg" - -repositories { - maven(url = "https://mirrors.huaweicloud.com/repository/maven") - maven(url = "https://dl.bintray.com/kotlin/kotlin-eap") - gradlePluginPortal() - jcenter() - mavenCentral() -} - -val miraiCoreVersion = "1.3.3" -val miraiConsoleVersion = "1.0-RC-1" -val ktorVersion = "1.4.1" -val kotlinVersion = "1.4.0" -val kotlinSerializationVersion = "1.0.1" -val autoService = "1.0-rc7" - -fun ktor(id: String, version: String = this@Build_gradle.ktorVersion) = "io.ktor:ktor-$id:$version" -fun kotlinx(id: String, version: String) = "org.jetbrains.kotlinx:kotlinx-$id:$version" -fun String.runCommand(workingDir: File): String? { - return try { - val parts = this.split("\\s".toRegex()) - val proc = ProcessBuilder(*parts.toTypedArray()) - .directory(workingDir) - .redirectOutput(ProcessBuilder.Redirect.PIPE) - .redirectError(ProcessBuilder.Redirect.PIPE) - .start() - - proc.waitFor(60, TimeUnit.MINUTES) - proc.inputStream.bufferedReader().readText().trim() - } catch (e: java.io.IOException) { - e.printStackTrace() - null - } -} - -dependencies { - kapt("com.google.auto.service", "auto-service", autoService) - - compileOnly(kotlin("stdlib-jdk8")) - compileOnly("net.mamoe:mirai-core:$miraiCoreVersion") - compileOnly("net.mamoe:mirai-console:$miraiConsoleVersion") - compileOnly("net.mamoe:mirai-console-terminal:$miraiConsoleVersion") - compileOnly(kotlin("serialization", kotlinVersion)) - compileOnly("com.google.auto.service", "auto-service-annotations", autoService) - - implementation(kotlinx("serialization-cbor", kotlinSerializationVersion)) - implementation(kotlinx("serialization-json", kotlinSerializationVersion)) - implementation("ch.qos.logback:logback-classic:1.2.3") - implementation("com.google.code.gson:gson:2.8.6") - - api(ktor("server-cio")) - api(ktor("client-okhttp")) - api(ktor("websockets")) - api(ktor("client-websockets")) - api(kotlin("reflect", kotlinVersion)) - - testImplementation(kotlin("stdlib-jdk8")) - testImplementation("net.mamoe:mirai-core:$miraiCoreVersion") - testImplementation("net.mamoe:mirai-core-qqandroid:$miraiCoreVersion") - testImplementation("net.mamoe:mirai-console:$miraiConsoleVersion") - testImplementation("net.mamoe:mirai-console-terminal:$miraiConsoleVersion") -} - -java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 -} - -tasks { - compileKotlin { - kotlinOptions.jvmTarget = "1.8" - kotlinOptions.freeCompilerArgs = listOf("-Xjvm-default=all") - } - compileTestKotlin { - kotlinOptions.jvmTarget = "1.8" - kotlinOptions.freeCompilerArgs = listOf("-Xjvm-default=all") - } - - buildConfig { - val commitHash = "git rev-parse --short HEAD".runCommand(projectDir) - buildConfigField("String", "VERSION", "\"$projectVersion\"") - if (commitHash != null) { buildConfigField("String", "COMMIT_HASH", "\"$commitHash\"") } - } - - shadowJar { - dependsOn(generateBuildConfig) - } - - val runMiraiConsole by creating(JavaExec::class.java) { - group = "mirai" - main = "mirai.RunMirai" - dependsOn(shadowJar) - dependsOn(testClasses) - - val testConsoleDir = "test" - - doFirst { - fun removeOldVersions() { - File("$testConsoleDir/plugins/").walk() - .filter { it.name.matches(Regex("""${project.name}-.*-all.jar""")) } - .forEach { - it.delete() - println("deleting old files: ${it.name}") - } - } - - fun copyBuildOutput() { - File("build/libs/").walk() - .filter { it.name.contains("-all") } - .maxBy { it.lastModified() } - ?.let { - println("Coping ${it.name}") - it.inputStream() - .transferTo(File("$testConsoleDir/plugins/${it.name}").apply { createNewFile() } - .outputStream()) - println("Copied ${it.name}") - } - } - - workingDir = File(testConsoleDir) - workingDir.mkdir() - File(workingDir, "plugins").mkdir() - removeOldVersions() - copyBuildOutput() +allprojects { + group = "com.github.yyuueexxiinngg" + version = Versions.projectVersion - classpath = sourceSets["test"].runtimeClasspath - standardInput = System.`in` - args(miraiCoreVersion, miraiConsoleVersion) - } + repositories { + maven(url = "https://mirrors.huaweicloud.com/repository/maven") + maven(url = "https://dl.bintray.com/kotlin/kotlin-eap") + gradlePluginPortal() + jcenter() + mavenCentral() } } \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 0000000..c39a297 --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + `kotlin-dsl` +} + +repositories { + jcenter() +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt new file mode 100644 index 0000000..4e89c95 --- /dev/null +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -0,0 +1,39 @@ +import java.io.File +import java.util.concurrent.TimeUnit + +object Versions { + const val projectVersion = "0.3.0" + + const val miraiCoreVersion = "1.3.3" + const val miraiConsoleVersion = "1.0-RC-1" + + const val ktorVersion = "1.4.1" + const val kotlinVersion = "1.4.0" + const val kotlinSerializationVersion = "1.0.1" + const val autoService = "1.0-rc7" + const val logback = "1.2.3" + const val gson = "2.8.6" + + // OneBot Kotlin + const val clikt = "3.0.1" +} + +fun ktor(id: String, version: String = Versions.ktorVersion) = "io.ktor:ktor-$id:$version" +fun kotlinx(id: String, version: String) = "org.jetbrains.kotlinx:kotlinx-$id:$version" + +fun String.runCommand(workingDir: File): String? { + return try { + val parts = this.split("\\s".toRegex()) + val proc = ProcessBuilder(*parts.toTypedArray()) + .directory(workingDir) + .redirectOutput(ProcessBuilder.Redirect.PIPE) + .redirectError(ProcessBuilder.Redirect.PIPE) + .start() + + proc.waitFor(60, TimeUnit.MINUTES) + proc.inputStream.bufferedReader().readText().trim() + } catch (e: java.io.IOException) { + e.printStackTrace() + null + } +} \ No newline at end of file diff --git a/onebot-kotlin/build.gradle.kts b/onebot-kotlin/build.gradle.kts new file mode 100644 index 0000000..97d83e8 --- /dev/null +++ b/onebot-kotlin/build.gradle.kts @@ -0,0 +1,46 @@ +plugins { + java + kotlin("jvm") + id("com.github.johnrengelman.shadow") +} + +dependencies { + implementation(kotlin("stdlib-jdk8")) + implementation("net.mamoe:mirai-core:${Versions.miraiCoreVersion}") + implementation("net.mamoe:mirai-core-qqandroid:${Versions.miraiCoreVersion}") + implementation("net.mamoe:mirai-console:${Versions.miraiConsoleVersion}") + implementation("net.mamoe:mirai-console-terminal:${Versions.miraiConsoleVersion}") + implementation("com.github.ajalt.clikt:clikt:${Versions.clikt}") + + implementation(project(":onebot-mirai")) +} + +val jar by tasks.getting(Jar::class) { + manifest { + attributes["Main-Class"] = "com.github.yyuueexxiinngg.onebot.MainKt" + } +} + +tasks { + val runEmbedded by creating(JavaExec::class.java) { + group = "onebot-kotlin" + main = "com.github.yyuueexxiinngg.onebot.MainKt" + workingDir = File("../test") + dependsOn(shadowJar) + dependsOn(testClasses) + doFirst { + classpath = sourceSets["test"].runtimeClasspath + standardInput = System.`in` + args("--backend", "mirai") + } + } +} + +kotlin.sourceSets.all { + languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn") +} + +kotlin.target.compilations.all { + kotlinOptions.freeCompilerArgs += "-Xjvm-default=enable" + kotlinOptions.jvmTarget = "1.8" +} \ No newline at end of file diff --git a/onebot-kotlin/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Main.kt b/onebot-kotlin/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Main.kt new file mode 100644 index 0000000..859fd7b --- /dev/null +++ b/onebot-kotlin/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Main.kt @@ -0,0 +1,102 @@ +package com.github.yyuueexxiinngg.onebot + +import kotlinx.coroutines.CancellationException +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.arguments.argument +import com.github.ajalt.clikt.parameters.arguments.multiple +import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.flag +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.types.enum +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.alsoLogin +import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load +import net.mamoe.mirai.console.terminal.ConsoleTerminalExperimentalApi +import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader +import net.mamoe.mirai.console.util.ConsoleExperimentalApi + +object OneBotKtCli : CliktCommand(name = "onebot-kotlin") { + enum class BackendType { + Mirai, + Telegram + } + + private val backend: BackendType by option( + help = """``` + Backend client of OntBot. + "Mirai" to use mirai, a Kotlin implementation of QQ protocol; + ------------------------------------------ + 后端. "Mirai" 为使用mirai, 一个Kotlin实现的QQ协议客户端; + ```""".trimIndent(), + envvar = "onebot.backend" + ).enum().default(BackendType.Mirai) + + internal val account: String? by option( + help = """``` + Account to auto login. + QQ when using mirai backend + ------------------------------------ + 需要自动登录的帐号 + 使用mirai后段时为QQ号 + ```""".trimIndent(), + envvar = "onebot.account" + ) + + internal val password: String? by option( + help = """``` + Account password to auto login. + ------------------------------------ + 需要自动登录的帐号密码 + ```""".trimIndent(), + envvar = "onebot.password" + ) + + private val args: Boolean? by option( + help = """``` + Arguments pass through to backend. + Usage: --args -- --help + ------------------------------------ + 要传递给后端的参数 + 用法: --args -- --help + ``` """.trimIndent(), + envvar = "onebot.password" + ).flag() + + private val argsToPass by argument().multiple() + + override fun run() { + when (backend) { + BackendType.Mirai -> runMirai(argsToPass.toTypedArray()) + else -> runMirai(argsToPass.toTypedArray()) + } + } +} + +fun main(args: Array) { + OneBotKtCli.main(args) +} + +@OptIn(ConsoleExperimentalApi::class, ExperimentalCommandDescriptors::class, ConsoleTerminalExperimentalApi::class) +fun runMirai(args: Array) { + MiraiConsoleTerminalLoader.parse(args, exitProcess = true) + MiraiConsoleTerminalLoader.startAsDaemon() + PluginBase.load() + PluginBase.enable() + + try { + runBlocking { + if (OneBotKtCli.account != null && OneBotKtCli.password != null) { + MiraiConsole.addBot(OneBotKtCli.account!!.toLong(), OneBotKtCli.password!!) { + fileBasedDeviceInfo() + }.alsoLogin() + } + + MiraiConsole.job.join() + } + } catch (e: CancellationException) { + // ignored + } +} \ No newline at end of file diff --git a/onebot-mirai/build.gradle.kts b/onebot-mirai/build.gradle.kts new file mode 100644 index 0000000..f30a163 --- /dev/null +++ b/onebot-mirai/build.gradle.kts @@ -0,0 +1,107 @@ +plugins { + java + kotlin("jvm") + kotlin("plugin.serialization") + kotlin("kapt") + id("com.github.johnrengelman.shadow") + id("com.github.gmazzo.buildconfig") +} + +dependencies { + kapt("com.google.auto.service", "auto-service", Versions.autoService) + + compileOnly(kotlin("stdlib-jdk8")) + compileOnly("net.mamoe:mirai-core:${Versions.miraiCoreVersion}") + compileOnly("net.mamoe:mirai-console:${Versions.miraiConsoleVersion}") + compileOnly("net.mamoe:mirai-console-terminal:${Versions.miraiConsoleVersion}") + compileOnly(kotlin("serialization", Versions.kotlinVersion)) + compileOnly("com.google.auto.service", "auto-service-annotations", Versions.autoService) + + implementation(kotlin("reflect", Versions.kotlinVersion)) + implementation(kotlinx("serialization-cbor", Versions.kotlinSerializationVersion)) + implementation(kotlinx("serialization-json", Versions.kotlinSerializationVersion)) + implementation(ktor("server-cio")) + implementation(ktor("websockets")) + implementation(ktor("client-okhttp")) + implementation(ktor("client-websockets")) + implementation("ch.qos.logback:logback-classic:${Versions.logback}") + implementation("com.google.code.gson:gson:${Versions.gson}") + + testImplementation(kotlin("stdlib-jdk8")) + testImplementation("net.mamoe:mirai-core:${Versions.miraiCoreVersion}") + testImplementation("net.mamoe:mirai-core-qqandroid:${Versions.miraiCoreVersion}") + testImplementation("net.mamoe:mirai-console:${Versions.miraiConsoleVersion}") + testImplementation("net.mamoe:mirai-console-terminal:${Versions.miraiConsoleVersion}") +} + +tasks { + buildConfig { + packageName("com.github.yyuueexxiinngg.onebot") + val commitHash = "git rev-parse --short HEAD".runCommand(projectDir) + buildConfigField("String", "VERSION", "\"${Versions.projectVersion}\"") + if (commitHash != null) { + buildConfigField("String", "COMMIT_HASH", "\"$commitHash\"") + } + } + + shadowJar { + dependsOn(generateBuildConfig) + } + + val runMiraiConsole by creating(JavaExec::class.java) { + group = "mirai" + main = "mirai.RunMirai" + dependsOn(shadowJar) + dependsOn(testClasses) + + val testConsoleDir = "../test" + + doFirst { + fun removeOldVersions() { + File("$testConsoleDir/plugins/").walk() + .filter { it.name.matches(Regex("""${project.name}-.*-all.jar""")) } + .forEach { + it.delete() + println("deleting old files: ${it.name}") + } + } + + fun copyBuildOutput() { + File("build/libs/").walk() + .filter { it.name.contains("-all") } + .maxBy { it.lastModified() } + ?.let { + println("Coping ${it.name}") + it.inputStream() + .transferTo(File("$testConsoleDir/plugins/${it.name}").apply { createNewFile() } + .outputStream()) + println("Copied ${it.name}") + } + } + + workingDir = File(testConsoleDir) + workingDir.mkdir() + File(workingDir, "plugins").mkdir() + removeOldVersions() + copyBuildOutput() + + classpath = sourceSets["test"].runtimeClasspath + standardInput = System.`in` + args(Versions.miraiCoreVersion, Versions.miraiConsoleVersion) + } + } +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +kotlin.sourceSets.all { + languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn") +} + +kotlin.target.compilations.all { + kotlinOptions.freeCompilerArgs += "-Xjvm-default=enable" + kotlinOptions.jvmTarget = "1.8" +} \ No newline at end of file diff --git a/src/main/kotlin/tech/mihoyo/mirai/MiraiApi.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/MiraiApi.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/MiraiApi.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/MiraiApi.kt index 95024b9..495e167 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/MiraiApi.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/MiraiApi.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai +package com.github.yyuueexxiinngg.onebot import com.google.gson.Gson import kotlinx.coroutines.isActive @@ -16,11 +16,11 @@ import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent import net.mamoe.mirai.event.events.MemberJoinRequestEvent import net.mamoe.mirai.event.events.NewFriendRequestEvent import net.mamoe.mirai.message.data.recall -import tech.mihoyo.mirai.PluginBase.saveImage -import tech.mihoyo.mirai.data.common.* -import tech.mihoyo.mirai.util.* -import tech.mihoyo.mirai.web.queue.CacheSourceQueue -import tech.mihoyo.mirai.web.queue.CacheRequestQueue +import com.github.yyuueexxiinngg.onebot.PluginBase.saveImage +import com.github.yyuueexxiinngg.onebot.data.common.* +import com.github.yyuueexxiinngg.onebot.util.* +import com.github.yyuueexxiinngg.onebot.web.queue.CacheSourceQueue +import com.github.yyuueexxiinngg.onebot.web.queue.CacheRequestQueue @OptIn(LowLevelAPI::class) @@ -363,7 +363,7 @@ class MiraiApi(val bot: Bot) { val replyElement = operation?.get("reply") if (replyElement != null) { val nextCallParams = context.toMutableMap() - if (messageType == "group" && operation?.get("at_sender")?.jsonPrimitive?.booleanOrNull == true) { + if (messageType == "group" && operation["at_sender"]?.jsonPrimitive?.booleanOrNull == true) { context["user_id"]?.jsonPrimitive?.apply { when (replyElement) { is JsonArray -> { diff --git a/src/main/kotlin/tech/mihoyo/mirai/PluginBase.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginBase.kt similarity index 93% rename from src/main/kotlin/tech/mihoyo/mirai/PluginBase.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginBase.kt index d946aa9..e0ecf01 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/PluginBase.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginBase.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai +package com.github.yyuueexxiinngg.onebot import com.google.auto.service.AutoService import kotlinx.coroutines.Dispatchers @@ -22,20 +22,20 @@ import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Voice import net.mamoe.mirai.message.data.source import net.mamoe.mirai.utils.currentTimeMillis -import tech.mihoyo.mirai.SessionManager.allSession -import tech.mihoyo.mirai.SessionManager.closeSession -import tech.mihoyo.mirai.util.EventFilter -import tech.mihoyo.mirai.util.HttpClient -import tech.mihoyo.mirai.util.HttpClient.Companion.initHTTPClientProxy -import tech.mihoyo.mirai.util.toUHexString +import com.github.yyuueexxiinngg.onebot.SessionManager.allSession +import com.github.yyuueexxiinngg.onebot.SessionManager.closeSession +import com.github.yyuueexxiinngg.onebot.util.EventFilter +import com.github.yyuueexxiinngg.onebot.util.HttpClient +import com.github.yyuueexxiinngg.onebot.util.HttpClient.Companion.initHTTPClientProxy +import com.github.yyuueexxiinngg.onebot.util.toUHexString import java.io.File import kotlin.reflect.jvm.isAccessible -import yyuueexxiinngg.onebot_mirai.BuildConfig +import com.github.yyuueexxiinngg.onebot.BuildConfig @AutoService(JvmPlugin::class) object PluginBase : KotlinPlugin( JvmPluginDescription( - id = "tech.mihoyo.onebot", + id = "com.github.yyuueexxiinngg.onebot", version = BuildConfig.VERSION, ) { name("OneBot") @@ -122,7 +122,7 @@ object PluginBase : KotlinPlugin( is Member, is Friend -> { imageMD5 = (delegate?.let { _delegate -> _delegate::class.members.find { it.name == "picMd5" } } - ?.call(delegate) as ByteArray?)?.let { it.toUHexString("") } ?: "" + ?.call(delegate) as ByteArray?)?.toUHexString("") ?: "" val imageHeight = delegate?.let { _delegate -> _delegate::class.members.find { it.name == "picHeight" } } ?.call(delegate) as Int? @@ -137,7 +137,7 @@ object PluginBase : KotlinPlugin( is Group -> { imageMD5 = (delegate?.let { _delegate -> _delegate::class.members.find { it.name == "md5" } } - ?.call(delegate) as ByteArray?)?.let { it.toUHexString("") } ?: "" + ?.call(delegate) as ByteArray?)?.toUHexString("") ?: "" imageSize = (delegate?.let { _delegate -> _delegate::class.members.find { it.name == "size" } } ?.call(delegate) as Int?) ?: 0 diff --git a/src/main/kotlin/tech/mihoyo/mirai/PluginSettings.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginSettings.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/PluginSettings.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginSettings.kt index 517b829..ecea27e 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/PluginSettings.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/PluginSettings.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai +package com.github.yyuueexxiinngg.onebot import kotlinx.serialization.SerialName import net.mamoe.mirai.console.data.* diff --git a/src/main/kotlin/tech/mihoyo/mirai/Session.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Session.kt similarity index 87% rename from src/main/kotlin/tech/mihoyo/mirai/Session.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Session.kt index a9463cd..09117dd 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/Session.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/Session.kt @@ -1,12 +1,13 @@ -package tech.mihoyo.mirai +package com.github.yyuueexxiinngg.onebot import kotlinx.coroutines.* import net.mamoe.mirai.Bot -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.web.http.HttpApiServer -import tech.mihoyo.mirai.web.http.ReportService -import tech.mihoyo.mirai.web.websocket.WebSocketReverseClient -import tech.mihoyo.mirai.web.websocket.WebSocketServer +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.web.http.HttpApiServer +import com.github.yyuueexxiinngg.onebot.web.http.ReportService +import com.github.yyuueexxiinngg.onebot.web.websocket.WebSocketReverseClient +import com.github.yyuueexxiinngg.onebot.web.websocket.WebSocketServer +import io.ktor.util.* import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext diff --git a/src/main/kotlin/tech/mihoyo/mirai/data/common/BotEventDTO.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/BotEventDTO.kt similarity index 99% rename from src/main/kotlin/tech/mihoyo/mirai/data/common/BotEventDTO.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/BotEventDTO.kt index f7b032c..7557d58 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/data/common/BotEventDTO.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/BotEventDTO.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.data.common +package com.github.yyuueexxiinngg.onebot.data.common import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable diff --git a/src/main/kotlin/tech/mihoyo/mirai/data/common/ContactDTO.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ContactDTO.kt similarity index 97% rename from src/main/kotlin/tech/mihoyo/mirai/data/common/ContactDTO.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ContactDTO.kt index fd8673b..cd4599b 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/data/common/ContactDTO.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ContactDTO.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.data.common +package com.github.yyuueexxiinngg.onebot.data.common import kotlinx.serialization.Serializable import net.mamoe.mirai.contact.* diff --git a/src/main/kotlin/tech/mihoyo/mirai/data/common/DTO.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/DTO.kt similarity index 94% rename from src/main/kotlin/tech/mihoyo/mirai/data/common/DTO.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/DTO.kt index 3211957..91b81e2 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/data/common/DTO.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/DTO.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.data.common +package com.github.yyuueexxiinngg.onebot.data.common import kotlinx.serialization.Serializable diff --git a/src/main/kotlin/tech/mihoyo/mirai/data/common/MessageDTO.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/MessageDTO.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/data/common/MessageDTO.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/MessageDTO.kt index 86156e8..9bacb04 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/data/common/MessageDTO.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/MessageDTO.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.data.common +package com.github.yyuueexxiinngg.onebot.data.common import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer @@ -15,13 +15,13 @@ import kotlinx.serialization.builtins.serializer import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder -import tech.mihoyo.mirai.util.PokeMap +import com.github.yyuueexxiinngg.onebot.util.PokeMap import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Group import net.mamoe.mirai.message.* import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.currentTimeSeconds -import tech.mihoyo.mirai.util.toCQString +import com.github.yyuueexxiinngg.onebot.util.toCQString import java.net.URL /* diff --git a/src/main/kotlin/tech/mihoyo/mirai/data/common/ResponseDTO.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ResponseDTO.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/data/common/ResponseDTO.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ResponseDTO.kt index 699cb40..a7bdd4a 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/data/common/ResponseDTO.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/data/common/ResponseDTO.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.data.common +package com.github.yyuueexxiinngg.onebot.data.common import com.google.gson.annotations.SerializedName import kotlinx.serialization.* @@ -10,7 +10,7 @@ import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.JsonElement import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.MemberPermission -import yyuueexxiinngg.onebot_mirai.BuildConfig +import com.github.yyuueexxiinngg.onebot.BuildConfig @Serializable sealed class CQResponseDataDTO @@ -54,6 +54,7 @@ open class CQResponseDTO( error("Not implemented") } + @Suppress("UNCHECKED_CAST") override fun serialize(encoder: Encoder, value: Any?) { return when (value) { is List<*> -> encoder.encodeSerializableValue( diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/CQMessgeParser.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/CQMessgeParser.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/util/CQMessgeParser.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/CQMessgeParser.kt index 35e6fa1..11daef7 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/CQMessgeParser.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/CQMessgeParser.kt @@ -21,7 +21,7 @@ * @website https://github.com/iTXTech/mirai-native * */ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -34,9 +34,9 @@ import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.uploadImage import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.currentTimeMillis -import tech.mihoyo.mirai.PluginBase -import tech.mihoyo.mirai.PluginBase.saveImageAsync -import tech.mihoyo.mirai.PluginBase.saveRecordAsync +import com.github.yyuueexxiinngg.onebot.PluginBase +import com.github.yyuueexxiinngg.onebot.PluginBase.saveImageAsync +import com.github.yyuueexxiinngg.onebot.PluginBase.saveRecordAsync import java.io.ByteArrayInputStream import java.io.File import java.net.URL @@ -235,7 +235,7 @@ private fun String.toMap(): HashMap { return map } -@OptIn(MiraiExperimentalAPI::class, ExperimentalUnsignedTypes::class) +@OptIn(MiraiExperimentalAPI::class) suspend fun Message.toCQString(): String { return when (this) { is PlainText -> content.escape() @@ -564,7 +564,7 @@ fun md5(data: ByteArray): ByteArray { fun md5(str: String): ByteArray = md5(str.toByteArray()) -@ExperimentalUnsignedTypes +@OptIn(ExperimentalUnsignedTypes::class) internal fun ByteArray.toUHexString( separator: String = " ", offset: Int = 0, diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/EventFilter.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/EventFilter.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/util/EventFilter.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/EventFilter.kt index 1e2cf6c..ed329fb 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/EventFilter.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/EventFilter.kt @@ -1,7 +1,7 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import kotlinx.serialization.json.* -import tech.mihoyo.mirai.PluginBase +import com.github.yyuueexxiinngg.onebot.PluginBase import java.io.File class EventFilter { diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/FaceMap.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/FaceMap.kt similarity index 95% rename from src/main/kotlin/tech/mihoyo/mirai/util/FaceMap.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/FaceMap.kt index e4adc52..71b45ef 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/FaceMap.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/FaceMap.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import net.mamoe.mirai.message.data.Face import kotlin.reflect.full.memberProperties diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/HttpClient.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/HttpClient.kt similarity index 97% rename from src/main/kotlin/tech/mihoyo/mirai/util/HttpClient.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/HttpClient.kt index 5a07cb4..b0e2f34 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/HttpClient.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/HttpClient.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import io.ktor.client.engine.* import io.ktor.client.engine.okhttp.* @@ -7,7 +7,7 @@ import io.ktor.client.request.* import io.ktor.http.* import io.ktor.network.sockets.* import io.ktor.util.* -import tech.mihoyo.mirai.PluginSettings +import com.github.yyuueexxiinngg.onebot.PluginSettings import java.io.InputStream class HttpClient { diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/ImgUtil.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/ImgUtil.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/util/ImgUtil.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/ImgUtil.kt index 0f603fb..62aaeaa 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/ImgUtil.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/ImgUtil.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.Contact diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/Json.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Json.kt similarity index 94% rename from src/main/kotlin/tech/mihoyo/mirai/util/Json.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Json.kt index 84f0bea..40fc777 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/Json.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Json.kt @@ -1,11 +1,11 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import kotlinx.serialization.* import kotlinx.serialization.json.Json import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.modules.polymorphic import kotlinx.serialization.modules.subclass -import tech.mihoyo.mirai.data.common.* +import com.github.yyuueexxiinngg.onebot.data.common.* import kotlin.reflect.KClass inline fun T.toJson( diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/Logger.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Logger.kt similarity index 89% rename from src/main/kotlin/tech/mihoyo/mirai/util/Logger.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Logger.kt index 6b9537b..8136662 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/Logger.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Logger.kt @@ -1,8 +1,8 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import net.mamoe.mirai.utils.MiraiLoggerPlatformBase -import tech.mihoyo.mirai.PluginBase -import tech.mihoyo.mirai.PluginSettings +import com.github.yyuueexxiinngg.onebot.PluginBase +import com.github.yyuueexxiinngg.onebot.PluginSettings class Logger(override val identity: String?) : MiraiLoggerPlatformBase() { private val consoleLogger = PluginBase.logger diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/Music.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Music.kt similarity index 99% rename from src/main/kotlin/tech/mihoyo/mirai/util/Music.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Music.kt index 17ef649..92e28ba 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/Music.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/Music.kt @@ -22,7 +22,7 @@ * */ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import io.ktor.client.HttpClient import io.ktor.client.engine.okhttp.* diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/PokeMap.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/PokeMap.kt similarity index 94% rename from src/main/kotlin/tech/mihoyo/mirai/util/PokeMap.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/PokeMap.kt index 4e0d36e..7d28881 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/PokeMap.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/PokeMap.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import net.mamoe.mirai.message.data.PokeMessage import net.mamoe.mirai.message.data.PokeMessage.Types.Poke diff --git a/src/main/kotlin/tech/mihoyo/mirai/util/RichMessageHelper.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/RichMessageHelper.kt similarity index 98% rename from src/main/kotlin/tech/mihoyo/mirai/util/RichMessageHelper.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/RichMessageHelper.kt index ca386c5..dfcd5a7 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/util/RichMessageHelper.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/util/RichMessageHelper.kt @@ -22,7 +22,7 @@ * */ -package tech.mihoyo.mirai.util +package com.github.yyuueexxiinngg.onebot.util import net.mamoe.mirai.Bot import net.mamoe.mirai.message.data.ServiceMessage diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/HeartbeatScope.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/HeartbeatScope.kt similarity index 82% rename from src/main/kotlin/tech/mihoyo/mirai/web/HeartbeatScope.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/HeartbeatScope.kt index c3873d2..8481cc0 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/HeartbeatScope.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/HeartbeatScope.kt @@ -1,9 +1,9 @@ -package tech.mihoyo.mirai.web +package com.github.yyuueexxiinngg.onebot.web import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob -import tech.mihoyo.mirai.util.logger +import com.github.yyuueexxiinngg.onebot.util.logger import kotlin.coroutines.CoroutineContext class HeartbeatScope(coroutineContext: CoroutineContext) : CoroutineScope { diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiModule.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiModule.kt similarity index 97% rename from src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiModule.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiModule.kt index ff66945..13a1801 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiModule.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiModule.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.web.http +package com.github.yyuueexxiinngg.onebot.web.http import io.ktor.application.* import io.ktor.features.* @@ -20,12 +20,12 @@ import io.ktor.util.pipeline.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.serialization.json.* -import tech.mihoyo.mirai.BotSession -import tech.mihoyo.mirai.PluginSettings -import tech.mihoyo.mirai.callMiraiApi -import tech.mihoyo.mirai.data.common.CQResponseDTO -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.util.toJson +import com.github.yyuueexxiinngg.onebot.BotSession +import com.github.yyuueexxiinngg.onebot.PluginSettings +import com.github.yyuueexxiinngg.onebot.callMiraiApi +import com.github.yyuueexxiinngg.onebot.data.common.CQResponseDTO +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.util.toJson import java.nio.charset.Charset import kotlin.coroutines.EmptyCoroutineContext diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiServer.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiServer.kt similarity index 86% rename from src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiServer.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiServer.kt index a504c07..ca6c0fc 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiServer.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/HttpApiServer.kt @@ -1,10 +1,10 @@ -package tech.mihoyo.mirai.web.http +package com.github.yyuueexxiinngg.onebot.web.http import io.ktor.server.cio.* import io.ktor.server.engine.* +import com.github.yyuueexxiinngg.onebot.BotSession +import com.github.yyuueexxiinngg.onebot.util.logger import io.ktor.util.* -import tech.mihoyo.mirai.BotSession -import tech.mihoyo.mirai.util.logger @OptIn(KtorExperimentalAPI::class) class HttpApiServer( diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/http/ReportService.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/ReportService.kt similarity index 91% rename from src/main/kotlin/tech/mihoyo/mirai/web/http/ReportService.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/ReportService.kt index 2d2979b..e095a46 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/http/ReportService.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/http/ReportService.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.web.http +package com.github.yyuueexxiinngg.onebot.web.http import io.ktor.client.HttpClient import io.ktor.client.engine.okhttp.* @@ -18,13 +18,13 @@ import net.mamoe.mirai.event.Listener import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.utils.currentTimeSeconds -import tech.mihoyo.mirai.BotSession -import tech.mihoyo.mirai.MiraiApi -import tech.mihoyo.mirai.data.common.* -import tech.mihoyo.mirai.util.EventFilter -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.util.toJson -import tech.mihoyo.mirai.web.HeartbeatScope +import com.github.yyuueexxiinngg.onebot.BotSession +import com.github.yyuueexxiinngg.onebot.MiraiApi +import com.github.yyuueexxiinngg.onebot.data.common.* +import com.github.yyuueexxiinngg.onebot.util.EventFilter +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.util.toJson +import com.github.yyuueexxiinngg.onebot.web.HeartbeatScope import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec import kotlin.coroutines.CoroutineContext @@ -76,7 +76,7 @@ class ReportService( report( session.cqApiImpl, - settings.postUrl!!, + settings.postUrl, session.bot.id, CQLifecycleMetaEventDTO(session.botId, "enable", currentTimeSeconds).toJson(), settings.secret, @@ -94,7 +94,7 @@ class ReportService( scope.launch(Dispatchers.IO) { report( session.cqApiImpl, - settings.postUrl!!, + settings.postUrl, bot.id, jsonToSend, settings.secret, @@ -110,7 +110,7 @@ class ReportService( while (true) { report( session.cqApiImpl, - settings.postUrl!!, + settings.postUrl, session.bot.id, CQHeartbeatMetaEventDTO( session.botId, @@ -175,7 +175,7 @@ class ReportService( if (settings.postUrl != "") { report( session.cqApiImpl, - settings.postUrl!!, + settings.postUrl, session.bot.id, CQLifecycleMetaEventDTO(session.botId, "disable", currentTimeSeconds).toJson(), settings.secret, diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheRequestQueue.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheRequestQueue.kt similarity index 94% rename from src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheRequestQueue.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheRequestQueue.kt index eaff2e8..fe53dc6 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheRequestQueue.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheRequestQueue.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.web.queue +package com.github.yyuueexxiinngg.onebot.web.queue import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheSourceQueue.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheSourceQueue.kt similarity index 94% rename from src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheSourceQueue.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheSourceQueue.kt index 2cc0025..af1bff8 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/queue/CacheSourceQueue.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/queue/CacheSourceQueue.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package tech.mihoyo.mirai.web.queue +package com.github.yyuueexxiinngg.onebot.web.queue import net.mamoe.mirai.message.data.MessageSource diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketActions.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketActions.kt similarity index 78% rename from src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketActions.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketActions.kt index 29b4e05..1918f2a 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketActions.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketActions.kt @@ -1,14 +1,14 @@ -package tech.mihoyo.mirai.web.websocket +package com.github.yyuueexxiinngg.onebot.web.websocket import io.ktor.http.cio.websocket.Frame import kotlinx.coroutines.* import kotlinx.coroutines.channels.SendChannel import kotlinx.serialization.json.* -import tech.mihoyo.mirai.MiraiApi -import tech.mihoyo.mirai.callMiraiApi -import tech.mihoyo.mirai.data.common.CQResponseDTO -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.util.toJson +import com.github.yyuueexxiinngg.onebot.MiraiApi +import com.github.yyuueexxiinngg.onebot.callMiraiApi +import com.github.yyuueexxiinngg.onebot.data.common.CQResponseDTO +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.util.toJson import kotlin.coroutines.EmptyCoroutineContext suspend fun handleWebSocketActions(outgoing: SendChannel, mirai: MiraiApi, cqActionText: String) { diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketReverseClient.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketReverseClient.kt similarity index 96% rename from src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketReverseClient.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketReverseClient.kt index aa553f6..e29cfd3 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketReverseClient.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketReverseClient.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.web.websocket +package com.github.yyuueexxiinngg.onebot.web.websocket import io.ktor.client.HttpClient import io.ktor.client.features.websocket.* @@ -17,13 +17,13 @@ import net.mamoe.mirai.event.Listener import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.utils.currentTimeSeconds -import tech.mihoyo.mirai.BotSession -import tech.mihoyo.mirai.PluginSettings -import tech.mihoyo.mirai.data.common.* -import tech.mihoyo.mirai.util.EventFilter -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.util.toJson -import tech.mihoyo.mirai.web.HeartbeatScope +import com.github.yyuueexxiinngg.onebot.BotSession +import com.github.yyuueexxiinngg.onebot.PluginSettings +import com.github.yyuueexxiinngg.onebot.data.common.* +import com.github.yyuueexxiinngg.onebot.util.EventFilter +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.util.toJson +import com.github.yyuueexxiinngg.onebot.web.HeartbeatScope import java.io.EOFException import java.io.IOException import java.net.ConnectException @@ -102,7 +102,7 @@ class WebSocketReverseClient( header("User-Agent", "CQHttp/4.15.0") header("X-Self-ID", bot.id.toString()) header("X-Client-Role", clientType) - settings.accessToken?.let { + settings.accessToken.let { if (it != "") { header( "Authorization", @@ -138,7 +138,7 @@ class WebSocketReverseClient( header("User-Agent", "CQHttp/4.15.0") header("X-Self-ID", bot.id.toString()) header("X-Client-Role", clientType) - settings.accessToken?.let { + settings.accessToken.let { if (it != "") { header( "Authorization", diff --git a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketServer.kt b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketServer.kt similarity index 86% rename from src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketServer.kt rename to onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketServer.kt index 9436805..2e7243a 100644 --- a/src/main/kotlin/tech/mihoyo/mirai/web/websocket/WebsocketServer.kt +++ b/onebot-mirai/src/main/kotlin/com/github/yyuueexxiinngg/onebot/web/websocket/WebsocketServer.kt @@ -1,4 +1,4 @@ -package tech.mihoyo.mirai.web.websocket +package com.github.yyuueexxiinngg.onebot.web.websocket import io.ktor.application.Application import io.ktor.application.install @@ -19,16 +19,17 @@ import kotlinx.coroutines.channels.consumeEach import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.utils.currentTimeSeconds -import tech.mihoyo.mirai.BotSession -import tech.mihoyo.mirai.PluginSettings -import tech.mihoyo.mirai.data.common.CQHeartbeatMetaEventDTO -import tech.mihoyo.mirai.data.common.CQIgnoreEventDTO -import tech.mihoyo.mirai.data.common.CQPluginStatusData -import tech.mihoyo.mirai.data.common.toCQDTO -import tech.mihoyo.mirai.util.EventFilter -import tech.mihoyo.mirai.util.logger -import tech.mihoyo.mirai.util.toJson -import tech.mihoyo.mirai.web.HeartbeatScope +import com.github.yyuueexxiinngg.onebot.BotSession +import com.github.yyuueexxiinngg.onebot.PluginSettings +import com.github.yyuueexxiinngg.onebot.data.common.CQHeartbeatMetaEventDTO +import com.github.yyuueexxiinngg.onebot.data.common.CQIgnoreEventDTO +import com.github.yyuueexxiinngg.onebot.data.common.CQPluginStatusData +import com.github.yyuueexxiinngg.onebot.data.common.toCQDTO +import com.github.yyuueexxiinngg.onebot.util.EventFilter +import com.github.yyuueexxiinngg.onebot.util.logger +import com.github.yyuueexxiinngg.onebot.util.toJson +import com.github.yyuueexxiinngg.onebot.web.HeartbeatScope +import io.ktor.util.* import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext @@ -38,10 +39,11 @@ class WebsocketServerScope(coroutineContext: CoroutineContext) : CoroutineScope } + SupervisorJob() } +@OptIn(KtorExperimentalAPI::class) class WebSocketServer( private val session: BotSession ) { - lateinit var server: ApplicationEngine + private lateinit var server: ApplicationEngine init { val settings = session.settings.ws @@ -107,11 +109,9 @@ fun Application.cqWebsocketServer(session: BotSession, settings: PluginSettings. try { logger.debug("Bot: ${session.bot.id} 正向Websocket服务端 /api 开始处理API请求") incoming.consumeEach { - when (it) { - is Frame.Text -> { - scope.launch { - handleWebSocketActions(outgoing, session.cqApiImpl, it.readText()) - } + if (it is Frame.Text) { + scope.launch { + handleWebSocketActions(outgoing, session.cqApiImpl, it.readText()) } } } @@ -138,11 +138,9 @@ fun Application.cqWebsocketServer(session: BotSession, settings: PluginSettings. try { logger.debug("Bot: ${session.bot.id} 正向Websocket服务端 / 开始处理API请求") incoming.consumeEach { - when (it) { - is Frame.Text -> { - scope.launch { - handleWebSocketActions(outgoing, session.cqApiImpl, it.readText()) - } + if (it is Frame.Text) { + scope.launch { + handleWebSocketActions(outgoing, session.cqApiImpl, it.readText()) } } } diff --git a/src/test/kotlin/mirai/RunMirai.kt b/onebot-mirai/src/test/kotlin/mirai/RunMirai.kt similarity index 100% rename from src/test/kotlin/mirai/RunMirai.kt rename to onebot-mirai/src/test/kotlin/mirai/RunMirai.kt diff --git a/settings.gradle.kts b/settings.gradle.kts index 0ff853a..68954f9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,4 +16,6 @@ pluginManagement { } } -rootProject.name = "onebot-mirai" \ No newline at end of file +rootProject.name = "onebot" +include(":onebot-mirai") +include(":onebot-kotlin") \ No newline at end of file