|
|
@@ -10,22 +10,19 @@ import bot_luo_core.cli.annotation.Method
|
|
|
import bot_luo_core.cli.handlers.GroupArgHandler
|
|
|
import bot_luo_core.cli.handlers.TagArgHandler
|
|
|
import bot_luo_core.cli.handlers.UserArgHandler
|
|
|
-import bot_luo_core.data.Group
|
|
|
-import bot_luo_core.data.Tags
|
|
|
-import bot_luo_core.data.Tags.BUILTIN_GROUP_TAGS
|
|
|
-import bot_luo_core.data.Tags.BUILTIN_USER_TAGS
|
|
|
-import bot_luo_core.data.Tags.addToGroupTag
|
|
|
-import bot_luo_core.data.Tags.addToUserTag
|
|
|
-import bot_luo_core.data.Tags.groupTag
|
|
|
-import bot_luo_core.data.Tags.readGroupTag
|
|
|
-import bot_luo_core.data.Tags.readUserTag
|
|
|
-import bot_luo_core.data.Tags.remFromGroupTag
|
|
|
-import bot_luo_core.data.Tags.remFromUserTag
|
|
|
-import bot_luo_core.data.Tags.remGroupTag
|
|
|
-import bot_luo_core.data.Tags.remUserTag
|
|
|
-import bot_luo_core.data.Tags.userTag
|
|
|
-import bot_luo_core.data.User
|
|
|
+import bot_luo_core.data.*
|
|
|
+import bot_luo_core.data.GroupTags.BUILTIN_GROUP_TAGS
|
|
|
+import bot_luo_core.data.GroupTags.addToGroupTag
|
|
|
+import bot_luo_core.data.GroupTags.readGroupTag
|
|
|
+import bot_luo_core.data.GroupTags.remFromGroupTag
|
|
|
+import bot_luo_core.data.GroupTags.remGroupTag
|
|
|
+import bot_luo_core.data.UserTags.BUILTIN_USER_TAGS
|
|
|
+import bot_luo_core.data.UserTags.addToUserTag
|
|
|
+import bot_luo_core.data.UserTags.readUserTag
|
|
|
+import bot_luo_core.data.UserTags.remFromUserTag
|
|
|
+import bot_luo_core.data.UserTags.remUserTag
|
|
|
import bot_luo_core.util.TableBuilder
|
|
|
+import bot_luo_core.util.Tag.tree
|
|
|
import bot_luo_core.util.Text.toLowercase
|
|
|
import kotlin.collections.ArrayList
|
|
|
|
|
|
@@ -40,7 +37,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
/* ======================== add ======================== */
|
|
|
|
|
|
@Method(name = "group-add", alias = ["ga"], pmsLevel = CmdPermissionLevel.OP, title = "添加群组标签", order = 0)
|
|
|
- fun groupAdd(
|
|
|
+ suspend fun groupAdd(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待加入标签", handler = TagArgHandler::class, multiValued = true)
|
|
|
@@ -55,12 +52,14 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("群组标签添加 —— 标签($tag)").br()
|
|
|
|
|
|
- for (t in tagIn) {
|
|
|
- if (addToGroupTag(tag, t)) {
|
|
|
- table.tr().tb(t).tb("[√] success added")
|
|
|
- suc++
|
|
|
- } else {
|
|
|
- table.tr().tb(t).tb("[×] duplicated item or circulated tag")
|
|
|
+ withLockedAccessing(GroupTags) {
|
|
|
+ for (t in tagIn) {
|
|
|
+ if (addToGroupTag(tag, t)) {
|
|
|
+ table.tr().tb(t).tb("[√] success added")
|
|
|
+ suc++
|
|
|
+ } else {
|
|
|
+ table.tr().tb(t).tb("[×] duplicated item or circulated tag")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
@@ -68,7 +67,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
|
|
|
@Method(name = "group-add", alias = ["ga"], pmsLevel = CmdPermissionLevel.OP, title = "添加群组标签", order = 1)
|
|
|
- fun groupAdd1(
|
|
|
+ suspend fun groupAdd1(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待加入群组", handler = GroupArgHandler::class, multiValued = true)
|
|
|
@@ -83,12 +82,14 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("群组标签添加 —— 标签($tag)").br()
|
|
|
|
|
|
- for (g in groupIn) {
|
|
|
- if (addToGroupTag(tag, g.id.toString())) {
|
|
|
- table.tr(g.name).tb("(${g.id})").tb("[√] success added")
|
|
|
- suc++
|
|
|
- } else {
|
|
|
- table.tr(g.name).tb("(${g.id})").tb("[×] duplicated item")
|
|
|
+ withLockedAccessing(GroupTags) {
|
|
|
+ for (g in groupIn) {
|
|
|
+ if (addToGroupTag(tag, g.id.toString())) {
|
|
|
+ table.tr(g.name).tb("(${g.id})").tb("[√] success added")
|
|
|
+ suc++
|
|
|
+ } else {
|
|
|
+ table.tr(g.name).tb("(${g.id})").tb("[×] duplicated item")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
@@ -96,7 +97,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
|
|
|
@Method(name = "user-add", alias = ["ua"], pmsLevel = CmdPermissionLevel.OP, title = "添加用户标签", order = 0)
|
|
|
- fun userAdd(
|
|
|
+ suspend fun userAdd(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待加入标签", handler = TagArgHandler::class, multiValued = true)
|
|
|
@@ -111,12 +112,14 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("用户标签添加 —— 标签($tag)").br()
|
|
|
|
|
|
- for (t in tagIn) {
|
|
|
- if (addToUserTag(tag, t)) {
|
|
|
- table.tr().tb(t).tb("[√] success added")
|
|
|
- suc++
|
|
|
- } else {
|
|
|
- table.tr().tb(t).tb("[×] duplicated item or circulated tag")
|
|
|
+ withLockedAccessing(UserTags) {
|
|
|
+ for (t in tagIn) {
|
|
|
+ if (addToUserTag(tag, t)) {
|
|
|
+ table.tr().tb(t).tb("[√] success added")
|
|
|
+ suc++
|
|
|
+ } else {
|
|
|
+ table.tr().tb(t).tb("[×] duplicated item or circulated tag")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
@@ -124,7 +127,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
|
|
|
@Method(name = "user-add", alias = ["ua"], pmsLevel = CmdPermissionLevel.OP, title = "添加用户标签", order = 1)
|
|
|
- fun userAdd1(
|
|
|
+ suspend fun userAdd1(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待加入用户", handler = UserArgHandler::class, multiValued = true)
|
|
|
@@ -139,12 +142,14 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("用户标签添加 —— 标签($tag)").br()
|
|
|
|
|
|
- for (u in userIn) {
|
|
|
- if (addToUserTag(tag, u.id.toString())) {
|
|
|
- table.tr(u.name).tb("(${u.id})").tb("[√] success added")
|
|
|
- suc++
|
|
|
- } else {
|
|
|
- table.tr(u.name).tb("(${u.id})").tb("[×] duplicated item")
|
|
|
+ withLockedAccessing(UserTags) {
|
|
|
+ for (u in userIn) {
|
|
|
+ if (addToUserTag(tag, u.id.toString())) {
|
|
|
+ table.tr(u.name).tb("(${u.id})").tb("[√] success added")
|
|
|
+ suc++
|
|
|
+ } else {
|
|
|
+ table.tr(u.name).tb("(${u.id})").tb("[×] duplicated item")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
@@ -161,7 +166,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
if (tag == null) {
|
|
|
table.th("群组标签 —— 所有标签").br()
|
|
|
- for (t in groupTag.keys) {
|
|
|
+ for (t in GroupTags.tags.keys) {
|
|
|
table.tr(t)
|
|
|
}
|
|
|
for (t in BUILTIN_GROUP_TAGS) {
|
|
|
@@ -187,7 +192,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
val table = TableBuilder(4)
|
|
|
if (tag == null) {
|
|
|
table.th("用户标签 —— 所有标签").br()
|
|
|
- for (t in userTag.keys) {
|
|
|
+ for (t in UserTags.tags.keys) {
|
|
|
table.tr(t)
|
|
|
}
|
|
|
for (t in BUILTIN_USER_TAGS) {
|
|
|
@@ -214,7 +219,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
): CmdReceipt {
|
|
|
val table = TableBuilder(1)
|
|
|
table.th("展开群组标签 —— 标签($tag)")
|
|
|
- tree(tag, table) { groupTag[it] ?: ArrayList() }
|
|
|
+ tree(tag, table) { GroupTags.tags[it] ?: ArrayList() }
|
|
|
context.print(table.toString())
|
|
|
return SUCCESS
|
|
|
}
|
|
|
@@ -226,7 +231,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
): CmdReceipt {
|
|
|
val table = TableBuilder(1)
|
|
|
table.th("展开用户标签 —— 标签($tag)")
|
|
|
- tree(tag, table) { userTag[it] ?: ArrayList() }
|
|
|
+ tree(tag, table) { UserTags.tags[it] ?: ArrayList() }
|
|
|
context.print(table.toString())
|
|
|
return SUCCESS
|
|
|
}
|
|
|
@@ -247,7 +252,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
context.print("操作超时,")
|
|
|
false
|
|
|
}) {
|
|
|
- if (remGroupTag(tag)) {
|
|
|
+ if (withLockedAccessing(GroupTags) { remGroupTag(tag) }) {
|
|
|
context.print("已移除标签 $tag 及其引用")
|
|
|
SUCCESS
|
|
|
} else {
|
|
|
@@ -274,7 +279,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
context.print("操作超时,")
|
|
|
false
|
|
|
}) {
|
|
|
- if (remUserTag(tag)) {
|
|
|
+ if (withLockedAccessing(UserTags) { remUserTag(tag) }) {
|
|
|
context.print("已移除标签 $tag 及其引用")
|
|
|
SUCCESS
|
|
|
} else {
|
|
|
@@ -288,7 +293,7 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
|
|
|
@Method(name = "group-remove", alias = ["gr"], pmsLevel = CmdPermissionLevel.OP, title = "移除群组标签", order = 1)
|
|
|
- fun groupRem(
|
|
|
+ suspend fun groupRem(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待移除群组或标签", multiValued = true)
|
|
|
@@ -300,18 +305,20 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("移除群组标签 —— 标签($tag)")
|
|
|
- for (i in itemsIn) {
|
|
|
- if (remFromGroupTag(tag, i.toLowercase()))
|
|
|
- table.tr().tb(i).tb("[√] success removed")
|
|
|
- else
|
|
|
- table.tr().tb(i).tb("[×] tag or item not found")
|
|
|
+ withLockedAccessing(GroupTags) {
|
|
|
+ for (i in itemsIn) {
|
|
|
+ if (remFromGroupTag(tag, i.toLowercase()))
|
|
|
+ table.tr().tb(i).tb("[√] success removed")
|
|
|
+ else
|
|
|
+ table.tr().tb(i).tb("[×] tag or item not found")
|
|
|
+ }
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
return SUCCESS
|
|
|
}
|
|
|
|
|
|
@Method(name = "group-remove", alias = ["gr"], pmsLevel = CmdPermissionLevel.OP, title = "移除群组标签", order = 1)
|
|
|
- fun userRem(
|
|
|
+ suspend fun userRem(
|
|
|
@Argument(display = "被操作标签", handler = TagArgHandler::class)
|
|
|
tag: String,
|
|
|
@Argument(display = "待移除用户或标签", multiValued = true)
|
|
|
@@ -323,52 +330,15 @@ class TagCmd(context: CmdContext) : Cmd(context) {
|
|
|
}
|
|
|
val table = TableBuilder(4)
|
|
|
table.th("移除用户标签 —— 标签($tag)")
|
|
|
- for (i in itemsIn) {
|
|
|
- if (remFromUserTag(tag, i.toLowercase()))
|
|
|
- table.tr().tb(i).tb("[√] success removed")
|
|
|
- else
|
|
|
- table.tr().tb(i).tb("[×] tag or item not found")
|
|
|
+ withLockedAccessing(UserTags) {
|
|
|
+ for (i in itemsIn) {
|
|
|
+ if (remFromUserTag(tag, i.toLowercase()))
|
|
|
+ table.tr().tb(i).tb("[√] success removed")
|
|
|
+ else
|
|
|
+ table.tr().tb(i).tb("[×] tag or item not found")
|
|
|
+ }
|
|
|
}
|
|
|
context.print(table.toString())
|
|
|
return SUCCESS
|
|
|
}
|
|
|
-
|
|
|
- override fun onExit(receipt: CmdReceipt) {
|
|
|
- if (receipt.needSave()) Tags.saveTags()
|
|
|
- }
|
|
|
-
|
|
|
- companion object {
|
|
|
- private fun tree(
|
|
|
- tag: String,
|
|
|
- table: TableBuilder,
|
|
|
- line: ArrayList<Int> = ArrayList(),
|
|
|
- tagGetter: (tag: String) -> ArrayList<String>
|
|
|
- ) {
|
|
|
- table.tr()
|
|
|
- line.forEachIndexed { index, l ->
|
|
|
- if (index < line.lastIndex) {
|
|
|
- if (l > 0) table.tb("│")
|
|
|
- else table.tb(" ")
|
|
|
- } else {
|
|
|
- if (l > 0) table.tb("├")
|
|
|
- else table.tb("└")
|
|
|
- }
|
|
|
- }
|
|
|
- table.tb(tag)
|
|
|
- val items = tagGetter(tag)
|
|
|
- items.sortedWith { o1, o2 ->
|
|
|
- var r = 0
|
|
|
- if (o1.startsWith('#')) r++
|
|
|
- if (o2.startsWith('#')) r--
|
|
|
- r
|
|
|
- }
|
|
|
- items.forEachIndexed { index, i ->
|
|
|
- line.add(items.lastIndex - index)
|
|
|
-
|
|
|
- tree(i, table, line, tagGetter)
|
|
|
-
|
|
|
- line.removeLast()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|