Skip to content

Commit

Permalink
Completely disable /help
Browse files Browse the repository at this point in the history
  • Loading branch information
CL-Jeremy committed Sep 21, 2020
1 parent cb25784 commit 606048e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
}

group = "plugin.com.github.cljeremy"
group = "com.github.cljeremy"
version = "0.1-SNAPSHOT"

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,64 @@ package com.github.cljeremy.MiraiConsoleHelpForwarder

import com.google.auto.service.AutoService
import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.console.permission.PermissionService.Companion.grantPermission
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.message.FriendMessageEvent
import net.mamoe.mirai.message.GroupMessageEvent
import net.mamoe.mirai.message.TempMessageEvent
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.asMessageChain
import net.mamoe.mirai.utils.currentTimeMillis


@AutoService(JvmPlugin::class)
object MyPluginMain : KotlinPlugin(
object PluginMain : KotlinPlugin(
JvmPluginDescription(
"com.github.cljeremy.mirai-console-help-forwarder",
"0.1.0"
)
) {

override fun onEnable() {
// override built-in help (unless in console)
// // grant everybody top-level permission of plugin
// AbstractPermitteeId.AnyUser.grantPermission(parentPermission)
// // override built-in help (unless in console)
logger.warning("Disabling built-in help command")
HelpCommand.register(override = true)
// grant everybody top-level permission of plugin
AbstractPermitteeId.AnyUser.grantPermission(parentPermission)
HelpCommand.unregister()
}

override fun onDisable() {
HelpCommand.unregister()
logger.warning("Enabling built-in help command")
BuiltInCommands.HelpCommand.register()
}
}

object HelpCommand : RawCommand(
MyPluginMain, "help",
PluginMain, "help",
description = "Help command to be forwarded"
) {
override suspend fun CommandSender.onCommand(args: MessageChain) {
// if executed from console, execute built-in help command
if (user == null) {
BuiltInCommands.HelpCommand.execute(this, args, false)
return
}
val time = (currentTimeMillis / 1000).toInt()
val verbatimCommand = "/help ${args.joinToString(" ")}"
fun asMessageChain() = PlainText(verbatimCommand).asMessageChain()
MyPluginMain.logger.info("Forwarding $verbatimCommand")
// broadcast event to relay original command message
val fromEvent = when (this) {
is CommandSenderOnMessage<*> ->
fromEvent
is FriendCommandSender ->
FriendMessageEvent(user, asMessageChain(), time)
is MemberCommandSender ->
GroupMessageEvent(name, permission as MemberPermission, user, asMessageChain(), time)
is TempCommandSender ->
TempMessageEvent(user, asMessageChain(), time)
else -> {
null
}
} ?: return
MyPluginMain.logger.info(fromEvent.toString())
fromEvent.broadcast()
// // if executed from console, execute built-in help command
// if (isNotUser()) {
// BuiltInCommands.HelpCommand.execute(this, args, false)
// return
// }
// unregister()
// PluginMain.logger.info("Forwarding ${
// (PlainText("/${names.joinToString(" ")}") + args).joinToString(" ")
// }")
// // broadcast event to relay original command message
// val fromEvent = when (this) {
// !is CommandSenderOnMessage<*> ->
// null
// is FriendCommandSender ->
// FriendMessageEvent(user, message, fromEvent.time)
// is MemberCommandSender ->
// GroupMessageEvent(name, user.permission, user, message, fromEvent.time)
// is TempCommandSender ->
// TempMessageEvent(user, message, fromEvent.time)
// else -> null
// } ?: return
// PluginMain.logger.info(fromEvent.toString())
// fromEvent.broadcast()
// register(override = true)
}
}
6 changes: 3 additions & 3 deletions src/test/kotlin/RunConsole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load
import net.mamoe.mirai.console.pure.MiraiConsolePureLoader
import com.github.cljeremy.MiraiConsoleHelpForwarder.MyPluginMain
import com.github.cljeremy.MiraiConsoleHelpForwarder.PluginMain

@net.mamoe.mirai.console.util.ConsoleExperimentalApi
suspend fun main() {
MiraiConsolePureLoader.startAsDaemon()

MyPluginMain.load() // 主动加载插件, Console 会调用 MyPluginMain.onLoad
MyPluginMain.enable() // 主动启用插件, Console 会调用 MyPluginMain.onEnable
PluginMain.load() // 主动加载插件, Console 会调用 MyPluginMain.onLoad
PluginMain.enable() // 主动启用插件, Console 会调用 MyPluginMain.onEnable

MiraiConsole.job.join()
}

0 comments on commit 606048e

Please sign in to comment.