Integrate Command with PermissionService

This commit is contained in:
Him188 2020-09-08 17:21:53 +08:00
parent 7638a7e759
commit 5c2a9a9553
17 changed files with 100 additions and 227 deletions

View File

@ -62,17 +62,14 @@ public object BuiltInCommands {
public object Managers : CompositeCommand( public object Managers : CompositeCommand(
ConsoleCommandOwner, "managers", ConsoleCommandOwner, "managers",
description = "Manage the managers for each bot", description = "Manage the managers for each bot"
permission = CommandPermission.Console or CommandPermission.Manager
), BuiltInCommand { ), BuiltInCommand {
@Permission(CommandPermission.Console::class)
@SubCommand @SubCommand
public suspend fun CommandSender.add(target: User) { public suspend fun CommandSender.add(target: User) {
target.bot.addManager(target.id) target.bot.addManager(target.id)
sendMessage("已成功添加 ${target.render()}${target.bot.render()} 的管理员") sendMessage("已成功添加 ${target.render()}${target.bot.render()} 的管理员")
} }
@Permission(CommandPermission.Console::class)
@SubCommand @SubCommand
public suspend fun CommandSender.remove(target: User) { public suspend fun CommandSender.remove(target: User) {
target.bot.removeManager(target.id) target.bot.removeManager(target.id)

View File

@ -15,7 +15,11 @@ import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.java.JCommand import net.mamoe.mirai.console.command.java.JCommand
import net.mamoe.mirai.console.internal.command.createCommandPermission
import net.mamoe.mirai.console.internal.command.isValidSubName import net.mamoe.mirai.console.internal.command.isValidSubName
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.SingleMessage import net.mamoe.mirai.message.data.SingleMessage
@ -51,7 +55,7 @@ public interface Command {
/** /**
* 指令权限 * 指令权限
*/ */
public val permission: CommandPermission public val permission: Permission
/** /**
* `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 * `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选
@ -95,13 +99,13 @@ public suspend inline fun Command.onCommand(sender: CommandSender, args: Message
* @see CompositeCommand * @see CompositeCommand
* @see RawCommand * @see RawCommand
*/ */
public abstract class AbstractCommand @JvmOverloads constructor( public abstract class AbstractCommand
@JvmOverloads constructor(
/** 指令拥有者. */ /** 指令拥有者. */
public override val owner: CommandOwner, public override val owner: CommandOwner,
vararg names: String, vararg names: String,
description: String = "<no description available>", description: String = "<no description available>",
/** 指令权限 */ basePermission: PermissionId? = null,
public override val permission: CommandPermission = CommandPermission.Default,
/** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */
public override val prefixOptional: Boolean = false public override val prefixOptional: Boolean = false
) : Command { ) : Command {
@ -111,4 +115,6 @@ public abstract class AbstractCommand @JvmOverloads constructor(
list.firstOrNull { !it.isValidSubName() }?.let { error("Invalid name: $it") } list.firstOrNull { !it.isValidSubName() }?.let { error("Invalid name: $it") }
}.toTypedArray() }.toTypedArray()
@OptIn(ExperimentalPermission::class)
public override val permission: Permission by lazy { createCommandPermission(basePermission) }
} }

View File

@ -10,6 +10,9 @@
package net.mamoe.mirai.console.command package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterAllCommands import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterAllCommands
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionIdNamespace
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
/** /**
@ -20,9 +23,12 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
* *
* @see JvmPlugin 是一个 [CommandOwner] * @see JvmPlugin 是一个 [CommandOwner]
*/ */
public interface CommandOwner public interface CommandOwner : PermissionIdNamespace
/** /**
* 代表控制台所有者. 所有的 mirai-console 内建的指令都属于 [ConsoleCommandOwner]. * 代表控制台所有者. 所有的 mirai-console 内建的指令都属于 [ConsoleCommandOwner].
*/ */
internal object ConsoleCommandOwner : CommandOwner internal object ConsoleCommandOwner : CommandOwner {
@ExperimentalPermission
override fun permissionId(id: String): PermissionId = PermissionId("console", id)
}

View File

@ -1,143 +0,0 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("unused", "NOTHING_TO_INLINE", "MemberVisibilityCanBePrivate")
package net.mamoe.mirai.console.command
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.internal.command.AndCommandPermissionImpl
import net.mamoe.mirai.console.internal.command.OrCommandPermissionImpl
import net.mamoe.mirai.console.util.BotManager.INSTANCE.isManager
import net.mamoe.mirai.contact.isAdministrator
import net.mamoe.mirai.contact.isOperator
import net.mamoe.mirai.contact.isOwner
/**
* 指令权限.
*
* [CommandManager.executeCommand] 时将会检查权限.
*
* @see Command.permission 从指令获取权限
*/
public fun interface CommandPermission {
/**
* 判断 [this] 是否拥有这个指令的权限
*
* @see CommandSender.hasPermission
* @see CommandPermission.testPermission
*/
public fun CommandSender.hasPermission(): Boolean
/**
* 满足两个权限其中一个即可使用指令
*/ // no extension for Java
public infix fun or(another: CommandPermission): CommandPermission = OrCommandPermissionImpl(this, another)
/**
* 同时拥有两个权限才能使用指令
*/ // no extension for Java
public infix fun and(another: CommandPermission): CommandPermission = AndCommandPermissionImpl(this, another)
/**
* 任何人都可以使用这个指令
*/
public object Any : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean = true
}
/**
* 任何人都不能使用这个指令. 指令只能通过调用 [Command.onCommand] 执行.
*/
public object None : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean = false
}
/**
* 来自任何 [Bot] 的任何一个管理员或群主都可以使用这个指令
*/
public object Operator : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean {
return this is MemberCommandSender && this.user.isOperator()
}
}
/**
* 来自任何 [Bot] 的任何一个群主都可以使用这个指令
*/
public object GroupOwner : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean {
return this is MemberCommandSender && this.user.isOwner()
}
}
/**
* 管理员 (不包含群主) 可以使用这个指令
*/
public object GroupAdmin : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean {
return this is MemberCommandSender && this.user.isAdministrator()
}
}
/**
* 任何 [Bot] manager 都可以使用这个指令
*/
public object Manager : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean {
return this is MemberCommandSender && this.user.isManager
}
}
/**
* 仅控制台能使用和这个指令
*/
public object Console : CommandPermission {
public override fun CommandSender.hasPermission(): Boolean = this is ConsoleCommandSender
}
/**
* 默认权限.
*
* @return [Manager] or [Console]
*/
public object Default : CommandPermission by (Manager or Console)
}
/**
* 判断 [this] 是否拥有权限 [permission]
*
* @see CommandSender.hasPermission
* @see CommandPermission.testPermission
* @see CommandPermission.hasPermission
*/
public inline fun CommandSender.hasPermission(permission: CommandPermission): Boolean =
permission.run { this@hasPermission.hasPermission() }
/**
* 判断 [sender] 是否拥有权限 [this]
*
* @see CommandSender.hasPermission
* @see CommandPermission.testPermission
* @see CommandPermission.hasPermission
*/
public inline fun CommandPermission.testPermission(sender: CommandSender): Boolean = this.run { sender.hasPermission() }
/**
* 判断 [sender] 是否拥有权限 [Command.permission]
*
* @see CommandSender.hasPermission
* @see CommandPermission.testPermission
* @see CommandPermission.hasPermission
*/
public inline fun Command.testPermission(sender: CommandSender): Boolean = sender.hasPermission(this.permission)

View File

@ -20,11 +20,12 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.description.* import net.mamoe.mirai.console.command.description.*
import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import kotlin.annotation.AnnotationRetention.RUNTIME import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.reflect.KClass
/** /**
* 复合指令. 指令注册时候会通过反射构造指令解析器. * 复合指令. 指令注册时候会通过反射构造指令解析器.
@ -85,10 +86,10 @@ public abstract class CompositeCommand(
owner: CommandOwner, owner: CommandOwner,
vararg names: String, vararg names: String,
description: String = "no description available", description: String = "no description available",
permission: CommandPermission = CommandPermission.Default, basePermission: PermissionId? = null,
prefixOptional: Boolean = false, prefixOptional: Boolean = false,
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext overrideContext: CommandArgumentContext = EmptyCommandArgumentContext
) : Command, AbstractReflectionCommand(owner, names, description, permission, prefixOptional), ) : Command, AbstractReflectionCommand(owner, names, description, basePermission, prefixOptional),
CommandArgumentContextAware { CommandArgumentContextAware {
/** /**
@ -112,7 +113,8 @@ public abstract class CompositeCommand(
/** 指定子指令要求的权限 */ /** 指定子指令要求的权限 */
@Retention(RUNTIME) @Retention(RUNTIME)
@Target(FUNCTION) @Target(FUNCTION)
protected annotation class Permission(val value: KClass<out CommandPermission>) @ExperimentalPermission
protected annotation class Permission(val value: String)
/** 指令描述 */ /** 指令描述 */
@Retention(RUNTIME) @Retention(RUNTIME)

View File

@ -14,6 +14,10 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.java.JRawCommand import net.mamoe.mirai.console.command.java.JRawCommand
import net.mamoe.mirai.console.internal.command.createCommandPermission
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
/** /**
@ -40,10 +44,13 @@ public abstract class RawCommand(
/** 指令描述, 用于显示在 [BuiltInCommands.Help] */ /** 指令描述, 用于显示在 [BuiltInCommands.Help] */
public override val description: String = "<no descriptions given>", public override val description: String = "<no descriptions given>",
/** 指令权限 */ /** 指令权限 */
public override val permission: CommandPermission = CommandPermission.Default, basePermission: PermissionId? = null,
/** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */
public override val prefixOptional: Boolean = false public override val prefixOptional: Boolean = false
) : Command { ) : Command {
@OptIn(ExperimentalPermission::class)
public override val permission: Permission by lazy { createCommandPermission(basePermission) }
/** /**
* 在指令被执行时调用. * 在指令被执行时调用.
* *

View File

@ -22,6 +22,7 @@ import net.mamoe.mirai.console.command.description.*
import net.mamoe.mirai.console.command.java.JSimpleCommand import net.mamoe.mirai.console.command.java.JSimpleCommand
import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand
import net.mamoe.mirai.console.internal.command.SimpleCommandSubCommandAnnotationResolver import net.mamoe.mirai.console.internal.command.SimpleCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
/** /**
@ -51,10 +52,10 @@ public abstract class SimpleCommand(
owner: CommandOwner, owner: CommandOwner,
vararg names: String, vararg names: String,
description: String = "no description available", description: String = "no description available",
permission: CommandPermission = CommandPermission.Default, basePermission: PermissionId? = null,
prefixOptional: Boolean = false, prefixOptional: Boolean = false,
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext overrideContext: CommandArgumentContext = EmptyCommandArgumentContext
) : Command, AbstractReflectionCommand(owner, names, description, permission, prefixOptional), ) : Command, AbstractReflectionCommand(owner, names, description, basePermission, prefixOptional),
CommandArgumentContextAware { CommandArgumentContextAware {
/** /**

View File

@ -9,8 +9,13 @@
package net.mamoe.mirai.console.command.java package net.mamoe.mirai.console.command.java
import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.BuiltInCommands
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.CompositeCommand
import net.mamoe.mirai.console.command.description.buildCommandArgumentContext import net.mamoe.mirai.console.command.description.buildCommandArgumentContext
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
/** /**
@ -64,16 +69,17 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
* @see buildCommandArgumentContext * @see buildCommandArgumentContext
*/ */
@ConsoleExperimentalAPI @ConsoleExperimentalAPI
public abstract class JCompositeCommand( public abstract class JCompositeCommand @JvmOverloads constructor(
owner: CommandOwner, owner: CommandOwner,
vararg names: String vararg names: String,
) : CompositeCommand(owner, *names) { basePermission: PermissionId? = null,
) : CompositeCommand(owner, *names, basePermission = basePermission) {
/** 指令描述, 用于显示在 [BuiltInCommands.Help] */ /** 指令描述, 用于显示在 [BuiltInCommands.Help] */
public final override var description: String = "<no descriptions given>" public final override var description: String = "<no descriptions given>"
protected set protected set
/** 指令权限 */ @OptIn(ExperimentalPermission::class)
public final override var permission: CommandPermission = CommandPermission.Default public final override var permission: net.mamoe.mirai.console.permission.Permission = super.permission
protected set protected set
/** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */

View File

@ -13,6 +13,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
import net.mamoe.mirai.console.internal.command.createCommandPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.SingleMessage import net.mamoe.mirai.message.data.SingleMessage
@ -42,14 +45,15 @@ import net.mamoe.mirai.message.data.SingleMessage
* *
* @see JRawCommand * @see JRawCommand
*/ */
public abstract class JRawCommand( public abstract class JRawCommand @JvmOverloads constructor(
/** /**
* 指令拥有者. * 指令拥有者.
* @see CommandOwner * @see CommandOwner
*/ */
public override val owner: CommandOwner, public override val owner: CommandOwner,
/** 指令名. 需要至少有一个元素. 所有元素都不能带有空格 */ /** 指令名. 需要至少有一个元素. 所有元素都不能带有空格 */
public override vararg val names: String public override vararg val names: String,
basePermission: PermissionId? = null,
) : Command { ) : Command {
/** 用法说明, 用于发送给用户 */ /** 用法说明, 用于发送给用户 */
public override var usage: String = "<no usages given>" public override var usage: String = "<no usages given>"
@ -60,7 +64,7 @@ public abstract class JRawCommand(
protected set protected set
/** 指令权限 */ /** 指令权限 */
public final override var permission: CommandPermission = CommandPermission.Default public final override var permission: Permission = createCommandPermission(basePermission)
protected set protected set
/** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */

View File

@ -12,9 +12,10 @@ package net.mamoe.mirai.console.command.java
import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.CommandOwner import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.CommandPermission
import net.mamoe.mirai.console.command.SimpleCommand import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.description.CommandArgumentContext import net.mamoe.mirai.console.command.description.CommandArgumentContext
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
/** /**
* Java 实现: * Java 实现:
@ -41,11 +42,12 @@ import net.mamoe.mirai.console.command.description.CommandArgumentContext
*/ */
public abstract class JSimpleCommand( public abstract class JSimpleCommand(
owner: CommandOwner, owner: CommandOwner,
vararg names: String vararg names: String,
) : SimpleCommand(owner, *names) { basePermission: PermissionId,
) : SimpleCommand(owner, *names, basePermission = basePermission) {
public override var description: String = super.description public override var description: String = super.description
protected set protected set
public override var permission: CommandPermission = super.permission public override var permission: Permission = super.permission
protected set protected set
public override var prefixOptional: Boolean = super.prefixOptional public override var prefixOptional: Boolean = super.prefixOptional
protected set protected set

View File

@ -1,32 +0,0 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.internal.command
import net.mamoe.mirai.console.command.CommandPermission
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.hasPermission
internal class OrCommandPermissionImpl(
private val first: CommandPermission,
private val second: CommandPermission
) : CommandPermission {
override fun CommandSender.hasPermission(): Boolean {
return this.hasPermission(first) || this.hasPermission(second)
}
}
internal class AndCommandPermissionImpl(
private val first: CommandPermission,
private val second: CommandPermission
) : CommandPermission {
override fun CommandSender.hasPermission(): Boolean {
return this.hasPermission(first) && this.hasPermission(second)
}
}

View File

@ -16,11 +16,15 @@ import net.mamoe.mirai.console.command.Command.Companion.primaryName
import net.mamoe.mirai.console.command.description.CommandArgumentContext import net.mamoe.mirai.console.command.description.CommandArgumentContext
import net.mamoe.mirai.console.command.description.CommandArgumentContextAware import net.mamoe.mirai.console.command.description.CommandArgumentContextAware
import net.mamoe.mirai.console.internal.data.kClassQualifiedNameOrTip import net.mamoe.mirai.console.internal.data.kClassQualifiedNameOrTip
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import kotlin.reflect.KAnnotatedElement import kotlin.reflect.KAnnotatedElement
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.KFunction import kotlin.reflect.KFunction
import kotlin.reflect.full.* import kotlin.reflect.full.callSuspend
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.isSubclassOf
internal object CompositeCommandSubCommandAnnotationResolver : internal object CompositeCommandSubCommandAnnotationResolver :
AbstractReflectionCommand.SubCommandAnnotationResolver { AbstractReflectionCommand.SubCommandAnnotationResolver {
@ -44,13 +48,13 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
owner: CommandOwner, owner: CommandOwner,
names: Array<out String>, names: Array<out String>,
description: String = "<no description available>", description: String = "<no description available>",
permission: CommandPermission = CommandPermission.Default, basePermission: PermissionId? = null,
prefixOptional: Boolean = false prefixOptional: Boolean = false
) : Command, AbstractCommand( ) : Command, AbstractCommand(
owner, owner,
names = names, names = names,
description = description, description = description,
permission = permission, basePermission = basePermission,
prefixOptional = prefixOptional prefixOptional = prefixOptional
), CommandArgumentContextAware { ), CommandArgumentContextAware {
internal abstract val subCommandAnnotationResolver: SubCommandAnnotationResolver internal abstract val subCommandAnnotationResolver: SubCommandAnnotationResolver
@ -70,7 +74,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy { internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy {
DefaultSubCommandDescriptor( DefaultSubCommandDescriptor(
"", "",
permission, createCommandPermission(basePermission),
onCommand = { sender: CommandSender, args: MessageChain -> onCommand = { sender: CommandSender, args: MessageChain ->
sender.onDefault(args) sender.onDefault(args)
} }
@ -115,7 +119,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
internal class DefaultSubCommandDescriptor( internal class DefaultSubCommandDescriptor(
val description: String, val description: String,
val permission: CommandPermission, val permission: Permission,
val onCommand: suspend (sender: CommandSender, rawArgs: MessageChain) -> Unit val onCommand: suspend (sender: CommandSender, rawArgs: MessageChain) -> Unit
) )
@ -123,7 +127,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor(
val names: Array<out String>, val names: Array<out String>,
val params: Array<CommandParameter<*>>, val params: Array<CommandParameter<*>>,
val description: String, val description: String,
val permission: CommandPermission, val permission: Permission,
val onCommand: suspend (sender: CommandSender, parsedArgs: Array<out Any>) -> Boolean, val onCommand: suspend (sender: CommandSender, parsedArgs: Array<out Any>) -> Boolean,
val context: CommandArgumentContext val context: CommandArgumentContext
) { ) {
@ -209,10 +213,6 @@ internal fun Any.flattenCommandComponents(): MessageChain = buildMessageChain {
internal inline fun <reified T : Annotation> KAnnotatedElement.hasAnnotation(): Boolean = internal inline fun <reified T : Annotation> KAnnotatedElement.hasAnnotation(): Boolean =
findAnnotation<T>() != null findAnnotation<T>() != null
internal inline fun <T : Any> KClass<out T>.getInstance(): T {
return this.objectInstance ?: this.createInstance()
}
internal val KClass<*>.qualifiedNameOrTip: String get() = this.qualifiedName ?: "<anonymous class>" internal val KClass<*>.qualifiedNameOrTip: String get() = this.qualifiedName ?: "<anonymous class>"
internal fun Array<AbstractReflectionCommand.SubCommandDescriptor>.createUsage(baseCommand: AbstractReflectionCommand): String = internal fun Array<AbstractReflectionCommand.SubCommandDescriptor>.createUsage(baseCommand: AbstractReflectionCommand): String =
@ -246,6 +246,7 @@ internal fun AbstractReflectionCommand.SubCommandDescriptor.createUsage(baseComm
appendLine() appendLine()
}.trimEnd() }.trimEnd()
@OptIn(ExperimentalPermission::class)
internal fun AbstractReflectionCommand.createSubCommand( internal fun AbstractReflectionCommand.createSubCommand(
function: KFunction<*>, function: KFunction<*>,
context: CommandArgumentContext context: CommandArgumentContext
@ -322,8 +323,8 @@ internal fun AbstractReflectionCommand.createSubCommand(
return SubCommandDescriptor( return SubCommandDescriptor(
commandName, commandName,
params, params,
subDescription, subDescription, // overridePermission?.value
overridePermission?.value?.getInstance() ?: permission, overridePermission?.value?.let { PermissionService.INSTANCE[PermissionId.parseFromString(it)] } ?: permission,
onCommand = { sender: CommandSender, args: Array<out Any> -> onCommand = { sender: CommandSender, args: Array<out Any> ->
val result = if (notStatic) { val result = if (notStatic) {
if (hasSenderParam) { if (hasSenderParam) {

View File

@ -10,6 +10,8 @@
package net.mamoe.mirai.console.internal.command package net.mamoe.mirai.console.internal.command
import net.mamoe.mirai.console.command.* import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.Command.Companion.primaryName
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
@ -136,12 +138,17 @@ internal fun Group.fuzzySearchMember(
} }
} }
@OptIn(ExperimentalPermission::class)
internal fun Command.createCommandPermission(basePermission: PermissionId?): Permission {
return PermissionService.INSTANCE.register(owner.permissionId(primaryName), description, basePermission)
}
//// internal //// internal
@JvmSynthetic @JvmSynthetic
internal inline fun <reified T> List<T>.dropToTypedArray(n: Int): Array<T> = Array(size - n) { this[n + it] } internal inline fun <reified T> List<T>.dropToTypedArray(n: Int): Array<T> = Array(size - n) { this[n + it] }
@OptIn(ExperimentalPermission::class)
@JvmSynthetic @JvmSynthetic
@Throws(CommandExecutionException::class) @Throws(CommandExecutionException::class)
internal suspend fun CommandSender.executeCommandInternal( internal suspend fun CommandSender.executeCommandInternal(
@ -150,7 +157,7 @@ internal suspend fun CommandSender.executeCommandInternal(
commandName: String, commandName: String,
checkPermission: Boolean checkPermission: Boolean
): CommandExecuteResult { ): CommandExecuteResult {
if (checkPermission && !command.testPermission(this)) { if (checkPermission && !command.permission.testPermission(this)) {
return CommandExecuteResult.PermissionDenied(command, commandName) return CommandExecuteResult.PermissionDenied(command, commandName)
} }

View File

@ -47,8 +47,8 @@ internal abstract class JvmPluginInternal(
resourceContainerDelegate.getResourceAsStream(path) resourceContainerDelegate.getResourceAsStream(path)
@OptIn(ExperimentalPermission::class) @OptIn(ExperimentalPermission::class)
override fun permissionIdentifier(identifierString: String): PermissionId { override fun permissionId(id: String): PermissionId {
return PermissionId(description.name, identifierString) return PermissionId(description.name, id)
} }
// region JvmPlugin // region JvmPlugin

View File

@ -14,7 +14,7 @@ import kotlin.reflect.KProperty
@ExperimentalPermission @ExperimentalPermission
public abstract class PermissionGroup( public abstract class PermissionGroup(
private val identifierNamespace: PermissionIdentifierNamespace, private val idNamespace: PermissionIdNamespace,
) { ) {
@ExperimentalPermission @ExperimentalPermission
public inner class PermissionBuilder { public inner class PermissionBuilder {
@ -45,7 +45,7 @@ public abstract class PermissionGroup(
public fun build(property: KProperty<*>): Permission { public fun build(property: KProperty<*>): Permission {
return PermissionService.INSTANCE.register( return PermissionService.INSTANCE.register(
identifierNamespace.permissionIdentifier(property.name), idNamespace.permissionId(property.name),
description, description,
basePermission basePermission
) )

View File

@ -36,12 +36,21 @@ public data class PermissionId(
public object AsStringSerializer : KSerializer<PermissionId> by String.serializer().map( public object AsStringSerializer : KSerializer<PermissionId> by String.serializer().map(
serializer = { it.namespace + ":" + it.id }, serializer = { it.namespace + ":" + it.id },
deserializer = { it.split(':').let { (namespace, id) -> PermissionId(namespace, id) } } deserializer = ::parseFromString
) )
public override fun toString(): String {
return "$namespace:$id"
}
public companion object {
public fun parseFromString(string: String): PermissionId =
string.split(':').let { (namespace, id) -> PermissionId(namespace, id) }
}
} }
@ExperimentalPermission @ExperimentalPermission
public interface PermissionIdentifierNamespace { public interface PermissionIdNamespace {
@ExperimentalPermission @ExperimentalPermission
public fun permissionIdentifier(identifierString: String): PermissionId public fun permissionId(id: String): PermissionId
} }

View File

@ -22,7 +22,7 @@ import net.mamoe.mirai.console.data.AutoSavePluginDataHolder
import net.mamoe.mirai.console.data.PluginConfig import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.PermissionIdentifierNamespace import net.mamoe.mirai.console.permission.PermissionIdNamespace
import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.PluginFileExtensions import net.mamoe.mirai.console.plugin.PluginFileExtensions
import net.mamoe.mirai.console.plugin.ResourceContainer import net.mamoe.mirai.console.plugin.ResourceContainer
@ -45,7 +45,7 @@ import net.mamoe.mirai.utils.MiraiLogger
*/ */
@OptIn(ExperimentalPermission::class) @OptIn(ExperimentalPermission::class)
public interface JvmPlugin : Plugin, CoroutineScope, public interface JvmPlugin : Plugin, CoroutineScope,
PluginFileExtensions, ResourceContainer, AutoSavePluginDataHolder, PermissionIdentifierNamespace { PluginFileExtensions, ResourceContainer, AutoSavePluginDataHolder, PermissionIdNamespace {
/** 日志 */ /** 日志 */
public val logger: MiraiLogger public val logger: MiraiLogger