mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-25 06:50:09 +08:00
Cleanup
This commit is contained in:
parent
9a2f40fe03
commit
2aecd24c85
mirai-console/src/main/kotlin/net/mamoe/mirai/console/command
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user