Skip to content

Commit

Permalink
remove database
Browse files Browse the repository at this point in the history
  • Loading branch information
WsureDev committed Mar 24, 2021
1 parent edd6c5e commit 17f3362
Show file tree
Hide file tree
Showing 25 changed files with 46 additions and 799 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ logs/

device.json
gradle/

bots/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@
1. 失败经验:mybatisPlus、mybatis、ktorm、sqlite jdbc在MiraiAndroid上无法运行(扫包路径问题、MiraiAndroid内二次打包文件过滤问题、安卓so文件加载问题、Android.jar与rt.jar区别)。
2. 成功经验:数据库方案选型:h2 jdbc+Exposed 或 原生jdbc
3. exposed:写操作使用entity(DAO),读操作请用table(DSL),使用Entity读会导致出现对象已关闭的错误
4. 数据库已经从本项目`master`分支移除,需要查看用例请看`Exposed-archived`分支
21 changes: 4 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
plugins {
id("net.mamoe.mirai-console") version "2.0-RC" // mirai-console version
id 'org.jetbrains.kotlin.jvm' version "1.4.21"
id 'org.jetbrains.kotlin.plugin.serialization' version "1.4.21"
id("net.mamoe.mirai-console") version "2.4.0" // mirai-console version
id 'org.jetbrains.kotlin.jvm' version "1.4.31"
id 'org.jetbrains.kotlin.plugin.serialization' version "1.4.31"
}

mirai {
coreVersion = "2.0-RC" // mirai-core version
coreVersion = "2.4.0" // mirai-core version
}

group = "top.wsure.warframe"
Expand All @@ -19,21 +19,8 @@ repositories {
}

dependencies {
def exposedVersion = "0.29.1"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0"

implementation "com.squareup.okhttp3:okhttp:4.9.0"

implementation "com.h2database:h2:1.4.200"

implementation "org.jetbrains.exposed:exposed-core:$exposedVersion"

implementation "org.jetbrains.exposed:exposed-dao:$exposedVersion"

implementation "org.jetbrains.exposed:exposed-jdbc:$exposedVersion"

implementation "org.jetbrains.exposed:exposed-jodatime:$exposedVersion"

}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
88 changes: 6 additions & 82 deletions src/main/kotlin/top/wsure/warframe/WorldState.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package top.wsure.warframe

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandSender.Companion.toCommandSender
Expand All @@ -16,13 +13,10 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.utils.info
import top.wsure.warframe.command.DatabaseCommand
import top.wsure.warframe.command.ChatCommandEditor
import top.wsure.warframe.command.WFHelp
import top.wsure.warframe.data.RemoteCommand
import top.wsure.warframe.data.WorldStateData
import top.wsure.warframe.utils.CommandUtils
import top.wsure.warframe.utils.CommandUtils.Companion.handleCommand
import top.wsure.warframe.utils.DBUtils

object WorldState : KotlinPlugin(
// @OptIn(ConsoleExperimentalApi::class)
Expand All @@ -34,10 +28,6 @@ object WorldState : KotlinPlugin(
)
) {

private const val HELP_KEY = "help"
private const val DB_NAME = "test"
val DB_FILE = resolveDataFile(DB_NAME)

@ExperimentalCommandDescriptors
@ConsoleExperimentalApi
override fun onEnable() {
Expand All @@ -54,96 +44,30 @@ object WorldState : KotlinPlugin(

WFHelp(WorldState,WorldStateData.helpKey).register()

DatabaseCommand.register()
ChatCommandEditor.register()

AbstractPermitteeId.AnyContact.permit(WorldState.parentPermission)

initDatabase()
}

globalEventChannel().subscribeAlways<MessageEvent> {
val sender = kotlin.runCatching {
this.toCommandSender()
}.getOrNull()
if(WorldStateData.useCM){
val sender = kotlin.runCatching {
this.toCommandSender()
}.getOrNull() ?: return@subscribeAlways

if (sender != null) {
WorldState.launch { // Async
runCatching {
CommandManager.executeCommand(sender, message)
}
}
}

}
/*
Thread {
//加载数据库
logger.info("Plugin loaded!")
globalEventChannel().subscribeAlways<MessageEvent> { event ->
val messageContent = event.message.contentToString()
val instruction = MessageUtils.getUrlByEnum(messageContent)
val host = instruction?.url
if (host != null) {
logger.info { "${event.senderName} 查询 $host" }
var response:String? = null
try {
response = OkHttpUtils.doGet(host)
}catch (e:Exception){
logger.error(e)
}
if (response != null) {
event.subject.sendMessage(PlainText(response))
} else {
event.subject.sendMessage(PlainText("暂时无法查询,请访问\n$host"))
}
SaveDataService.storage(event.sender,instruction)
}
if (messageContent == HELP_KEY) {
val messageChain = MessageChainBuilder()
.append(PlainText("warframe-world-state插件功能如下\n"))
.append(PlainText(BeginWithKeyword.getHelpMenu()))
.append(PlainText("\n"))
.append(PlainText(WorldStateKey.getHelpMenu()))
.append(PlainText("\n"))
.append(PlainText(DatabaseKey.getHelpMenu()))
.build()
event.subject.sendMessage(messageChain)
}
val dbKey = MessageUtils.getDatabaseEnum(messageContent)
if (dbKey != null){
when(dbKey) {
DatabaseKey.USER_LIST -> UserDao.userList(event)
DatabaseKey.KEY_TOP -> StatisticalService.queryKeyTop(event)
}
}
}
globalEventChannel().subscribeAlways<MessageRecallEvent> { event ->
logger.info { "${event.authorId} 的消息被撤回了" }
}
}.start()
*/
}

/**
* Will be invoked when the plugin is disabled
*/
override fun onDisable() {
logger.info { "Plugin unloaded" }

}

private suspend fun initDatabase(){
withContext(Dispatchers.Default){
logger.info("初始化数据库 - 开始")
DBUtils.initTableIfNotExist()
logger.info("初始化数据库 - 结束")
}
}
}
30 changes: 30 additions & 0 deletions src/main/kotlin/top/wsure/warframe/command/ChatCommandEditor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package top.wsure.warframe.command

import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.ConsoleCommandSender.sendMessage
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.isConsole
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.message.data.PlainText
import top.wsure.warframe.WorldState
import top.wsure.warframe.data.WorldStateData

object ChatCommandEditor: SimpleCommand(
WorldState,
"chat-command",
"聊天命令","cm","chatCommand",
description = "聊天命令转发设置"
) {
@ExperimentalCommandDescriptors
@ConsoleExperimentalApi
override val prefixOptional = true

@Handler
suspend fun CommandSender.handle(useCM:Boolean) { // 函数名随意, 但参数需要按顺序放置.
if (isConsole()) {
WorldStateData.useCM = useCM
sendMessage(PlainText("设置使用聊天命令${if (WorldStateData.useCM) "开启,可以在聊天中使用命令" else "关闭,再次开启需要在控制台执行"}"))
}
}
}
28 changes: 0 additions & 28 deletions src/main/kotlin/top/wsure/warframe/command/DatabaseCommand.kt

This file was deleted.

1 change: 0 additions & 1 deletion src/main/kotlin/top/wsure/warframe/command/WFHelp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import top.wsure.warframe.WorldState
import top.wsure.warframe.data.WorldStateData
import top.wsure.warframe.utils.CommandUtils

Expand Down
14 changes: 0 additions & 14 deletions src/main/kotlin/top/wsure/warframe/command/WFParamCommand.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package top.wsure.warframe.command

import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.RawCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.isNotConsole
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.User
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.content
import net.mamoe.mirai.utils.MiraiLogger
import top.wsure.warframe.data.RemoteCommand
import top.wsure.warframe.data.WorldStateData
import top.wsure.warframe.service.SaveDataService
import top.wsure.warframe.utils.CommandUtils
import top.wsure.warframe.utils.MessageUtils

/**
* 这是一个包含参数的指令
Expand All @@ -43,15 +38,6 @@ class WFParamCommand(
val msg = args.joinToString(" ") { it.content }
val remoteUrl = WorldStateData.host + command.path + msg
logger.info("${this.user?.nick} 查询 $remoteUrl")
if(isNotConsole()){
SaveDataService.storage(
this.user ?: this.bot as User, MessageUtils.Instruction(
command.alia,
msg,
remoteUrl
)
)
}
sendMessage(CommandUtils.getRemoteResponse(remoteUrl,this.bot,this.user))
}

Expand Down
13 changes: 0 additions & 13 deletions src/main/kotlin/top/wsure/warframe/command/WFSimpleCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.isNotConsole
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.User
import net.mamoe.mirai.utils.MiraiLogger
import top.wsure.warframe.data.RemoteCommand
import top.wsure.warframe.data.WorldStateData
import top.wsure.warframe.service.SaveDataService
import top.wsure.warframe.utils.CommandUtils
import top.wsure.warframe.utils.MessageUtils

/**
* 一个简单的简单参数注册类
Expand All @@ -38,15 +34,6 @@ class WFSimpleCommand(
suspend fun CommandSender.handle() { // 函数名随意, 但参数需要按顺序放置.
val remoteUrl = WorldStateData.host + command.path
logger.info("${this.user?.nick} 查询 $remoteUrl")
if(isNotConsole()){
SaveDataService.storage(
this.user ?: this.bot as User, MessageUtils.Instruction(
command.alia,
command.name,
remoteUrl
)
)
}
sendMessage(CommandUtils.getRemoteResponse(remoteUrl,this.bot,this.user))
}

Expand Down
40 changes: 0 additions & 40 deletions src/main/kotlin/top/wsure/warframe/dao/GroupDao.kt

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/kotlin/top/wsure/warframe/dao/SearchRecordDao.kt

This file was deleted.

Loading

0 comments on commit 17f3362

Please sign in to comment.