1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-03-25 06:50:09 +08:00
This commit is contained in:
Him188 2020-05-13 11:00:48 +08:00
parent 9a2f40fe03
commit 2aecd24c85
2 changed files with 31 additions and 14 deletions
mirai-console/src/main/kotlin/net/mamoe/mirai/console/command

View File

@ -16,11 +16,12 @@ import kotlin.contracts.contract
* this output type of that arg
* input is always String
*/
abstract class CommandArgParser<T : Any> {
abstract class CommandArgParser<out T : Any> {
abstract fun parse(s: String, sender: CommandSender): T
open fun parse(s: SingleMessage, sender: CommandSender): T = parse(s.content, sender)
}
@Suppress("unused")
@JvmSynthetic
inline fun CommandArgParser<*>.illegalArgument(message: String, cause: Throwable? = null): Nothing {
throw ParserException(message, cause)

View File

@ -17,18 +17,29 @@ import net.mamoe.mirai.contact.isAdministrator
import net.mamoe.mirai.contact.isOperator
import net.mamoe.mirai.contact.isOwner
/**
* 指令权限
*
* @see AnonymousCommandPermission
*/
abstract class CommandPermission {
/**
* 判断 [this] 是否拥有这个指令的权限
*/
abstract fun CommandSender.hasPermission(): Boolean
/**
* 满足两个权限其中一个即可使用指令
*/ // no extension for Java
infix fun or(another: CommandPermission): CommandPermission = OrCommandPermission(this, another)
/**
* 同时拥有两个权限才能使用指令
*/ // no extension for Java
infix fun and(another: CommandPermission): CommandPermission = AndCommandPermission(this, another)
/**
* 任何人都可以使用这个指令
*/
@ -149,6 +160,22 @@ abstract class CommandPermission {
object Console : CommandPermission() {
override fun CommandSender.hasPermission(): Boolean = false
}
companion object {
@JvmStatic
val Default: CommandPermission = Manager or Console
}
}
/**
* 使用 [lambda][block] 快速构造 [CommandPermission]
*/
@JvmSynthetic
@Suppress("FunctionName")
inline fun AnonymousCommandPermission(crossinline block: CommandSender.() -> Boolean): CommandPermission {
return object : CommandPermission() {
override fun CommandSender.hasPermission(): Boolean = block()
}
}
inline fun CommandSender.hasPermission(permission: CommandPermission): Boolean =
@ -158,17 +185,6 @@ inline fun CommandSender.hasPermission(permission: CommandPermission): Boolean =
inline fun CommandPermission.hasPermission(sender: CommandSender): Boolean = this.run { sender.hasPermission() }
/**
* 满足两个权限其中一个即可使用指令
*/
fun CommandPermission.or(another: CommandPermission): CommandPermission = OrCommandPermission(this, another)
/**
* 必须同时拥有两个权限才能使用这个指令
*/
fun CommandPermission.and(another: CommandPermission): CommandPermission = AndCommandPermission(this, another)
internal class OrCommandPermission(
private val first: CommandPermission,
private val second: CommandPermission