@@ -2,79 +2,82 @@ package coreLibrary.commands
22
33import cf.wayzer.placehold.PlaceHoldApi.with
44
5+ val configCommands = Commands ()
56command(" config" , " 查看或修改配置" .with (), commands = Commands .controlCommand) {
67 usage = " [help/arg...]"
7- permission = " scriptAgent.config"
8+ requirePermission(" scriptAgent.$name " )
9+ body(configCommands)
10+ }
11+
12+ command(" list" , " 列出所有配置项" .with (), commands = configCommands) {
13+ usage = " [page]"
14+ body {
15+ val page = arg.getOrNull(0 )?.toIntOrNull() ? : 1
16+ reply(menu(" 配置项" , ConfigBuilder .all.values.sortedBy { it.path }, page, 15 ) {
17+ " [green]{key} [blue]{desc}" .with (
18+ " key" to it.path,
19+ " desc" to (it.desc.firstOrNull() ? : " " )
20+ )
21+ })
22+ }
23+ }
24+ command(" reload" , " 重载配置文件" .with (), commands = configCommands) {
25+ requirePermission(" scriptAgent.config.$name " )
26+ body {
27+ ConfigBuilder .reloadFile()
28+ reply(" [green]重载成功" .with ())
29+ }
30+ }
31+
32+ @CommandInfo.CommandBuilder
33+ inline fun CommandInfo.subCommand (
34+ usage : String ,
35+ crossinline block : suspend context(CommandContext ) (ConfigBuilder .ConfigKey <* >) -> Unit
36+ ) {
37+ this .usage = " <key> $usage "
838 onComplete {
9- onComplete(0 ) { listOf (" help" , " reload" ) + ConfigBuilder .all.keys }
10- onComplete(1 ) { listOf (" set" , " write" , " reset" ) }
39+ onComplete(0 ) { ConfigBuilder .all.keys.toList() }
1140 }
1241 body {
13- if (arg.isEmpty() || arg[0 ].equals(" help" , true ))
14- returnReply(
15- """
16- [yellow]可用操作
17- [purple]config reload [light_purple]重载配置文件
18- [purple]config list [页码] [light_purple]列出配置项
19- [purple]config <配置项> [light_purple]查看配置项介绍及当前值
20- [purple]config <配置项> set <value> [light_purple]设置配置值
21- [purple]config <配置项> write [light_purple]写入默认值到配置文件
22- [purple]config <配置项> reset [light_purple]恢复默认值(从配置文件移除默认值)
23- """ .trimIndent().with ()
24- )
25- if (arg[0 ].equals(" list" , true )) {
26- val page = arg.getOrNull(1 )?.toIntOrNull() ? : 1
27- returnReply(menu(" 配置项" , ConfigBuilder .all.values.sortedBy { it.path }, page, 15 ) {
28- " [green]{key} [blue]{desc}" .with (
29- " key" to it.path,
30- " desc" to (it.desc.firstOrNull() ? : " " )
31- )
32- })
33- }
34- if (arg[0 ].equals(" reload" , true )) {
35- if (! hasPermission(" $permission .reload" ))
36- replyNoPermission()
37- ConfigBuilder .reloadFile()
38- returnReply(" [green]重载成功" .with ())
39- }
4042 val config = arg.firstOrNull()?.let { ConfigBuilder .all[it] } ? : returnReply(" [red]找不到配置项" .with ())
41- if (! hasPermission(permission + " ." + config.path))
43+ if (! hasPermission(" scriptAgent.config ." + config.path))
4244 returnReply(" [red]你没有权限修改配置项: {config}" .with (" config" to config.path))
43- when (arg.getOrNull(1 )?.lowercase()) {
44- null -> {
45- returnReply(
46- """
45+ block(context, config)
46+ }
47+ }
48+ command(" get" , " 获取配置项" .with (), commands = configCommands) {
49+ subCommand(" " ) { config ->
50+ reply(
51+ """
4752 |[yellow]==== [light_yellow]配置项: {name}[yellow] ====
4853 |[purple]{desc:${" \n " } }
4954 |[cyan]当前值: [yellow]{value}
5055 |[cyan]默认值: [yellow]{default}
5156 |[yellow]使用/sa config help查看可用操作
5257 """ .trimMargin().with (
53- " name" to config.path, " desc" to config.desc,
54- " value" to config.getString(), " default" to config.default,
55- )
56- )
57- }
58-
59- " reset" -> {
60- config.reset()
61- returnReply(" [green]重置成功,当前:[yellow]{value}" .with (" value" to config.getString()))
62- }
63-
64- " write" -> {
65- config.writeDefault()
66- reply(" [green]写入文件成功" .with ())
67- }
68-
69- " set" -> {
70- if (arg.size <= 2 ) returnReply(" [red]请输入值" .with ())
71- val value = arg.subList(2 , arg.size).joinToString(" " )
72- returnReply(" [green]设置成功,当前:[yellow]{value}" .with (" value" to config.setString(value)))
73- }
74-
75- else -> {
76- returnReply(" [red]未知操作,请查阅help帮助" .with ())
77- }
78- }
58+ " name" to config.path, " desc" to config.desc,
59+ " value" to config.getString(), " default" to config.default,
60+ )
61+ )
62+ }
63+ }
64+ command(" reset" , " 恢复默认值" .with (), commands = configCommands) {
65+ subCommand(" " ) { config ->
66+ config.reset()
67+ reply(" [green]恢复成功,当前:[yellow]{value}" .with (" value" to config.getString()))
68+ }
69+ }
70+ command(" write" , " 设置配置项" .with (), commands = configCommands) {
71+ subCommand(" " ) { config ->
72+ if (config.get() != config.default)
73+ config.writeDefault()
74+ reply(" [green]写入文件成功" .with ())
75+ }
76+ }
77+ command(" set" , " 设置配置项" .with (), commands = configCommands) {
78+ subCommand(" <value>" ) { config ->
79+ if (arg.size <= 1 ) returnReply(" [red]请输入值" .with ())
80+ val value = arg.subList(1 , arg.size).joinToString(" " )
81+ reply(" [green]设置成功,当前:[yellow]{value}" .with (" value" to config.setString(value)))
7982 }
8083}
0 commit comments