Skip to content

Commit 4eb6f21

Browse files
committed
🎨(coreLib/CommandApi) 增加CommandInfo.requirePermission
1 parent d6e9739 commit 4eb6f21

4 files changed

Lines changed: 27 additions & 27 deletions

File tree

scripts/coreLibrary/lib/CommandApi.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class CommandInfo(
118118
val attrs: List<CommandHandler> = mutableListOf()
119119
var usage: String = ""
120120

121-
@Deprecated("use RequirePermission(permission)")
121+
@Deprecated("use requirePermission(permission)")
122122
var permission: String = ""
123123
private var onComplete: CommandHandler = CommandHandler {}
124124
private var body: CommandHandler = CommandHandler {}
@@ -128,7 +128,7 @@ class CommandInfo(
128128
if (frozen) return
129129
@Suppress("DEPRECATION")
130130
if (permission.isNotEmpty())
131-
attr(RequirePermission(permission))
131+
attr(Commands.Permission(permission))
132132
frozen = true
133133
}
134134

@@ -141,6 +141,8 @@ class CommandInfo(
141141
(attrs as MutableList).add(beforeBody)
142142
}
143143

144+
inline fun <reified T> attr() = attrs.filterIsInstance<T>()
145+
144146
@Deprecated("replace CommandHandler", level = DeprecationLevel.HIDDEN)
145147
fun onComplete(block: CommandHandlerOld) = onComplete {
146148
block.invoke(context)
@@ -186,7 +188,7 @@ class CommandInfo(
186188
}
187189

188190
@CommandBuilder
189-
@Deprecated("use +RequirePermission(permission)")
191+
@Deprecated("use requirePermission(permission)")
190192
fun CommandContext.replyNoPermission(): Nothing {
191193
reply("[red]你没有执行该命令的权限".with())
192194
Return()
@@ -223,6 +225,13 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
223225
}
224226
}
225227

228+
data class Permission(val permission: String) : Hidden {
229+
context(CommandContext) override suspend fun visible(): Boolean = hasPermission(permission)
230+
context(CommandContext) override suspend fun handle() {
231+
if (!visible()) returnReply("[red]你没有执行该命令的权限".with())
232+
}
233+
}
234+
226235
protected val nameMap = mutableMapOf<String, CommandInfo>()
227236
open fun subCommands(): Map<String, CommandInfo> = nameMap
228237
fun getSub(name: String): CommandInfo? = subCommands()[name.lowercase()]
@@ -318,15 +327,16 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
318327
}
319328

320329
//compatibility for [CommandInfo.body]
321-
@Deprecated("use CommandHandler instead", level = DeprecationLevel.ERROR,
330+
@Deprecated(
331+
"use CommandHandler instead", level = DeprecationLevel.ERROR,
322332
replaceWith = ReplaceWith("this.handle()")
323333
)
324334
override suspend fun invoke(p1: CommandContext) = error("use CommandHandler")
325335

326336
object Root : Commands() {
327337
init {
328338
this += CommandInfo(null, "ScriptAgent", "ScriptAgent 控制指令".with(), listOf("sa")).apply {
329-
attr(RequirePermission("scriptAgent.admin"))
339+
requirePermission("scriptAgent.admin")
330340
body(controlCommand)
331341
}
332342
thisContextScript().listenTo<ScriptDisableEvent> {
@@ -349,7 +359,7 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
349359
val detail = buildString {
350360
if (!showDetail) return@buildString
351361
if (it.script != null) append(" | ${it.script.id}")
352-
if (it.permission.isNotBlank()) append(" | ${it.permission}")
362+
it.attr<Permission>().firstOrNull()?.let { append(" | ${it.permission}") }
353363
}
354364
return "[light_yellow]{prefix}{name}[light_red]{aliases} [white]{usage} [light_cyan]{desc}[cyan]{detail}".with(
355365
"prefix" to prefix, "name" to it.name, "aliases" to alias,
@@ -361,13 +371,6 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
361371
}
362372
}
363373

364-
data class RequirePermission(val permission: String) : Commands.Hidden {
365-
context(CommandContext) override suspend fun visible(): Boolean = hasPermission(permission)
366-
context(CommandContext) override suspend fun handle() {
367-
if (!visible()) returnReply("[red]你没有执行该命令的权限".with())
368-
}
369-
}
370-
371374
@ScriptDsl
372375
inline fun Script.command(
373376
name: String,
@@ -384,4 +387,9 @@ inline fun Script.command(
384387
@ScriptDsl
385388
inline fun Script.command(name: String, description: String, init: CommandInfo.() -> Unit) {
386389
command(name, description.with()) { init() }
390+
}
391+
392+
@CommandInfo.CommandBuilder
393+
fun CommandInfo.requirePermission(permission: String) {
394+
attr(Commands.Permission(permission))
387395
}

scripts/wayzer/cmds/voteKick.kts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ val banImpl = contextScript<wayzer.user.Ban>()
5151
command("kick", "踢出某人".with(), commands = VoteEvent.VoteCommands) {
5252
aliases = listOf("踢出")
5353
usage = "<玩家名/id> <理由>"
54-
attr(RequirePermission("wayzer.vote.kick"))
54+
requirePermission("wayzer.vote.kick")
5555
body {
5656
val target = getTarget()
5757
val reason = getInput("踢人理由", "[red]投票踢人需要理由".with())
@@ -72,13 +72,4 @@ command("kick", "踢出某人".with(), commands = VoteEvent.VoteCommands) {
7272
}
7373
}
7474

75-
command("votekick", "(弃用)投票踢人") {
76-
usage = "<player...>"
77-
attr(ClientOnly)
78-
body {
79-
//Redirect
80-
arg = listOf("kick", *arg.toTypedArray())
81-
VoteEvent.VoteCommands.handle()
82-
}
83-
}
8475
PermissionApi.registerDefault("wayzer.admin.skipKick", group = "@admin")

scripts/wayzer/cmds/voteOb.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.time.Duration
99
import java.time.Instant
1010

1111
val teams = contextScript<BetterTeam>()
12-
val voteKick = contextScript<wayzer.cmds.VoteKick>()
12+
val voteKick = contextScript<VoteKick>()
1313

1414
@Savable(false)
1515
val limitPlayers = mutableMapOf<String, Pair<String, Instant>>()//profile -> reason,time
@@ -85,7 +85,8 @@ listenTo<BetterTeam.AssignTeamEvent>(Event.Priority.Intercept) {
8585
}
8686
}
8787
command("votekick", "(弃用)投票踢人") {
88-
this.usage = "<player...>";this.type = CommandType.Client
88+
this.usage = "<player...>"
89+
attr(ClientOnly)
8990
body {
9091
//Redirect
9192
arg = listOf("ob", *arg.toTypedArray())
@@ -112,4 +113,4 @@ command("forceOB", "管理指令:使某人强制观战") {
112113
)
113114
}
114115
}
115-
PermissionApi.registerDefault("wayzer.admin.skipKick", "wayzer.admin.forceOb", group = "@admin")
116+
PermissionApi.registerDefault("wayzer.admin.forceOb", group = "@admin")

scripts/wayzer/user/ext/skills.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ companion object Api {
6868
@ScriptDsl
6969
fun Script.skill(name: String, desc: String, vararg aliases: String, body: SkillScope.() -> Unit) {
7070
skills += CommandInfo(this, name, desc) {
71-
attr(RequirePermission("wayzer.user.skills.$name"))
71+
requirePermission("wayzer.user.skills.$name")
7272
attr(ClientOnly)
7373
this.aliases = aliases.toList()
7474
body {

0 commit comments

Comments
 (0)