File tree Expand file tree Collapse file tree 5 files changed +31
-31
lines changed
saltify-core/src/commonMain/kotlin/org/ntqqrev/saltify
saltify-docs/content/docs-core Expand file tree Collapse file tree 5 files changed +31
-31
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,6 @@ import org.ntqqrev.saltify.core.sendGroupMessage
88import org.ntqqrev.saltify.core.sendPrivateMessage
99import org.ntqqrev.saltify.core.text
1010import org.ntqqrev.saltify.dsl.SaltifyPluginContext
11- import org.ntqqrev.saltify.entity.SaltifyBotConfig
12- import org.ntqqrev.saltify.model.PermissionLevel
1311import org.ntqqrev.saltify.model.milky.SendMessageOutput
1412
1513/* *
@@ -36,17 +34,3 @@ public suspend inline fun Event.MessageReceive.respond(
3634 client : SaltifyApplication ,
3735 text : String
3836): SendMessageOutput = respond(client) { text(text) }
39-
40- /* *
41- * 获取发送者的权限等级。
42- */
43- public val Event .MessageReceive .senderPermissionLevel: PermissionLevel
44- get() = when (senderId) {
45- in SaltifyBotConfig .superUsers -> PermissionLevel .SuperUser
46- in SaltifyBotConfig .restrictedUsers -> PermissionLevel .Restricted
47- else -> when ((data as ? IncomingMessage .Group )?.groupMember?.role) {
48- " owner" -> PermissionLevel .GroupOwner
49- " admin" -> PermissionLevel .GroupAdmin
50- else -> PermissionLevel .Everyone
51- }
52- }
Original file line number Diff line number Diff line change 1+ package org.ntqqrev.saltify.extension
2+
3+ import org.ntqqrev.saltify.entity.SaltifyBotConfig
4+ import org.ntqqrev.saltify.model.PermissionLevel
5+
6+ /* *
7+ * 获取用户的权限等级。
8+ */
9+ public fun permissionLevelOf (targetId : Long ): PermissionLevel = when (targetId) {
10+ in SaltifyBotConfig .superUsers -> PermissionLevel .SuperUser
11+ in SaltifyBotConfig .restrictedUsers -> PermissionLevel .Restricted
12+ else -> PermissionLevel .Everyone
13+ }
Original file line number Diff line number Diff line change @@ -17,5 +17,20 @@ public fun SaltifyCommandRequirementContext.group(vararg targetId: Long): Comman
1717
1818public fun SaltifyCommandRequirementContext.perm (targetLevel : PermissionLevel ): CommandRequirement =
1919 CommandRequirement {
20- context.event.senderPermissionLevel >= targetLevel
20+ permissionLevelOf( context.event.senderId) >= targetLevel
2121 }
22+
23+ public val SaltifyCommandRequirementContext .isGroupAdmin: CommandRequirement
24+ get() = CommandRequirement {
25+ val data = context.event.data as ? IncomingMessage .Group
26+ data?.groupMember?.role == " admin"
27+ }
28+
29+ public val SaltifyCommandRequirementContext .isGroupOwner: CommandRequirement
30+ get() = CommandRequirement {
31+ val data = context.event.data as ? IncomingMessage .Group
32+ data?.groupMember?.role == " owner"
33+ }
34+
35+ public val SaltifyCommandRequirementContext .isGroupAdminOrOwner: CommandRequirement
36+ get() = isGroupAdmin or isGroupOwner
Original file line number Diff line number Diff line change @@ -16,16 +16,6 @@ public open class PermissionLevel(
1616 */
1717 public object Everyone : PermissionLevel(0 )
1818
19- /* *
20- * 群管理
21- */
22- public object GroupAdmin : PermissionLevel(100 )
23-
24- /* *
25- * 群主
26- */
27- public object GroupOwner : PermissionLevel(200 )
28-
2919 /* *
3020 * 超级用户
3121 */
Original file line number Diff line number Diff line change @@ -4,8 +4,6 @@ Saltify 提供了内置的一套简易权限管理方案。默认支持如下几
44
55- ** Restricted** – 受限制 (Int.MIN_VALUE)
66- ** Everyone** – 所有人 (0)
7- - ** GroupAdmin** – 群管理员 (100)
8- - ** GroupOwner** – 群主 (200)
97- ** SuperUser** – 最高权限 (Int.MAX_VALUE)
108
119可以这样使用权限 API:
@@ -21,10 +19,10 @@ client.command("stop") {
2119}
2220
2321client.on<Event .MessageReceive > { event ->
24- if (event.senderPermissionLevel >= PermissionLevel .GroupAdmin ) {
22+ if (permissionLevelOf( event.senderId) >= PermissionLevel .SuperUser ) {
2523 TODO ()
2624 }
2725}
2826```
2927
30- 需要注意的是,requirements 判定失败是默认返回的,所以上例1所示适用范围可能较窄 。
28+ 需要注意的是,requirements 判定失败是静默返回的,所以上例1所示适用情况可能较少 。
You can’t perform that action at this time.
0 commit comments