diff --git a/.gitignore b/.gitignore index d4fea68..93befee 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ logs/ device.json gradle/ + +bots/ diff --git a/README.md b/README.md index f3d49d3..3cb4db3 100644 --- a/README.md +++ b/README.md @@ -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`分支 diff --git a/build.gradle b/build.gradle index bb35e58..4d81cf4 100644 --- a/build.gradle +++ b/build.gradle @@ -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" @@ -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 diff --git a/src/main/kotlin/top/wsure/warframe/WorldState.kt b/src/main/kotlin/top/wsure/warframe/WorldState.kt index 9a8e83f..65e3db7 100644 --- a/src/main/kotlin/top/wsure/warframe/WorldState.kt +++ b/src/main/kotlin/top/wsure/warframe/WorldState.kt @@ -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 @@ -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) @@ -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() { @@ -54,81 +44,24 @@ object WorldState : KotlinPlugin( WFHelp(WorldState,WorldStateData.helpKey).register() - DatabaseCommand.register() + ChatCommandEditor.register() AbstractPermitteeId.AnyContact.permit(WorldState.parentPermission) - - initDatabase() } globalEventChannel().subscribeAlways { - 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 { 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 { event -> - logger.info { "${event.authorId} 的消息被撤回了" } - } - }.start() -*/ } /** @@ -136,14 +69,5 @@ object WorldState : KotlinPlugin( */ override fun onDisable() { logger.info { "Plugin unloaded" } - - } - - private suspend fun initDatabase(){ - withContext(Dispatchers.Default){ - logger.info("初始化数据库 - 开始") - DBUtils.initTableIfNotExist() - logger.info("初始化数据库 - 结束") - } } } \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/command/ChatCommandEditor.kt b/src/main/kotlin/top/wsure/warframe/command/ChatCommandEditor.kt new file mode 100644 index 0000000..e93ae55 --- /dev/null +++ b/src/main/kotlin/top/wsure/warframe/command/ChatCommandEditor.kt @@ -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 "关闭,再次开启需要在控制台执行"}")) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/command/DatabaseCommand.kt b/src/main/kotlin/top/wsure/warframe/command/DatabaseCommand.kt deleted file mode 100644 index 80b2311..0000000 --- a/src/main/kotlin/top/wsure/warframe/command/DatabaseCommand.kt +++ /dev/null @@ -1,28 +0,0 @@ -package top.wsure.warframe.command - -import net.mamoe.mirai.console.command.CommandSender -import net.mamoe.mirai.console.command.CompositeCommand -import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors -import net.mamoe.mirai.console.util.ConsoleExperimentalApi -import top.wsure.warframe.WorldState -import top.wsure.warframe.dao.UserDao -import top.wsure.warframe.service.StatisticalService - -object DatabaseCommand : CompositeCommand( - WorldState,"db","统计",description = "数据库统计" -){ - @ExperimentalCommandDescriptors - @ConsoleExperimentalApi - override val prefixOptional = true - - @SubCommand("热门查询") - suspend fun CommandSender.top(){ - sendMessage(StatisticalService.queryKeyTop()) - } - - @SubCommand("用户列表") - suspend fun CommandSender.userList(){ - sendMessage(UserDao.userList(this.user)) - } - -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/command/WFHelp.kt b/src/main/kotlin/top/wsure/warframe/command/WFHelp.kt index 85121f6..63b79fc 100644 --- a/src/main/kotlin/top/wsure/warframe/command/WFHelp.kt +++ b/src/main/kotlin/top/wsure/warframe/command/WFHelp.kt @@ -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 diff --git a/src/main/kotlin/top/wsure/warframe/command/WFParamCommand.kt b/src/main/kotlin/top/wsure/warframe/command/WFParamCommand.kt index 2c21179..2395da2 100644 --- a/src/main/kotlin/top/wsure/warframe/command/WFParamCommand.kt +++ b/src/main/kotlin/top/wsure/warframe/command/WFParamCommand.kt @@ -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 /** * 这是一个包含参数的指令 @@ -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)) } diff --git a/src/main/kotlin/top/wsure/warframe/command/WFSimpleCommand.kt b/src/main/kotlin/top/wsure/warframe/command/WFSimpleCommand.kt index 9be4db7..0b8479c 100644 --- a/src/main/kotlin/top/wsure/warframe/command/WFSimpleCommand.kt +++ b/src/main/kotlin/top/wsure/warframe/command/WFSimpleCommand.kt @@ -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 /** * 一个简单的简单参数注册类 @@ -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)) } diff --git a/src/main/kotlin/top/wsure/warframe/dao/GroupDao.kt b/src/main/kotlin/top/wsure/warframe/dao/GroupDao.kt deleted file mode 100644 index 3f591ce..0000000 --- a/src/main/kotlin/top/wsure/warframe/dao/GroupDao.kt +++ /dev/null @@ -1,40 +0,0 @@ -package top.wsure.warframe.dao - -import net.mamoe.mirai.contact.Group -import org.joda.time.DateTime -import top.wsure.warframe.entity.GroupEntity - -/** - * FileName: GroupService - * Author: wsure - * Date: 2021/2/5 4:17 下午 - * Description: - */ -object GroupDao { - fun saveGroup(group:Group){ - val exist = GroupEntity.findById(group.id) - if(exist == null){ - insert(group) - } else { - updateGroupEntity(group, exist) - } - } - - private fun updateGroupEntity(group: Group, exist: GroupEntity): GroupEntity { - exist.name = group.name - exist.remark = group.owner.remark - exist.avatarUrl = group.avatarUrl - exist.updateDate = DateTime.now() - return exist - } - - private fun insert(group: Group): GroupEntity { - return GroupEntity.new(group.id) { - name = group.name - remark = group.owner.remark - avatarUrl = group.avatarUrl - createDate = DateTime.now() - updateDate = DateTime.now() - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/dao/SearchRecordDao.kt b/src/main/kotlin/top/wsure/warframe/dao/SearchRecordDao.kt deleted file mode 100644 index d0332fa..0000000 --- a/src/main/kotlin/top/wsure/warframe/dao/SearchRecordDao.kt +++ /dev/null @@ -1,27 +0,0 @@ -package top.wsure.warframe.dao - -import net.mamoe.mirai.contact.User -import org.joda.time.DateTime -import top.wsure.warframe.entity.SearchRecordEntity -import top.wsure.warframe.utils.MessageUtils - -/** - * FileName: SearchRecordDao - * Author: wsure - * Date: 2021/2/5 6:00 下午 - * Description: - */ -object SearchRecordDao { - - fun insert(user: User, instruction: MessageUtils.Instruction):SearchRecordEntity{ - return SearchRecordEntity.new { - userId = user.id - nick = user.nick - keyWord = instruction.keyWord - param = instruction.param - url = instruction.url - createDate = DateTime.now() - updateDate = DateTime.now() - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/dao/UserDao.kt b/src/main/kotlin/top/wsure/warframe/dao/UserDao.kt deleted file mode 100644 index 4567b46..0000000 --- a/src/main/kotlin/top/wsure/warframe/dao/UserDao.kt +++ /dev/null @@ -1,74 +0,0 @@ -package top.wsure.warframe.dao - -import net.mamoe.mirai.contact.Contact -import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.User -import net.mamoe.mirai.event.events.MessageEvent -import net.mamoe.mirai.message.data.Message -import net.mamoe.mirai.message.data.MessageChainBuilder -import net.mamoe.mirai.message.data.PlainText -import org.jetbrains.exposed.sql.StdOutSqlLogger -import org.jetbrains.exposed.sql.addLogger -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction -import org.joda.time.DateTime -import top.wsure.warframe.WorldState -import top.wsure.warframe.entity.UserEntity -import top.wsure.warframe.entity.UserTable -import top.wsure.warframe.utils.DBUtils - -/** - * FileName: UserService - * Author: wsure - * Date: 2021/2/2 2:47 下午 - * Description: - */ -object UserDao { - fun saveUser(user: User) { - val exist = UserEntity.findById(user.id) - if (exist == null) { - insert(user) - } else { - updateUserEntity(user, exist) - } - } - - fun userList(user: User?) :Message{ - return MessageChainBuilder() - .append(PlainText("用户列表\n")) - .append(PlainText(users(user).joinToString("\n"))) - .build() - } - - private fun users(user: User?): List { - return transaction(DBUtils.getDatabase(WorldState.DB_FILE)) { - addLogger(StdOutSqlLogger) - UserTable.select { - if (user is Member) - UserTable.id.inList(user.group.members.map { it.id }) - else - UserTable.id.isNotNull() - }.map { "[${it[UserTable.id]}]: ${it[UserTable.nick]}" } - } - } - - private fun insert(user: User): UserEntity { - return UserEntity.new(user.id) { - nick = user.nick - remark = user.remark - avatarUrl = user.avatarUrl - createDate = DateTime.now() - updateDate = DateTime.now() - } - } - - private fun updateUserEntity(user: User, exist: UserEntity): UserEntity { - - exist.nick = user.nick - exist.remark = user.remark - exist.avatarUrl = user.avatarUrl - exist.updateDate = DateTime.now() - return exist - } - -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/data/WorldStateData.kt b/src/main/kotlin/top/wsure/warframe/data/WorldStateData.kt index 91b83d6..f59a9af 100644 --- a/src/main/kotlin/top/wsure/warframe/data/WorldStateData.kt +++ b/src/main/kotlin/top/wsure/warframe/data/WorldStateData.kt @@ -2,8 +2,6 @@ package top.wsure.warframe.data import net.mamoe.mirai.console.data.AutoSavePluginConfig import net.mamoe.mirai.console.data.value -import top.wsure.warframe.WorldState -import top.wsure.warframe.utils.CommandUtils /** * FileName: WorldStateData @@ -12,6 +10,8 @@ import top.wsure.warframe.utils.CommandUtils * Description: */ object WorldStateData : AutoSavePluginConfig("WorldStateData") { + + var useCM: Boolean by value(true) //api host 默认使用自身的,可自己改 var host: String by value("http://nymph.rbq.life:3000") @@ -19,8 +19,4 @@ object WorldStateData : AutoSavePluginConfig("WorldStateData") { var helpKey: String by value("菜单") var commandList: List by value(emptyList()) - -// init { -// commandList = CommandUtils.getRemoteCommand(host) -// } } \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/entity/GroupEntity.kt b/src/main/kotlin/top/wsure/warframe/entity/GroupEntity.kt deleted file mode 100644 index fe7cdf4..0000000 --- a/src/main/kotlin/top/wsure/warframe/entity/GroupEntity.kt +++ /dev/null @@ -1,30 +0,0 @@ -package top.wsure.warframe.entity - -import org.jetbrains.exposed.dao.LongEntity -import org.jetbrains.exposed.dao.LongEntityClass -import org.jetbrains.exposed.dao.id.EntityID -import org.jetbrains.exposed.dao.id.LongIdTable -import org.jetbrains.exposed.sql.jodatime.datetime - -/** - * FileName: GroupEntity - * Author: wsure - * Date: 2021/1/29 11:30 上午 - * Description: - */ -class GroupEntity(id:EntityID) :LongEntity(id) { - companion object : LongEntityClass(GroupTable) - var name by GroupTable.name - var remark by GroupTable.remark - var avatarUrl by GroupTable.avatarUrl - var createDate by GroupTable.createDate - var updateDate by GroupTable.updateDate -} - -object GroupTable :LongIdTable("QQ_GROUP") { - var name = text("name").nullable() - var remark = text("remark").nullable() - var avatarUrl = text("avatar_url").nullable() - val createDate = datetime("create_date") - val updateDate = datetime("update_date") -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/entity/SearchRecordEntity.kt b/src/main/kotlin/top/wsure/warframe/entity/SearchRecordEntity.kt deleted file mode 100644 index baa4958..0000000 --- a/src/main/kotlin/top/wsure/warframe/entity/SearchRecordEntity.kt +++ /dev/null @@ -1,39 +0,0 @@ -package top.wsure.warframe.entity - -import org.jetbrains.exposed.dao.LongEntity -import org.jetbrains.exposed.dao.LongEntityClass -import org.jetbrains.exposed.dao.id.EntityID -import org.jetbrains.exposed.dao.id.LongIdTable -import org.jetbrains.exposed.sql.jodatime.datetime -import org.joda.time.DateTime - -/** - * FileName: SearchRecordEntity - * Author: wsure - * Date: 2021/2/5 5:51 下午 - * Description: - */ -class SearchRecordEntity(id: EntityID) : LongEntity(id) { - companion object : LongEntityClass(SearchRecordTable) - - var userId by SearchRecordTable.userId - var groupId by SearchRecordTable.groupId - var nick by SearchRecordTable.nick - var keyWord by SearchRecordTable.keyWord - var param by SearchRecordTable.param - var url by SearchRecordTable.url - var createDate by SearchRecordTable.createDate - var updateDate by SearchRecordTable.updateDate -} - -object SearchRecordTable : LongIdTable("SEARCH_RECORD") { - - var userId = long("user_id") - var groupId = long("group_id").nullable() - var nick = text("nick").nullable() - var keyWord = text("key_word").nullable() - var param = text("param").nullable() - var url = text("url").nullable() - val createDate = datetime("create_date").default(DateTime.now()) - val updateDate = datetime("update_date").default(DateTime.now()) -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/entity/UserEntity.kt b/src/main/kotlin/top/wsure/warframe/entity/UserEntity.kt deleted file mode 100644 index 58deb98..0000000 --- a/src/main/kotlin/top/wsure/warframe/entity/UserEntity.kt +++ /dev/null @@ -1,33 +0,0 @@ -package top.wsure.warframe.entity - -import org.jetbrains.exposed.dao.LongEntity -import org.jetbrains.exposed.dao.LongEntityClass -import org.jetbrains.exposed.dao.id.EntityID -import org.jetbrains.exposed.dao.id.LongIdTable -import org.jetbrains.exposed.sql.jodatime.datetime - -/** - - * FileName: User - * Author: wsure - * Date: 2021/1/22 12:10 上午 - * Description:user - */ - -class UserEntity(id: EntityID) : LongEntity(id) { - companion object : LongEntityClass(UserTable) - - var nick by UserTable.nick - var remark by UserTable.remark - var avatarUrl by UserTable.avatarUrl - var createDate by UserTable.createDate - var updateDate by UserTable.updateDate -} - -object UserTable : LongIdTable("USER") { - var nick = text("nick").nullable().default(null) - var remark = text("remark").nullable() - var avatarUrl = text("avatar_url").nullable() - val createDate = datetime("create_date") - val updateDate = datetime("update_date") -} diff --git a/src/main/kotlin/top/wsure/warframe/enums/BeginWithKeyword.kt b/src/main/kotlin/top/wsure/warframe/enums/BeginWithKeyword.kt deleted file mode 100644 index a99b5e5..0000000 --- a/src/main/kotlin/top/wsure/warframe/enums/BeginWithKeyword.kt +++ /dev/null @@ -1,23 +0,0 @@ -package top.wsure.warframe.enums - -/** - * FileName: BeginWithKeyword - * Author: wsure - * Date: 2020/9/25 9:29 上午 - * Description: - */ -enum class BeginWithKeyword(val path:String) { - wm("wm"), - rm("rm"), - wiki("wiki"), - tran("dict/tran"), - ; - companion object { - fun getByStartWith(message:String):BeginWithKeyword ?{ - return values().firstOrNull { keyword -> message.startsWith(keyword.name) } - } - fun getHelpMenu():String { - return values().joinToString("\n") { e -> "${e.name} 搜索词" } - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/enums/DatabaseKey.kt b/src/main/kotlin/top/wsure/warframe/enums/DatabaseKey.kt deleted file mode 100644 index 3a47836..0000000 --- a/src/main/kotlin/top/wsure/warframe/enums/DatabaseKey.kt +++ /dev/null @@ -1,26 +0,0 @@ -package top.wsure.warframe.enums - -/** - * FileName: DatabaseKey - * Author: wsure - * Date: 2021/2/2 7:33 下午 - * Description: - */ -enum class DatabaseKey(val keyWord: String) { - USER_LIST("用户列表"), -// GROUP_LIST("群列表"), -// GROUP_TOP("本群排行"), - KEY_TOP("热门查询"), -// RECORD_STATISTICAL("插件统计"), - ; - companion object { - - fun getByKeyWord(key:String): DatabaseKey? { - return values().firstOrNull { e -> e.keyWord == key } - } - - fun getHelpMenu():String { - return values().joinToString("\n") { e -> "db ${e.keyWord}" } - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/enums/WorldStateKey.kt b/src/main/kotlin/top/wsure/warframe/enums/WorldStateKey.kt deleted file mode 100644 index e5d3c0a..0000000 --- a/src/main/kotlin/top/wsure/warframe/enums/WorldStateKey.kt +++ /dev/null @@ -1,43 +0,0 @@ -package top.wsure.warframe.enums - -/** - * FileName: WorldStateKey - * Author: wsure - * Date: 2020/9/24 10:26 上午 - * Description:世界状态 - */ - -enum class WorldStateKey(val keyWord: String) { - news("新闻"), - events("事件"), - alerts("警报"), - sortie("突击"), - Ostrons("地球赏金"), - Solaris("金星赏金"), - EntratiSyndicate("火卫二赏金"), - fissures("裂缝"), - flashSales("促销商品"), - invasions("入侵"), - voidTrader("奸商"), - dailyDeals("特价"), - persistentEnemies("小小黑"), - earthCycle("地球"), - cetusCycle("地球平原"), - cambionCycle("火卫二平原"), - vallisCycle("金星平原"), - nightwave("电波"), - arbitration("仲裁"), - ; - - companion object { - - fun getByKeyWord(key:String): WorldStateKey? { - return values().firstOrNull { e -> e.keyWord == key } - } - - fun getHelpMenu():String { - return values().joinToString("\n") { e -> e.keyWord } - } - } - -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/service/SaveDataService.kt b/src/main/kotlin/top/wsure/warframe/service/SaveDataService.kt deleted file mode 100644 index 1217cac..0000000 --- a/src/main/kotlin/top/wsure/warframe/service/SaveDataService.kt +++ /dev/null @@ -1,33 +0,0 @@ -package top.wsure.warframe.service - -import net.mamoe.mirai.contact.Group -import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.User -import org.jetbrains.exposed.sql.transactions.transaction -import top.wsure.warframe.WorldState -import top.wsure.warframe.dao.GroupDao -import top.wsure.warframe.dao.SearchRecordDao -import top.wsure.warframe.dao.UserDao -import top.wsure.warframe.utils.DBUtils -import top.wsure.warframe.utils.MessageUtils - -/** - * FileName: SaveDataService - * Author: wsure - * Date: 2021/2/5 5:43 下午 - * Description: - */ -object SaveDataService { - - fun storage(user: User, instruction: MessageUtils.Instruction) { - transaction(DBUtils.getDatabase(WorldState.DB_FILE)) { - UserDao.saveUser(user) - val record = SearchRecordDao.insert(user, instruction) - if (user is Member) { - val group: Group = user.group - record.groupId = group.id - GroupDao.saveGroup(group) - } - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/service/StatisticalService.kt b/src/main/kotlin/top/wsure/warframe/service/StatisticalService.kt deleted file mode 100644 index 2743e31..0000000 --- a/src/main/kotlin/top/wsure/warframe/service/StatisticalService.kt +++ /dev/null @@ -1,69 +0,0 @@ -package top.wsure.warframe.service - -import net.mamoe.mirai.contact.Contact -import net.mamoe.mirai.event.events.MessageEvent -import net.mamoe.mirai.message.data.Message -import net.mamoe.mirai.message.data.MessageChainBuilder -import net.mamoe.mirai.message.data.PlainText -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.transactions.transaction -import top.wsure.warframe.WorldState -import top.wsure.warframe.entity.SearchRecordTable -import top.wsure.warframe.enums.DatabaseKey -import top.wsure.warframe.enums.WorldStateKey -import top.wsure.warframe.utils.DBUtils -import java.time.LocalDateTime - -/** - * FileName: StatisticalService - * Author: wsure - * Date: 2021/2/6 5:11 下午 - * Description: - */ -object StatisticalService { - - private const val COUNT_COLUMN:String = "count_" - - fun queryKeyTop(): Message { - return MessageChainBuilder() - .append(PlainText("${DatabaseKey.KEY_TOP.keyWord}(Top5)\n功能\t参数\t次数\n")) - .append(PlainText(queryTopSearch(null, null, null).joinToString("\n") { - "${it.keyWord}\t${ - if (it.keyWord.equals("wf")) WorldStateKey.valueOf(it.param!!).keyWord else it.param - }\t${it.count}" - })) - .build() - } - - fun queryTopSearch(groupId: Long?, start: LocalDateTime?, end: LocalDateTime?): List { - - return transaction(DBUtils.getDatabase(WorldState.DB_FILE)) { - addLogger(StdOutSqlLogger) - val query = SearchRecordTable - .slice(SearchRecordTable.keyWord, SearchRecordTable.param, SearchRecordTable.id.count().alias(COUNT_COLUMN)) - .selectAll() - .groupBy(SearchRecordTable.keyWord, SearchRecordTable.param) - .orderBy( SearchRecordTable.id.count().alias(COUNT_COLUMN) to SortOrder.DESC) - .limit(5,0) - - if (groupId != null) - query.andWhere { SearchRecordTable.groupId eq groupId } - if (start != null && end != null) - query.andWhere { SearchRecordTable.createDate.between(start,end) } - - query.map { - TopSearchBo( - it[SearchRecordTable.keyWord], - it[SearchRecordTable.param], - it[SearchRecordTable.id.count().alias(COUNT_COLUMN)] - ) - } - } - } - - data class TopSearchBo( - val keyWord: String?, - val param: String?, - val count: Long - ) -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/utils/CommandUtils.kt b/src/main/kotlin/top/wsure/warframe/utils/CommandUtils.kt index 396209a..a77c453 100644 --- a/src/main/kotlin/top/wsure/warframe/utils/CommandUtils.kt +++ b/src/main/kotlin/top/wsure/warframe/utils/CommandUtils.kt @@ -1,18 +1,10 @@ package top.wsure.warframe.utils import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.command.Command -import net.mamoe.mirai.console.command.CommandExecuteResult -import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register -import net.mamoe.mirai.console.command.CommandSender -import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors -import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.contact.User import net.mamoe.mirai.message.data.Message -import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.PlainText import net.mamoe.mirai.message.data.content import net.mamoe.mirai.utils.MiraiLogger @@ -21,7 +13,6 @@ import top.wsure.warframe.command.WFParamCommand import top.wsure.warframe.command.WFSimpleCommand import top.wsure.warframe.data.CommandType import top.wsure.warframe.data.RemoteCommand -import top.wsure.warframe.enums.DatabaseKey /** * FileName: CommandUtils @@ -74,66 +65,12 @@ class CommandUtils { } }.forEach { it.register() } } - @ConsoleExperimentalApi - @ExperimentalCommandDescriptors - suspend fun handleCommand(sender: CommandSender, message: MessageChain) { - - fun isDebugging(command: Command?): Boolean { - /* - if (command?.prefixOptional == false || message.content.startsWith(CommandManager.commandPrefix)) { - if (MiraiConsoleImplementationBridge.loggerController.shouldLog("console.debug", SimpleLogger.LogPriority.DEBUG)) { - return true - } - }*/ - return false - } - - when (val result = CommandManager.executeCommand(sender, message)) { - is CommandExecuteResult.PermissionDenied -> { - if (isDebugging(result.command)) { - sender.sendMessage("权限不足. ${CommandManager.commandPrefix}${result.command.primaryName} 需要权限 ${result.command.permission.id}.") - // intercept() - } - } - is CommandExecuteResult.IllegalArgument -> { - result.exception.message?.let { sender.sendMessage(it) } - // intercept() - } - is CommandExecuteResult.Success -> { - // intercept() - } - is CommandExecuteResult.ExecutionFailed -> { - val owner = result.command.owner - val (logger, printOwner) = when (owner) { - is JvmPlugin -> owner.logger to false - else -> MiraiConsole.mainLogger to true - } - logger.warning( - "Exception in executing command `$message`" + if (printOwner) ", command owned by $owner" else "", - result.exception - ) - // intercept() - } - is CommandExecuteResult.Intercepted -> { - if (isDebugging(result.command)) { - sender.sendMessage("指令执行被拦截, 原因: ${result.reason}") - } - } - is CommandExecuteResult.UnmatchedSignature, - is CommandExecuteResult.UnresolvedCommand, - -> { - // noop - } - } - - } - fun getRemoteCommandHelp(commandList: List): Message { return PlainText("warframe-world-state插件功能如下\n${ commandList.joinToString("\n") { "${it.name}${if (it.type == CommandType.PARAM) " <搜索词>" else ""}" } - }\n${DatabaseKey.getHelpMenu()}") + }") } } diff --git a/src/main/kotlin/top/wsure/warframe/utils/DBUtils.kt b/src/main/kotlin/top/wsure/warframe/utils/DBUtils.kt deleted file mode 100644 index 931ec74..0000000 --- a/src/main/kotlin/top/wsure/warframe/utils/DBUtils.kt +++ /dev/null @@ -1,74 +0,0 @@ -package top.wsure.warframe.utils - -import org.jetbrains.exposed.sql.Database -import org.jetbrains.exposed.sql.SchemaUtils -import org.jetbrains.exposed.sql.StdOutSqlLogger -import org.jetbrains.exposed.sql.addLogger -import org.jetbrains.exposed.sql.transactions.transaction -import top.wsure.warframe.WorldState -import top.wsure.warframe.entity.UserTable -import java.io.File - - -/** - - * FileName: SqliteUtils - * Author: wsure - * Date: 2021/1/21 5:10 下午 - * Description: - */ -class DBUtils { - companion object { - private const val username: String = "Wsure" - private const val password: String = "Wsure" - private const val driver = "org.h2.Driver" - - fun getDatabase(file: File): Database { - val url = "jdbc:h2:file:${file.absolutePath};AUTO_SERVER=TRUE" - return Database.connect( - url = url, - driver = driver, - user = username, - password = password - ) - } - - fun initTableIfNotExist() { - - val createTablesSql = "create table if not exists user(\n" + - " id BIGINT(20) primary key NOT NULL ,\n" + - " nick TEXT DEFAULT NULL,\n" + - " remark TEXT DEFAULT NULL,\n" + - " avatar_url TEXT DEFAULT NULL,\n" + - " create_date DATETIME DEFAULT NULL,\n" + - " update_date DATETIME DEFAULT NULL\n" + - ");\n" + - "create table if not exists qq_group(\n" + - " id BIGINT(20) primary key NOT NULL ,\n" + - " name TEXT DEFAULT NULL,\n" + - " remark TEXT DEFAULT NULL,\n" + - " avatar_url TEXT DEFAULT NULL,\n" + - " create_date DATETIME DEFAULT NULL,\n" + - " update_date DATETIME DEFAULT NULL\n" + - ");\n" + - "create table if not exists search_record(\n" + - " id BIGINT(20) primary key auto_increment NOT NULL ,\n" + - " user_id BIGINT(20) NOT NULL ,\n" + - " group_id BIGINT(20) DEFAULT NULL ,\n" + - " nick TEXT DEFAULT NULL,\n" + - " key_word TEXT DEFAULT NULL,\n" + - " param TEXT DEFAULT NULL,\n" + - " url TEXT DEFAULT NULL,\n" + - " create_date DATETIME DEFAULT NULL,\n" + - " update_date DATETIME DEFAULT NULL\n" + - ");" - transaction(getDatabase(WorldState.DB_FILE)) { - SchemaUtils.create(UserTable) - addLogger(StdOutSqlLogger) - exec(createTablesSql) - } - } - - } - -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/utils/MessageUtils.kt b/src/main/kotlin/top/wsure/warframe/utils/MessageUtils.kt deleted file mode 100644 index fd8992f..0000000 --- a/src/main/kotlin/top/wsure/warframe/utils/MessageUtils.kt +++ /dev/null @@ -1,61 +0,0 @@ -package top.wsure.warframe.utils - -import top.wsure.warframe.enums.BeginWithKeyword -import top.wsure.warframe.enums.DatabaseKey -import top.wsure.warframe.enums.WorldStateKey - -/** - * FileName: MessageUtils - * Author: wsure - * Date: 2020/9/25 9:59 上午 - * Description: - */ -class MessageUtils { - companion object { - private const val NYMPH_HOST = "http://nymph.rbq.life:3000" - - private const val DB_KEY = "db" - - fun getUrlByEnum(messageContent: String):Instruction?{ - val message = messageContent.trim() - val worldStateKey = WorldStateKey.getByKeyWord(message) - val beginWithKeyword = BeginWithKeyword.getByStartWith(message) - return when { - worldStateKey != null -> Instruction("wf",worldStateKey.name,"${NYMPH_HOST}/wf/robot/${worldStateKey.name}") - - beginWithKeyword != null -> { - val param = getParam(beginWithKeyword,message) - if(param != null) - Instruction(beginWithKeyword.name,param,"${NYMPH_HOST}/${beginWithKeyword.path}/robot/${param}") - else - null - } - - else -> null - } - } - - private fun getParam(beginWithKeyword: BeginWithKeyword, message:String):String?{ - val param = message.removePrefix(beginWithKeyword.name).trim() - return when { - param.isBlank() -> null - else -> param - } - } - - fun getDatabaseEnum(message:String):DatabaseKey?{ - var dbKey:DatabaseKey? = null - if (message.startsWith("$DB_KEY ")){ - val keyword = message.removePrefix("$DB_KEY ").trim() - dbKey = DatabaseKey.getByKeyWord(keyword) - } - return dbKey - } - } - - data class Instruction( - val keyWord:String, - val param:String, - val url:String, - ) -} \ No newline at end of file diff --git a/src/main/kotlin/top/wsure/warframe/utils/OkHttpUtils.kt b/src/main/kotlin/top/wsure/warframe/utils/OkHttpUtils.kt index 38c84c5..a4832df 100644 --- a/src/main/kotlin/top/wsure/warframe/utils/OkHttpUtils.kt +++ b/src/main/kotlin/top/wsure/warframe/utils/OkHttpUtils.kt @@ -7,8 +7,6 @@ import kotlinx.serialization.json.Json import net.mamoe.mirai.utils.MiraiLogger import okhttp3.OkHttpClient import okhttp3.Request -import top.wsure.warframe.data.RemoteCommand -import top.wsure.warframe.utils.OkHttpUtils.Companion.doGetObject import java.io.InputStream import java.util.concurrent.TimeUnit