From 4240c940a331eadd5e27e33c9049beeab4fcd436 Mon Sep 17 00:00:00 2001 From: alazeprt Date: Wed, 21 Aug 2024 06:43:38 +0800 Subject: [PATCH] Access to the new onebot api --- .../kotlin/me/regadpole/plumbot/bot/QQBot.kt | 60 +++++++------------ 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt b/common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt index e56ace6..c7d2030 100644 --- a/common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt +++ b/common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt @@ -1,14 +1,14 @@ package me.regadpole.plumbot.bot -import cn.evole.onebot.client.OneBotClient -import cn.evole.onebot.client.core.BotConfig -import cn.evole.onebot.sdk.util.MsgUtils import io.ktor.http.* import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import love.forte.simbot.application.Application import love.forte.simbot.application.launchApplication import love.forte.simbot.application.listeners +import love.forte.simbot.common.id.ID +import love.forte.simbot.common.id.LongID.Companion.ID +import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration import love.forte.simbot.component.onebot.v11.core.bot.firstOneBotBotManager import love.forte.simbot.component.onebot.v11.core.event.message.OneBotGroupMessageEvent @@ -16,10 +16,7 @@ import love.forte.simbot.component.onebot.v11.core.event.message.OneBotPrivateMe import love.forte.simbot.component.onebot.v11.core.event.notice.OneBotGroupMemberDecreaseEvent import love.forte.simbot.component.onebot.v11.core.useOneBot11 import love.forte.simbot.core.application.Simple -import love.forte.simbot.core.application.launchSimpleApplication -import love.forte.simbot.event.ChatGroupMessageEvent import love.forte.simbot.event.EventResult -import love.forte.simbot.event.listen import love.forte.simbot.event.process import me.regadpole.plumbot.PlumBot import me.regadpole.plumbot.listener.qq.QQListener @@ -29,7 +26,9 @@ import taboolib.common.platform.function.submitAsync class QQBot(private val plugin: PlumBot): Bot { - private lateinit var onebot: Application + private lateinit var application: Application + + private lateinit var onebot: OneBotBot /** * Start a bot @@ -37,12 +36,12 @@ class QQBot(private val plugin: PlumBot): Bot { override fun start(): Bot { runBlocking { launch { - onebot = launchApplication(Simple) { + application = launchApplication(Simple) { useOneBot11() } - onebot.configure() - onebot.join() + application.configure() + application.join() } } val groups: List = plugin.getConfig().getConfig().enableGroups @@ -55,7 +54,7 @@ class QQBot(private val plugin: PlumBot): Bot { private suspend fun Application.configure() { val botManager = botManagers.firstOneBotBotManager() - val bot = botManager.register( + onebot = botManager.register( OneBotBotConfiguration().apply { botUniqueId = "11112222" apiServerHost = Url("http://127.0.0.1:3000") @@ -78,7 +77,7 @@ class QQBot(private val plugin: PlumBot): Bot { EventResult.empty() } } - bot.start() + onebot.start() } /** @@ -89,7 +88,7 @@ class QQBot(private val plugin: PlumBot): Bot { for (groupID in groups) { sendMsg(true, groupID, "PlumBot已关闭") } - onebot.close() + application.cancel() info("已关闭go-cqhttp服务") } @@ -101,7 +100,8 @@ class QQBot(private val plugin: PlumBot): Bot { override fun getGroupName(groupId: String): String? { if (groupId.toLongOrNull() == null) return null val group = groupId.toLong() - return onebot.bot.getGroupInfo(group, false).data.groupName + application.botManagers.firstBot() + return onebot.groupRelation.getGroup(group.ID)?.name } /** @@ -113,10 +113,8 @@ class QQBot(private val plugin: PlumBot): Bot { override fun checkUserInGroup(userId: String, groupId: String): Boolean { if (userId.toLongOrNull() == null || groupId.toLongOrNull() == null) return false val user = userId.toLong(); val group = groupId.toLong() - for (groupMemberInfoResp in onebot.bot.getGroupMemberList(group).data) { - if (groupMemberInfoResp.userId == user) return true - } - return false + if (onebot.groupRelation.getGroup(group.ID)?.getMember(user.ID) == null) return false + return true } /** @@ -127,7 +125,7 @@ class QQBot(private val plugin: PlumBot): Bot { override fun sendGroupMsg(targetId: String, message: String) { if (targetId.toLongOrNull() == null) return val target = targetId.toLong() - onebot.bot.sendGroupMsg(target, MsgUtils.builder().text(message).build(), true) + onebot.groupRelation.getGroup(target.ID)?.sendAsync(message) } /** @@ -138,7 +136,7 @@ class QQBot(private val plugin: PlumBot): Bot { override fun sendUserMsg(targetId: String, message: String) { if (targetId.toLongOrNull() == null) return val target = targetId.toLong() - onebot.bot.sendGroupMsg(target, MsgUtils.builder().text(message).build(), true) + onebot.contactRelation.getContact(target.ID)?.sendAsync(message) } /** @@ -166,28 +164,10 @@ class QQBot(private val plugin: PlumBot): Bot { } submitAsync(now = true) { if (isGroup) { - sendGroupCQMsg(targetId, msg) + sendGroupMsg(targetId, msg) } else { - sendUserCQMsg(targetId, msg) + sendUserMsg(targetId, msg) } } } - - /** - * 发送私聊消息 - */ - private fun sendUserCQMsg(targetId: String, msg: String) { - if (targetId.toLongOrNull() == null) return - val target = targetId.toLong() - onebot.bot.sendPrivateMsg(target, msg, false) - } - - /** - * 发送群聊消息 - */ - private fun sendGroupCQMsg(targetId: String, msg: String) { - if (targetId.toLongOrNull() == null) return - val target = targetId.toLong() - onebot.bot.sendGroupMsg(target, msg, false) - } } \ No newline at end of file