Browse Source

Check Command.owner, #216

Karlatemp 5 years ago
parent
commit
595c9480a6

+ 2 - 0
backend/mirai-console/src/command/AbstractCommand.kt

@@ -37,6 +37,8 @@ public abstract class AbstractCommand
 
     init {
         Command.checkCommandName(primaryName)
+        @Suppress("LeakingThis")
+        Command.checkCommandOwner(this)
         secondaryNames.forEach(Command.Companion::checkCommandName)
     }
 

+ 14 - 0
backend/mirai-console/src/command/Command.kt

@@ -117,5 +117,19 @@ public interface Command {
                 name.contains('.') -> throw IllegalArgumentException("'.' is forbidden in command name.")
             }
         }
+
+        /**
+         * 检查指令 [owner] 的合法性, 在非法时抛出 [IllegalArgumentException]
+         */
+        @JvmStatic
+        @Throws(IllegalArgumentException::class)
+        public fun checkCommandOwner(command: Command) {
+            val owner = command.owner
+            if (owner is ConsoleCommandOwner) {
+                if (command.javaClass.enclosingClass != BuiltInCommands::class.java) {
+                    throw IllegalArgumentException("ConsoleCommandOwner is not allowed")
+                }
+            }
+        }
     }
 }