Skip to content

Commit d25004b

Browse files
committed
Add more messages & Add more storage method
1 parent fefe30d commit d25004b

File tree

11 files changed

+240
-43
lines changed

11 files changed

+240
-43
lines changed

build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io.izzel.taboolib.gradle.Basic
22
import io.izzel.taboolib.gradle.Bukkit
3+
import io.izzel.taboolib.gradle.Database
34
import io.izzel.taboolib.gradle.Velocity
45
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
56

@@ -12,7 +13,7 @@ plugins {
1213
taboolib {
1314
env {
1415
// 安装模块
15-
install(Bukkit, Basic, Velocity)
16+
install(Bukkit, Basic, Velocity, Database)
1617
}
1718
description {
1819
contributors {

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=top.alazeprt.aqqbot
2-
version=1.0.4
2+
version=1.0.5-beta
33
kotlin.incremental=true
44
kotlin.incremental.java=true
55
kotlin.caching.enabled=true

src/main/kotlin/top/alazeprt/aqqbot/AQQBot.kt

+50-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import taboolib.common.platform.function.info
66
import taboolib.common.platform.function.releaseResourceFile
77
import taboolib.common.platform.function.submit
88
import taboolib.module.configuration.Configuration
9+
import taboolib.module.database.*
910
import top.alazeprt.aonebot.BotClient
1011
import top.alazeprt.aonebot.action.SendGroupMessage
1112
import top.alazeprt.aqqbot.qq.BotListener
1213
import java.io.File
1314
import java.net.URI
15+
import javax.sql.DataSource
1416

1517
object AQQBot : Plugin() {
1618

@@ -32,6 +34,12 @@ object AQQBot : Plugin() {
3234

3335
val dataMap: MutableMap<String, String> = mutableMapOf()
3436

37+
lateinit var table: Table<*, *>
38+
39+
lateinit var dataSource: DataSource
40+
41+
var isFileStorage: Boolean = false
42+
3543
override fun onActive() {
3644
info("Checking server type...")
3745
try {
@@ -44,6 +52,42 @@ object AQQBot : Plugin() {
4452
config = Configuration.loadFromFile(configFile)
4553
val dataFile = releaseResourceFile("data.yml", replace = false)
4654
dataConfig = Configuration.loadFromFile(dataFile)
55+
if (config.getString("storage.type")!!.lowercase() == "file") isFileStorage = true
56+
else if (config.getString("storage.type")!!.lowercase() == "sqlite") {
57+
val host = HostSQLite(File(getDataFolder(), config.getString("storage.sqlite.file")?: "aqqbot.db"))
58+
val dataSource by lazy { host.createDataSource() }
59+
table = Table("account_data", host) {
60+
add("userId") {
61+
type(ColumnTypeSQLite.INTEGER) {
62+
options(ColumnOptionSQLite.PRIMARY_KEY)
63+
}
64+
}
65+
add("name") {
66+
type(ColumnTypeSQLite.TEXT) {
67+
options(ColumnOptionSQLite.NOTNULL)
68+
}
69+
}
70+
}
71+
AQQBot.dataSource = dataSource
72+
table.createTable(dataSource)
73+
} else if (config.getString("storage.type")!!.lowercase() == "mysql") {
74+
val host = config.getHost("storage.mysql")
75+
val dataSource by lazy { host.createDataSource() }
76+
table = Table("account_data", host) {
77+
add("userId") {
78+
type(ColumnTypeSQL.BIGINT) {
79+
options(ColumnOptionSQL.PRIMARY_KEY)
80+
}
81+
}
82+
add("name") {
83+
type(ColumnTypeSQL.VARCHAR) {
84+
options(ColumnOptionSQL.NOTNULL)
85+
}
86+
}
87+
}
88+
AQQBot.dataSource = dataSource
89+
table.createTable(dataSource)
90+
}
4791
val botFile = releaseResourceFile("bot.yml", replace = false)
4892
botConfig = Configuration.loadFromFile(botFile)
4993
val messageFile = releaseResourceFile("messages.yml", replace = false)
@@ -77,10 +121,12 @@ object AQQBot : Plugin() {
77121
}
78122

79123
override fun onDisable() {
80-
dataMap.forEach {
81-
dataConfig[it.key] = it.value
124+
if (isFileStorage) {
125+
dataMap.forEach {
126+
dataConfig[it.key] = it.value
127+
}
128+
dataConfig.saveToFile(File(getDataFolder(), "data.yml"))
82129
}
83-
dataConfig.saveToFile(File(getDataFolder(), "data.yml"))
84130
if (config.getBoolean("notify.enable")) {
85131
enableGroups.forEach {
86132
val msg = config.getString("notify.messages.stop")?: return@forEach
@@ -92,5 +138,6 @@ object AQQBot : Plugin() {
92138
if (oneBotClient.isConnected) {
93139
oneBotClient.disconnect()
94140
}
141+
if (!isFileStorage) dataSource.connection.close()
95142
}
96143
}

src/main/kotlin/top/alazeprt/aqqbot/event/AJoinEvent.kt

+22-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ import org.bukkit.event.player.AsyncPlayerChatEvent
66
import org.bukkit.event.player.AsyncPlayerPreLoginEvent
77
import taboolib.common.platform.Ghost
88
import taboolib.common.platform.event.SubscribeEvent
9+
import taboolib.common.platform.function.info
910
import taboolib.common.platform.function.submit
1011
import top.alazeprt.aonebot.action.SendGroupMessage
1112
import top.alazeprt.aqqbot.AQQBot
13+
import top.alazeprt.aqqbot.AQQBot.isFileStorage
14+
import top.alazeprt.aqqbot.util.AI18n.get
15+
import top.alazeprt.aqqbot.util.DBQuery.playerInDatabase
1216

1317
object AJoinEvent {
1418
// Bukkit
1519
@Ghost
1620
@SubscribeEvent
1721
fun onJoin(event: AsyncPlayerPreLoginEvent) {
18-
if (event.name !in AQQBot.dataMap.values) {
19-
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "[AQQBot] 你的账户还没有绑定QQ!" +
20-
"\n请通过在QQ群发送 \"${AQQBot.config.getStringList("whitelist.prefix.bind")[0]} <游戏名称>\" 绑定账户")
22+
if (isFileStorage && event.name !in AQQBot.dataMap.values) {
23+
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, formatString(get("game.not_bind", mutableMapOf(Pair("command", AQQBot.config.getStringList("whitelist.prefix.bind")[0])))))
24+
}
25+
if (!isFileStorage && !playerInDatabase(event.name)) {
26+
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, formatString(get("game.not_bind", mutableMapOf(Pair("command", AQQBot.config.getStringList("whitelist.prefix.bind")[0])))))
2127
}
2228
}
2329

@@ -27,7 +33,7 @@ object AJoinEvent {
2733
if(AQQBot.config.getBoolean("chat.server_to_group")) {
2834
submit (async = true) {
2935
AQQBot.enableGroups.forEach {
30-
AQQBot.oneBotClient.action(SendGroupMessage(it.toLong(), "[服务器] ${event.player.name}: ${event.message}", true))
36+
AQQBot.oneBotClient.action(SendGroupMessage(it.toLong(), get("qq.chat_from_game", mutableMapOf("player" to event.player.name, "message" to event.message)), true))
3137
}
3238
}
3339
}
@@ -37,8 +43,17 @@ object AJoinEvent {
3743
@Ghost
3844
@SubscribeEvent
3945
fun onVCJoin(event: PostLoginEvent) {
40-
if (event.player.username !in AQQBot.dataMap.values)
41-
event.player.disconnect(Component.text("[AQQBot] 你的账户还没有绑定QQ!" +
42-
"\n请通过在QQ群发送 \"${AQQBot.config.getStringList("whitelist.prefix.bind")[0]} <游戏名称>\" 绑定账户"))
46+
if (isFileStorage && event.player.username !in AQQBot.dataMap.values) {
47+
event.player.disconnect(Component.text(formatString(get("game.not_bind", mutableMapOf(Pair("command", AQQBot.config.getStringList("whitelist.prefix.bind")[0]))))))
48+
}
49+
if (!isFileStorage && !playerInDatabase(event.player.username)) {
50+
event.player.disconnect(Component.text(formatString(get("game.not_bind", mutableMapOf(Pair("command", AQQBot.config.getStringList("whitelist.prefix.bind")[0]))))))
51+
}
52+
}
53+
54+
private fun formatString(input: String): String {
55+
return input.replace(Regex("&([0-9a-fklmnor])")) { matchResult ->
56+
"§" + matchResult.groupValues[1]
57+
}
4358
}
4459
}

src/main/kotlin/top/alazeprt/aqqbot/handler/StatsHandler.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import top.alazeprt.aonebot.action.SendGroupMessage
1212
import top.alazeprt.aonebot.event.message.GroupMessageEvent
1313
import top.alazeprt.aqqbot.AQQBot
1414
import top.alazeprt.aqqbot.AQQBot.config
15+
import top.alazeprt.aqqbot.AQQBot.isFileStorage
1516
import top.alazeprt.aqqbot.DependencyImpl.Companion.playerStats
1617
import top.alazeprt.aqqbot.DependencyImpl.Companion.withPAPI
1718
import top.alazeprt.aqqbot.util.AI18n.get
1819
import top.alazeprt.aqqbot.util.AI18n.getList
1920
import top.alazeprt.aqqbot.util.AI18n.getOriginList
21+
import top.alazeprt.aqqbot.util.DBQuery.qqInDatabase
2022
import java.text.SimpleDateFormat
2123

2224
class StatsHandler {
@@ -48,11 +50,14 @@ class StatsHandler {
4850
get("qq.stats.not_installed_dependency"), true))
4951
return
5052
}
51-
if (!AQQBot.dataMap.containsKey(userId.toString())) {
53+
if (isFileStorage && !AQQBot.dataMap.containsKey(userId.toString())) {
54+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.stats.not_bind"), true))
55+
return
56+
} else if (!isFileStorage && qqInDatabase(userId) == null) {
5257
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.stats.not_bind"), true))
5358
return
5459
}
55-
val name = AQQBot.dataMap[userId.toString()]!!
60+
val name = if(isFileStorage) AQQBot.dataMap[userId.toString()]!! else qqInDatabase(userId)!!
5661
val banEntry = Bukkit.getBanList<ProfileBanList>(BanList.Type.PROFILE).getBanEntry(name)
5762
val uuid = Bukkit.getOfflinePlayer(name).uniqueId
5863
val lastLogin = Bukkit.getOfflinePlayer(uuid).lastPlayed

src/main/kotlin/top/alazeprt/aqqbot/handler/WhitelistAdminHandler.kt

+40-13
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,70 @@ import top.alazeprt.aonebot.action.GetGroupMemberList
44
import top.alazeprt.aonebot.action.SendGroupMessage
55
import top.alazeprt.aonebot.event.message.GroupMessageEvent
66
import top.alazeprt.aqqbot.AQQBot
7+
import top.alazeprt.aqqbot.AQQBot.isFileStorage
78
import top.alazeprt.aqqbot.util.AI18n.get
9+
import top.alazeprt.aqqbot.util.DBQuery.addPlayer
10+
import top.alazeprt.aqqbot.util.DBQuery.playerInDatabase
11+
import top.alazeprt.aqqbot.util.DBQuery.qqInDatabase
12+
import top.alazeprt.aqqbot.util.DBQuery.removePlayer
813

914
class WhitelistAdminHandler {
1015
companion object {
1116
private fun bind(userId: String, groupId: Long, playerName: String) {
12-
if (AQQBot.dataMap.containsKey(userId)) {
17+
if (isFileStorage && AQQBot.dataMap.containsKey(userId)) {
18+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.already_bind", mutableMapOf(Pair("userId", userId)))))
19+
return
20+
} else if (!isFileStorage && qqInDatabase(userId.toLong()) != null) {
1321
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.already_bind", mutableMapOf(Pair("userId", userId)))))
1422
return
1523
}
1624
if (!validateName(playerName)) {
1725
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.invalid_name"), true))
1826
return
1927
}
20-
AQQBot.dataMap.values.forEach {
21-
if (it == playerName) {
28+
if (isFileStorage) {
29+
AQQBot.dataMap.values.forEach {
30+
if (it == playerName) {
31+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.already_exist"), true))
32+
return
33+
}
34+
}
35+
} else {
36+
if (playerInDatabase(playerName)) {
2237
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.already_exist"), true))
2338
return
2439
}
2540
}
26-
AQQBot.dataMap[userId] = playerName
41+
if (isFileStorage) AQQBot.dataMap[userId] = playerName
42+
else addPlayer(userId.toLong(), playerName)
2743
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.bind_successful"), true))
2844
}
2945

3046
private fun unbind(userId: String, groupId: Long, playerName: String) {
31-
if (!AQQBot.dataMap.containsKey(userId)) {
47+
if (isFileStorage && !AQQBot.dataMap.containsKey(userId)) {
48+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.not_bind", mutableMapOf(Pair("userId", userId))), true))
49+
return
50+
} else if (!isFileStorage && qqInDatabase(userId.toLong()) == null) {
3251
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.not_bind", mutableMapOf(Pair("userId", userId))), true))
3352
return
3453
}
35-
AQQBot.dataMap.forEach { (k, v) ->
36-
if (v == playerName && k == userId) {
37-
AQQBot.dataMap.remove(k)
38-
AQQBot.oneBotClient.action(SendGroupMessage(groupId, "解绑成功!", true))
39-
return
40-
} else if (k == userId) {
41-
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.bind_by_other", mutableMapOf(Pair("name", v))), true))
42-
return
54+
if (isFileStorage) {
55+
AQQBot.dataMap.forEach { (k, v) ->
56+
if (v == playerName && k == userId) {
57+
AQQBot.dataMap.remove(k)
58+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.unbind_successful"), true))
59+
return
60+
} else if (k == userId) {
61+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.bind_by_other", mutableMapOf(Pair("name", v))), true))
62+
return
63+
}
64+
}
65+
} else {
66+
if (qqInDatabase(userId.toLong()) != playerName) {
67+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.bind_by_other", mutableMapOf(Pair("name", qqInDatabase(userId.toLong())!!))), true))
4368
}
69+
removePlayer(userId.toLong(), playerName)
70+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.unbind_successful"), true))
4471
}
4572
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.admin.invalid_bind", mutableMapOf(Pair("userId", userId))), true))
4673
}

src/main/kotlin/top/alazeprt/aqqbot/handler/WhitelistHandler.kt

+48-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package top.alazeprt.aqqbot.handler
22

3+
import org.bukkit.Bukkit
34
import taboolib.common.platform.function.info
45
import top.alazeprt.aonebot.action.GetGroupMemberInfo
56
import top.alazeprt.aonebot.action.SendGroupMessage
67
import top.alazeprt.aonebot.event.message.GroupMessageEvent
78
import top.alazeprt.aonebot.util.GroupRole
89
import top.alazeprt.aqqbot.AQQBot
10+
import top.alazeprt.aqqbot.AQQBot.isFileStorage
911
import top.alazeprt.aqqbot.util.AI18n.get
12+
import top.alazeprt.aqqbot.util.DBQuery.addPlayer
13+
import top.alazeprt.aqqbot.util.DBQuery.playerInDatabase
14+
import top.alazeprt.aqqbot.util.DBQuery.qqInDatabase
15+
import top.alazeprt.aqqbot.util.DBQuery.removePlayer
1016

1117
class WhitelistHandler {
1218
companion object {
1319
private fun bind(userId: String, groupId: Long, playerName: String) {
14-
if (AQQBot.dataMap.containsKey(userId)) {
20+
if (isFileStorage && AQQBot.dataMap.containsKey(userId)) {
21+
AQQBot.oneBotClient.action(
22+
SendGroupMessage(groupId, get("qq.whitelist.already_bind"), true))
23+
return
24+
} else if (!isFileStorage && qqInDatabase(userId.toLong()) != null) {
1525
AQQBot.oneBotClient.action(
1626
SendGroupMessage(groupId, get("qq.whitelist.already_bind"), true))
1727
return
@@ -20,32 +30,57 @@ class WhitelistHandler {
2030
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.invalid_name"), true))
2131
return
2232
}
23-
AQQBot.dataMap.values.forEach {
24-
if (it == playerName) {
33+
if (isFileStorage) {
34+
AQQBot.dataMap.values.forEach {
35+
if (it == playerName) {
36+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.already_exist"), true))
37+
return
38+
}
39+
}
40+
} else {
41+
if (playerInDatabase(playerName)) {
2542
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.already_exist"), true))
2643
return
2744
}
2845
}
29-
AQQBot.dataMap[userId] = playerName
46+
if (isFileStorage) AQQBot.dataMap[userId] = playerName
47+
else addPlayer(userId.toLong(), playerName)
3048
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.bind_successful"), true))
3149
}
3250

3351
private fun unbind(userId: String, groupId: Long, playerName: String) {
34-
if (!AQQBot.dataMap.containsKey(userId)) {
52+
if (isFileStorage && !AQQBot.dataMap.containsKey(userId)) {
53+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.not_bind"), true))
54+
return
55+
} else if (!isFileStorage && qqInDatabase(userId.toLong()) == null) {
3556
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.not_bind"), true))
3657
return
3758
}
38-
AQQBot.dataMap.forEach { (k, v) ->
39-
if (v == playerName && k == userId) {
40-
AQQBot.dataMap.remove(k)
41-
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("unbind_successful"), true))
42-
return
43-
} else if (k == userId) {
44-
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.bind_by_other", mutableMapOf(Pair("name", v))), true))
59+
if (isFileStorage) {
60+
AQQBot.dataMap.forEach { (k, v) ->
61+
if (v == playerName && k == userId) {
62+
AQQBot.dataMap.remove(k)
63+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.unbind_successful"), true))
64+
return
65+
} else if (k == userId) {
66+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.bind_by_other", mutableMapOf(Pair("name", v))), true))
67+
return
68+
}
69+
}
70+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.invalid_bind"), true))
71+
} else {
72+
if (qqInDatabase(userId.toLong()) != playerName) {
73+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.bind_by_other", mutableMapOf(Pair("name", qqInDatabase(userId.toLong())!!))), true))
4574
return
4675
}
76+
removePlayer(userId.toLong(), playerName)
77+
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.unbind_successful"), true))
78+
for (player in Bukkit.getOnlinePlayers()) {
79+
if (player.name == playerName) {
80+
player.kickPlayer(get("qq.whitelist.unbind_kick"))
81+
}
82+
}
4783
}
48-
AQQBot.oneBotClient.action(SendGroupMessage(groupId, get("qq.whitelist.invalid_bind"), true))
4984
}
5085

5186
private fun validateName(name: String): Boolean {

0 commit comments

Comments
 (0)