From 869da1396111bcfb0edef77081c6bb34c24d87f2 Mon Sep 17 00:00:00 2001 From: DiamondIceNS Date: Fri, 9 Feb 2018 11:32:14 -0600 Subject: [PATCH] Fix @mention short-circuiting to Cleverbot chat By design, the bot should trigger Cleverbot any time an @mention is used and no matching command is found for the invocation. --- .../discordbridge/wrapper/DbBukkitServer.kt | 30 +++++++++++++++++++ .../controller/BotControllerManager.kt | 5 +++- .../discordbridge/wrapper/IDbServer.kt | 3 ++ .../discordbridge/wrapper/DbForgeServer.kt | 4 +++ .../discordbridge/wrapper/DbSpongeServer.kt | 4 +++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/discordbridge-bukkit/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbBukkitServer.kt b/discordbridge-bukkit/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbBukkitServer.kt index f77c787..a114eef 100644 --- a/discordbridge-bukkit/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbBukkitServer.kt +++ b/discordbridge-bukkit/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbBukkitServer.kt @@ -9,9 +9,15 @@ import net.md_5.bungee.api.chat.ComponentBuilder import net.md_5.bungee.api.chat.HoverEvent import org.bukkit.Bukkit import org.bukkit.Server +import org.bukkit.command.Command +import org.bukkit.command.SimpleCommandMap +import org.bukkit.plugin.SimplePluginManager import java.util.* +import java.util.logging.Level class DbBukkitServer(private val plugin: BukkitDiscordBridge, private val bukkitServer: Server) : IDbServer { + lateinit var knownCommands: List + override fun broadcastAttachment(att: UrlAttachment) { val msg = ComponentBuilder("${att.sender} sent ") .color(net.md_5.bungee.api.ChatColor.ITALIC) @@ -52,4 +58,28 @@ class DbBukkitServer(private val plugin: BukkitDiscordBridge, private val bukkit override fun getLogger(): IDbLogger { return DbBukkitLogger(plugin.logger) } + + override fun getAllCommandNames(): List { + if (::knownCommands.isInitialized) + return knownCommands + try { + val pm = bukkitServer.pluginManager as SimplePluginManager? + if (pm != null) { + val cmapField = pm::class.java.getDeclaredField("commandMap") + cmapField.isAccessible = true + val cmap = cmapField.get(pm) as SimpleCommandMap + val knownCommandsField = cmap::class.java.getDeclaredField("knownCommands") + knownCommandsField.isAccessible = true + val kc = (knownCommandsField.get(cmap) as Map).keys.toList() + knownCommands = kc + return kc + } + plugin.logger.warning("Could not get command list - plugin manager is null") + return listOf() + } + catch (e: Exception) { + plugin.logger.log(Level.WARNING, "Could not get command list", e) + return listOf() + } + } } diff --git a/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/command/controller/BotControllerManager.kt b/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/command/controller/BotControllerManager.kt index b265e91..db02b41 100644 --- a/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/command/controller/BotControllerManager.kt +++ b/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/command/controller/BotControllerManager.kt @@ -199,7 +199,9 @@ class BotControllerManager { if (command == null) { // Attempt to run as a server command if sent from Discord - if (event is DiscordMessageWrapper && event.originalMessage.member.hasPermission(Permission.ADMINISTRATOR)) + val knownCommands = DiscordBridge.getServer().getAllCommandNames() + if (knownCommands.contains(commandName) && event is DiscordMessageWrapper + && event.originalMessage.member.hasPermission(Permission.ADMINISTRATOR)) if (invokeServerCommand(event, args)) return true if (defaultToTalk) command = commands["talk"] @@ -371,6 +373,7 @@ class BotControllerManager { val sender = DiscordCommandSender(event.senderName, event.channel) DiscordBridge.logDebug("Discord user ${event.senderName} invoked Minecraft command '${args.joinToString(" ")}'") DiscordBridge.getServer().dispatchCommand(sender, args.joinToString(" ")) + // TODO return true } diff --git a/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/wrapper/IDbServer.kt b/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/wrapper/IDbServer.kt index dd629ec..d342cbe 100644 --- a/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/wrapper/IDbServer.kt +++ b/discordbridge-core/src/main/kotlin/gg/obsidian/discordbridge/wrapper/IDbServer.kt @@ -20,4 +20,7 @@ interface IDbServer { fun getPlayer(uuid: UUID): IDbPlayer? fun getScheduler(): IDbScheduler + + // https://bukkit.org/threads/get-all-the-available-commands.61941/ + fun getAllCommandNames(): List } diff --git a/discordbridge-forge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbForgeServer.kt b/discordbridge-forge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbForgeServer.kt index 8c82733..2f331e1 100644 --- a/discordbridge-forge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbForgeServer.kt +++ b/discordbridge-forge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbForgeServer.kt @@ -52,4 +52,8 @@ class DbForgeServer(private val mod: ForgeDiscordBridge) : IDbServer { override fun getLogger(): DbForgeLogger { return DbForgeLogger(mod.logger) } + + override fun getAllCommandNames(): List { + return FMLCommonHandler.instance().minecraftServerInstance.commandManager.commands.keys.toList() + } } diff --git a/discordbridge-sponge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbSpongeServer.kt b/discordbridge-sponge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbSpongeServer.kt index 621a6d8..46ff18e 100644 --- a/discordbridge-sponge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbSpongeServer.kt +++ b/discordbridge-sponge/src/main/kotlin/gg/obsidian/discordbridge/wrapper/DbSpongeServer.kt @@ -54,4 +54,8 @@ class DbSpongeServer(private val plugin: SpongeDiscordBridge, private val game: override fun getLogger(): DbSpongeLogger { return DbSpongeLogger(plugin.getLogger()) } + + override fun getAllCommandNames(): List { + return Sponge.getCommandManager().aliases.toList() + } }