|
@@ -2,7 +2,7 @@ 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 org.congressodeiradicali.karlmarx.{Bot, LocalizableString, Localizer, Plugin}
|
|
|
|
|
|
import scala.concurrent.{ExecutionContext, Future}
|
|
|
|
|
@@ -13,64 +13,43 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit
|
|
|
|
|
|
override val identifier = "pluginmanager"
|
|
|
|
|
|
- var managedPlugins: Map[String, Plugin] = Map()
|
|
|
- var pluginStatus: Map[String, Boolean] = Map()
|
|
|
+ def enableByIdentifier(id: String): Boolean =
|
|
|
+ if (bot.plugins.contains(id)) {
|
|
|
+ bot.plugins(id).enabled = true
|
|
|
+ true
|
|
|
+ } else false
|
|
|
|
|
|
- def managePlugin(plugin: Plugin) = {
|
|
|
- managedPlugins += (plugin.identifier -> plugin)
|
|
|
- pluginStatus += (plugin.identifier -> false)
|
|
|
- }
|
|
|
+ def disableByIdentifier(id: String): Boolean =
|
|
|
+ if (bot.plugins.keySet.contains(id)) {
|
|
|
+ bot.plugins(id).enabled = false
|
|
|
+ true
|
|
|
+ } else 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 {
|
|
|
+ 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"
|
|
|
- }
|
|
|
+ val response = if (enableByIdentifier(name))
|
|
|
+ LocalizableString.PLUGIN_ENABLED
|
|
|
+ else
|
|
|
+ LocalizableString.NO_SUCH_PLUGIN
|
|
|
+ println(response)
|
|
|
|
|
|
- Future { Some(response) }(ExecutionContext.global)
|
|
|
- }
|
|
|
+ Future { Some(localizer.getString(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)
|
|
|
+ val response = if (disableByIdentifier(name))
|
|
|
+ LocalizableString.PLUGIN_DISABLED
|
|
|
+ else
|
|
|
+ LocalizableString.NO_SUCH_PLUGIN
|
|
|
+
|
|
|
+ Future { Some(localizer.getString(response)) }(ExecutionContext.global)
|
|
|
+ },
|
|
|
+ /*
|
|
|
+ stringToCommandFilter("plugins") -> { (_: Message, _: Array[String]) =>
|
|
|
+ Future { Some(bot.plugins.values.map("- " + _.name).mkString("\n")) }(ExecutionContext.global)
|
|
|
}
|
|
|
- }
|
|
|
+ */
|
|
|
+ )
|
|
|
}
|