@@ -110,9 +110,9 @@ typealias CommandHandlerOld = suspend CommandContext.() -> Unit
110110
111111fun interface CommandHandler {
112112 // Default only handle Command
113- context( CommandContext ) fun canHandle () = context is CommandContext .Command
113+ fun CommandContext. canHandle () = context is CommandContext .Command
114114
115- context( CommandContext ) suspend fun handle ()
115+ suspend fun CommandContext. handle ()
116116}
117117
118118@Deprecated(" use CommandHandler.canHandle logic" )
@@ -192,7 +192,7 @@ class CommandInfo(
192192 freeze()
193193 }
194194
195- context( CommandContext ) override fun canHandle (): Boolean =
195+ override fun CommandContext. canHandle (): Boolean =
196196 context is CommandContext .TabComplete || body.canHandle()
197197
198198 override suspend fun onComplete (context : CommandContext ) {
@@ -212,19 +212,19 @@ class CommandInfo(
212212 (body as ? TabCompleter )?.onComplete(context)
213213 }
214214
215- context( CommandContext ) override suspend fun handle () {
215+ override suspend fun CommandContext. handle () {
216216 if (context is CommandContext .TabComplete )
217217 return onComplete(context)
218218 try {
219219 attrs.forEach { it.handle() }
220220 body.handle()
221221 } catch (e: CancellationException ) {
222222 if (e !is Return )
223- thisContextScript().logger.log(
223+ this . thisContextScript().logger.log(
224224 Level .WARNING , " You should not cancel command. If you need exit, using CommandInfo.Return()" , e
225225 )
226226 } catch (e: Exception ) {
227- reply(" [red]执行命令出现异常: {msg}" .with (" msg" to (e.message ? : " " )))
227+ context. reply(" [red]执行命令出现异常: {msg}" .with (" msg" to (e.message ? : " " )))
228228 e.printStackTrace()
229229 }
230230 }
@@ -259,19 +259,19 @@ class CommandInfo(
259259 annotation class CommandBuilder
260260}
261261
262- @Suppress(" DEPRECATION" , " SUPERTYPE_IS_SUSPEND_EXTENSION_FUNCTION_TYPE " )
263- open class Commands : CommandHandler , TabCompleter , CommandHandlerOld {
262+ @Suppress(" DEPRECATION" )
263+ open class Commands : CommandHandler , TabCompleter {
264264 fun interface Hidden : CommandHandler {
265- /* * 当前命令是否可用, 用于[Commands.helpCommand ]处理 */
266- context( CommandContext ) suspend fun visible (): Boolean
267- context( CommandContext ) override suspend fun handle () {
265+ /* * 当前命令是否可用, 用于[Commands]处理 */
266+ suspend fun CommandContext. visible (): Boolean
267+ override suspend fun CommandContext. handle () {
268268 if (! visible()) returnReply(" [red]该命令当前不可用" .with ())
269269 }
270270 }
271271
272272 data class Permission (val permission : String ) : Hidden {
273- context( CommandContext ) override suspend fun visible (): Boolean = hasPermission(permission)
274- context( CommandContext ) override suspend fun handle () {
273+ override suspend fun CommandContext. visible (): Boolean = hasPermission(permission)
274+ override suspend fun CommandContext. handle () {
275275 if (! visible()) returnReply(" [red]你没有执行该命令的权限" .with ())
276276 }
277277 }
@@ -281,11 +281,11 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
281281 open fun subCommands (): Map <String , CommandInfo > = nameMap
282282 fun getSub (name : String ): CommandInfo ? = subCommands()[name.lowercase()]
283283
284- context( CommandContext ) override fun canHandle (): Boolean = true
284+ override fun CommandContext. canHandle (): Boolean = true
285285 override suspend fun onComplete (context : CommandContext ) = context.run { handle() }
286286
287- context( CommandContext ) override suspend fun handle () {
288- context. onComplete(0 ) { subCommands().keys.toList() }
287+ override suspend fun CommandContext. handle () {
288+ onComplete(0 ) { subCommands().keys.toList() }
289289 if (arg.isEmpty()) return helpCommand.handle()
290290
291291 val name = arg.first()
@@ -395,13 +395,6 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
395395 addSub(this )
396396 }
397397
398- // compatibility for [CommandInfo.body]
399- @Deprecated(
400- " use CommandHandler instead" , level = DeprecationLevel .ERROR ,
401- replaceWith = ReplaceWith (" this.handle()" )
402- )
403- override suspend fun invoke (p1 : CommandContext ) = error(" use CommandHandler" )
404-
405398 object Root : Commands() {
406399 init {
407400 this + = CommandInfo (thisContextScript(), " ScriptAgent" , " ScriptAgent 控制指令" .with (), listOf (" sa" )).apply {
@@ -449,6 +442,13 @@ open class Commands : CommandHandler, TabCompleter, CommandHandlerOld {
449442 }
450443}
451444
445+ context(context: CommandContext )
446+ fun CommandHandler.canHandle () = context.canHandle()
447+ context(context: CommandContext )
448+ suspend inline fun CommandHandler.handle () = context.handle()
449+ context(context: CommandContext )
450+ suspend inline fun Commands.Hidden.visible () = context.visible()
451+
452452@ScriptDsl
453453inline fun Script.command (
454454 name : String ,
0 commit comments