12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package org.congressodeiradicali.karlmarx.coreplugins
- import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
- import com.bot4s.telegram.models.Message
- import org.congressodeiradicali.karlmarx.{Bot, Localizer, Plugin}
- import scala.concurrent.{ExecutionContext, Future}
- class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits {
- override val name = "Plugin Manager"
- override val description = "Manages other plugins"
- override val identifier = "pluginmanager"
- var managedPlugins: Map[String, Plugin] = Map()
- var pluginStatus: Map[String, Boolean] = Map()
- def managePlugin(plugin: Plugin) = {
- managedPlugins += (plugin.identifier -> plugin)
- pluginStatus += (plugin.identifier -> false)
- }
- def enableByIdentifier(id: String) : Boolean = managedPlugins.get(id).fold {
- false
- } { plugin =>
- bot.plugins :+= plugin
- pluginStatus = pluginStatus.updated(plugin.identifier, true)
- true
- }
- def disableByIdentifier(id: String) : Boolean = managedPlugins.get(id).fold { /*checks if the plugin is managed*/
- false
- } { plugin =>
- bot.plugins = bot.plugins.filter { _.identifier != plugin.identifier }
- pluginStatus = pluginStatus.updated(plugin.identifier, false)
- true
- }
- override var handlers: Map[CommandFilterMagnet, Handler] = Map {
- stringToCommandFilter("enable") -> { (_: Message, argv: Array[String]) =>
- val name = argv(1)
- val response = if (enableByIdentifier(name)) {
- "Enabled!"
- } else {
- "Plugin not found"
- }
- Future { Some(response) }(ExecutionContext.global)
- }
- stringToCommandFilter("disable") -> { (_: Message, argv: Array[String]) =>
- val name = argv(1)
- val response = if (disableByIdentifier(name)) {
- "Disabled!"
- } else {
- "Plugin not found"
- }
- Future { Some(response) }(ExecutionContext.global)
- }
- stringToCommandFilter("status") -> { (_, _) =>
- val response = pluginStatus.map { pair =>
- managedPlugins.get(pair._1).fold {
- // ignore and do the rest
- ""
- } { plugin =>
- s"${plugin.name}. Status: ${if (pair._2) "UP" else "DOWN"}"
- }
- }.mkString("\n")
- Future { Some(response) }(ExecutionContext.global)
- }
- }
- }
|