Commit e5404b39 authored by Him188's avatar Him188

Simplify MemberPermission comparing

parent c2fa014c
...@@ -188,7 +188,7 @@ internal class QQAndroidBot constructor( ...@@ -188,7 +188,7 @@ internal class QQAndroidBot constructor(
} }
} }
group.checkBotPermissionOperator() group.checkBotPermission(MemberPermission.ADMINISTRATOR)
} }
override suspend fun ignoreMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean) { override suspend fun ignoreMemberJoinRequest(event: MemberJoinRequestEvent, blackList: Boolean) {
...@@ -369,7 +369,7 @@ internal abstract class QQAndroidBotBase constructor( ...@@ -369,7 +369,7 @@ internal abstract class QQAndroidBotBase constructor(
else -> error("stub") else -> error("stub")
} }
if (this.id != source.fromId) { if (this.id != source.fromId) {
group.checkBotPermissionOperator() group.checkBotPermission(MemberPermission.ADMINISTRATOR)
} }
MessageRecallEvent.GroupRecall( MessageRecallEvent.GroupRecall(
this, this,
......
...@@ -109,7 +109,8 @@ internal class GroupImpl( ...@@ -109,7 +109,8 @@ internal class GroupImpl(
override var name: String override var name: String
get() = _name get() = _name
set(newValue) { set(newValue) {
checkBotPermissionOperator()
checkBotPermission(MemberPermission.ADMINISTRATOR)
if (_name != newValue) { if (_name != newValue) {
val oldValue = _name val oldValue = _name
_name = newValue _name = newValue
...@@ -131,7 +132,7 @@ internal class GroupImpl( ...@@ -131,7 +132,7 @@ internal class GroupImpl(
override var entranceAnnouncement: String override var entranceAnnouncement: String
get() = _announcement get() = _announcement
set(newValue) { set(newValue) {
checkBotPermissionOperator() checkBotPermission(MemberPermission.ADMINISTRATOR)
if (_announcement != newValue) { if (_announcement != newValue) {
val oldValue = _announcement val oldValue = _announcement
_announcement = newValue _announcement = newValue
...@@ -152,7 +153,7 @@ internal class GroupImpl( ...@@ -152,7 +153,7 @@ internal class GroupImpl(
override var isAllowMemberInvite: Boolean override var isAllowMemberInvite: Boolean
get() = _allowMemberInvite get() = _allowMemberInvite
set(newValue) { set(newValue) {
checkBotPermissionOperator() checkBotPermission(MemberPermission.ADMINISTRATOR)
if (_allowMemberInvite != newValue) { if (_allowMemberInvite != newValue) {
val oldValue = _allowMemberInvite val oldValue = _allowMemberInvite
_allowMemberInvite = newValue _allowMemberInvite = newValue
...@@ -186,7 +187,8 @@ internal class GroupImpl( ...@@ -186,7 +187,8 @@ internal class GroupImpl(
override var isConfessTalkEnabled: Boolean override var isConfessTalkEnabled: Boolean
get() = _confessTalk get() = _confessTalk
set(newValue) { set(newValue) {
checkBotPermissionOperator()
checkBotPermission(MemberPermission.ADMINISTRATOR)
if (_confessTalk != newValue) { if (_confessTalk != newValue) {
val oldValue = _confessTalk val oldValue = _confessTalk
_confessTalk = newValue _confessTalk = newValue
...@@ -207,7 +209,8 @@ internal class GroupImpl( ...@@ -207,7 +209,8 @@ internal class GroupImpl(
override var isMuteAll: Boolean override var isMuteAll: Boolean
get() = _muteAll get() = _muteAll
set(newValue) { set(newValue) {
checkBotPermissionOperator()
checkBotPermission(MemberPermission.ADMINISTRATOR)
if (_muteAll != newValue) { if (_muteAll != newValue) {
val oldValue = _muteAll val oldValue = _muteAll
_muteAll = newValue _muteAll = newValue
......
...@@ -105,7 +105,7 @@ internal class MemberImpl constructor( ...@@ -105,7 +105,7 @@ internal class MemberImpl constructor(
get() = _nameCard get() = _nameCard
set(newValue) { set(newValue) {
if (id != bot.id) { if (id != bot.id) {
group.checkBotPermissionOperator() group.checkBotPermission(MemberPermission.ADMINISTRATOR)
} }
if (_nameCard != newValue) { if (_nameCard != newValue) {
val oldValue = _nameCard val oldValue = _nameCard
......
...@@ -7,17 +7,25 @@ ...@@ -7,17 +7,25 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:Suppress("NOTHING_TO_INLINE") @file:Suppress("NOTHING_TO_INLINE", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.utils.SinceMirai import net.mamoe.mirai.utils.SinceMirai
import kotlin.internal.InlineOnly
/** /**
* 群成员的权限. * 群成员的权限.
* *
* 可通过 [compareTo] 判断是否有更高的权限. * 可通过 [compareTo] 判断是否有更高的权限.
*
* @see isOwner 判断权限是否为群主
* @see isOperator 判断权限是否为管理员或群主
*
* @see Member.isOwner 对 [Member] 的扩展函数, 判断此成员是否为群主
* @see Member.isOperator 对 [Member] 的扩展函数, 判断此成员是否为管理员或群主
* @see Member.isAdministrator 对 [Member] 的扩展函数, 判断此成员是否为管理员
*/ */
enum class MemberPermission : Comparable<MemberPermission> { enum class MemberPermission : Comparable<MemberPermission> {
/** /**
...@@ -46,16 +54,19 @@ enum class MemberPermission : Comparable<MemberPermission> { ...@@ -46,16 +54,19 @@ enum class MemberPermission : Comparable<MemberPermission> {
/** /**
* 判断权限是否为群主 * 判断权限是否为群主
*/ */
@InlineOnly
inline fun MemberPermission.isOwner(): Boolean = this == MemberPermission.OWNER inline fun MemberPermission.isOwner(): Boolean = this == MemberPermission.OWNER
/** /**
* 判断权限是否为管理员 * 判断权限是否为管理员
*/ */
@InlineOnly
inline fun MemberPermission.isAdministrator(): Boolean = this == MemberPermission.ADMINISTRATOR inline fun MemberPermission.isAdministrator(): Boolean = this == MemberPermission.ADMINISTRATOR
/** /**
* 判断权限是否为管理员或群主 * 判断权限是否为管理员或群主
*/ */
@InlineOnly
inline fun MemberPermission.isOperator(): Boolean = isAdministrator() || isOwner() inline fun MemberPermission.isOperator(): Boolean = isAdministrator() || isOwner()
...@@ -85,7 +96,7 @@ class PermissionDeniedException : IllegalStateException { ...@@ -85,7 +96,7 @@ class PermissionDeniedException : IllegalStateException {
} }
/** /**
* 要求 [Bot] 在这个群里的权限为 [required], 否则抛出异常 [PermissionDeniedException] * 要求 [Bot] 在这个群里的权限至少为 [required], 否则抛出异常 [PermissionDeniedException]
* *
* @throws PermissionDeniedException * @throws PermissionDeniedException
*/ */
...@@ -95,7 +106,7 @@ inline fun Group.checkBotPermission( ...@@ -95,7 +106,7 @@ inline fun Group.checkBotPermission(
"Permission denied: required $required, got actual $botPermission for $bot in group $id" "Permission denied: required $required, got actual $botPermission for $bot in group $id"
} }
) { ) {
if (botPermission != required) { if (botPermission < required) {
throw PermissionDeniedException(lazyMessage()) throw PermissionDeniedException(lazyMessage())
} }
} }
...@@ -105,12 +116,9 @@ inline fun Group.checkBotPermission( ...@@ -105,12 +116,9 @@ inline fun Group.checkBotPermission(
* *
* @throws PermissionDeniedException * @throws PermissionDeniedException
*/ */
@Deprecated("use checkBotPermission", ReplaceWith("checkBotPermission(MemberPermission.ADMINISTRATOR)"))
inline fun Group.checkBotPermissionOperator( inline fun Group.checkBotPermissionOperator(
crossinline lazyMessage: () -> String = { crossinline lazyMessage: () -> String = {
"Permission denied: required ${MemberPermission.ADMINISTRATOR} or ${MemberPermission.OWNER}, got actual $botPermission for $bot in group $id" "Permission denied: required ${MemberPermission.ADMINISTRATOR} or ${MemberPermission.OWNER}, got actual $botPermission for $bot in group $id"
} }
) { ) = checkBotPermission(MemberPermission.ADMINISTRATOR, lazyMessage)
if (!botPermission.isOperator()) { \ No newline at end of file
throw PermissionDeniedException(lazyMessage())
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment