Skip to content

Commit

Permalink
Fix @mention short-circuiting to Cleverbot chat
Browse files Browse the repository at this point in the history
By design, the bot should trigger Cleverbot any time an @mention
is used and no matching command is found for the invocation.
  • Loading branch information
DiamondIceNS committed Feb 9, 2018
1 parent 86b77f6 commit 869da13
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>

override fun broadcastAttachment(att: UrlAttachment) {
val msg = ComponentBuilder("${att.sender} sent ")
.color(net.md_5.bungee.api.ChatColor.ITALIC)
Expand Down Expand Up @@ -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<String> {
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<String, Command>).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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ class DbForgeServer(private val mod: ForgeDiscordBridge) : IDbServer {
override fun getLogger(): DbForgeLogger {
return DbForgeLogger(mod.logger)
}

override fun getAllCommandNames(): List<String> {
return FMLCommonHandler.instance().minecraftServerInstance.commandManager.commands.keys.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
return Sponge.getCommandManager().aliases.toList()
}
}

0 comments on commit 869da13

Please sign in to comment.