Skip to content

Commit

Permalink
Add restricted permission for mcp. fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Under-estimate committed Oct 10, 2024
1 parent 33edf97 commit 70c49f7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
## 权限列表
*有关权限部分的说明,参见[mirai-console的权限文档](https://github.com/mamoe/mirai-console/blob/master/docs/Permissions.md )*
根权限: `org.zrnq.mcmotd:*`
获取MC服务器信息: `org.zrnq.mcmotd:command.mcp`
绑定服务器到群聊: `org.zrnq.mcmotd:command.mcadd`
删除群聊绑定的服务器: `org.zrnq.mcmotd:command.mcdel`
启动/停止服务器的在线人数记录功能: `org.zrnq.mcmotd:command.mcrec`
获取http API访问计数: `org.zrnq.mcmotd:command.mcapi`
重载插件配置: `org.zrnq.mcmotd:command.mcreload`
+ 获取MC服务器信息: `org.zrnq.mcmotd:command.mcp`
+ 仅允许获取本群绑定的服务器信息: `org.zrnq.mcmotd:command.mcp.strict`
+ 绑定服务器到群聊: `org.zrnq.mcmotd:command.mcadd`
+ 删除群聊绑定的服务器: `org.zrnq.mcmotd:command.mcdel`
+ 启动/停止服务器的在线人数记录功能: `org.zrnq.mcmotd:command.mcrec`
+ 获取http API访问计数: `org.zrnq.mcmotd:command.mcapi`
+ 重载插件配置: `org.zrnq.mcmotd:command.mcreload`
## 插件命令
> /mcp (服务器地址/服务器名称) : 查询指定地址或绑定到指定名称上的服务器信息,当本群仅绑定了一个服务器时可省略参数。
>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.zrnq"
version = "1.2.1"
version = "1.2.2"
val ktor_version = "2.3.12"

repositories {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/zrnq/mcmotd/McMotd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ object McMotd : KotlinPlugin(
dataStorage = McMotdPluginData
configStorage.checkConfig()

QueryCommand.preparePermissions()

commandList = listOf(QueryCommand, BindCommand, DelCommand, RecordCommand, HttpServerCommand, ConfigReloadCommand)
commandList.forEach { it.register() }

Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/org/zrnq/mcmotd/MiraiCommandHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import kotlinx.coroutines.withContext
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.MemberCommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission
import net.mamoe.mirai.console.util.sendAnsiMessage
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import org.zrnq.mcmotd.output.APIOutputHandler
import org.zrnq.mcmotd.ImageUtil.appendPlayerHistory
import org.zrnq.mcmotd.McMotd.permissionId
import org.zrnq.mcmotd.McMotd.reload
import org.zrnq.mcmotd.data.McMotdPluginData
import org.zrnq.mcmotd.data.McMotdPluginConfig
Expand All @@ -26,6 +30,25 @@ import javax.imageio.ImageIO

@Suppress("unused")
object QueryCommand : SimpleCommand(McMotd, "mcp", description = "获取指定MC服务器的信息") {

override val permission: Permission
get() = strictPermission

private lateinit var strictPermission: Permission
private lateinit var relaxedPermission: Permission

fun preparePermissions() {
relaxedPermission = PermissionService.INSTANCE.register(
permissionId("command.mcp"),
"获取任意MC服务器的信息",
McMotd.parentPermission
)
strictPermission = PermissionService.INSTANCE.register(
permissionId("command.mcp.strict"),
"获取指定MC服务器的信息(仅限本群绑定的服务器)",
relaxedPermission)
}

@Handler
suspend fun MemberCommandSender.handle() {
val serverList = McMotdPluginData.getBoundServer(this.group.id)
Expand All @@ -46,6 +69,7 @@ object QueryCommand : SimpleCommand(McMotd, "mcp", description = "获取指定M
?.firstOrNull { it.first == target }
?.let { doPing(it.second); return }
}
if(!relaxedPermission.testPermission(this)) return
doPing(target)
}

Expand Down

0 comments on commit 70c49f7

Please sign in to comment.