Skip to content

Commit

Permalink
Access to the new onebot api
Browse files Browse the repository at this point in the history
  • Loading branch information
alazeprt committed Aug 20, 2024
1 parent f80d9b4 commit 4240c94
Showing 1 changed file with 20 additions and 40 deletions.
60 changes: 20 additions & 40 deletions common/src/main/kotlin/me/regadpole/plumbot/bot/QQBot.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
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
import love.forte.simbot.component.onebot.v11.core.event.message.OneBotPrivateMessageEvent
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
Expand All @@ -29,20 +26,22 @@ 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
*/
override fun start(): Bot {
runBlocking {
launch {
onebot = launchApplication(Simple) {
application = launchApplication(Simple) {
useOneBot11()
}

onebot.configure()
onebot.join()
application.configure()
application.join()
}
}
val groups: List<String> = plugin.getConfig().getConfig().enableGroups
Expand All @@ -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")
Expand All @@ -78,7 +77,7 @@ class QQBot(private val plugin: PlumBot): Bot {
EventResult.empty()
}
}
bot.start()
onebot.start()
}

/**
Expand All @@ -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服务")
}

Expand All @@ -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
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 4240c94

Please sign in to comment.