From 4baedb0804259f28d412181aec83239cd33365b8 Mon Sep 17 00:00:00 2001 From: CapacitorSet <9286933+CapacitorSet@users.noreply.github.com> Date: Mon, 8 Jul 2019 19:30:13 +0200 Subject: [PATCH] Small fixes --- .../congressodeiradicali/karlmarx/Bot.scala | 6 ++++-- .../karlmarx/coreplugins/BanPlugin.scala | 21 +++++++------------ .../karlmarx/coreplugins/EchoPlugin.scala | 2 +- .../coreplugins/PluginManagerPlugin.scala | 12 ++++++----- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala index 9cad96b..faad982 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Bot.scala @@ -36,14 +36,16 @@ class Bot(val token: String) extends TelegramBot LoggerConfig.level = LogLevel.TRACE val localizer = new Localizer("it_IT") + def localize(s: LocalizableString.Value) = localizer.getString(s) override val client: RequestHandler[Future] = new ScalajHttpClient(token) def getLogger() = logger var plugins: Map[String, Plugin] = Map( - "pluginmanager" -> new PluginManagerPlugin(localizer, this), - "echo" -> new EchoPlugin(localizer, this) + "pluginmanager" -> new PluginManagerPlugin(this), + "ban" -> new BanPlugin(this), + "echo" -> new EchoPlugin(this) ) def runAgainst(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command): Boolean = { diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala index 27f0856..348e800 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/BanPlugin.scala @@ -6,7 +6,7 @@ import org.congressodeiradicali.karlmarx._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} -class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits { +class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits { override val name: String = "Ban Plugin" override val description: String = "Ban users and unban them" @@ -14,7 +14,7 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandI override val identifier: String = "ban" override var handlers: Map[CommandFilterMagnet, Handler] = Map { - stringToCommandFilter("ban") -> { (msg, argv) => + stringToCommandFilter("ban") -> { (msg, _) => implicit val ec: ExecutionContext = ExecutionContext.global @@ -23,23 +23,18 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandI } { reply => reply.from.fold { Future { LocalizableString.BAN_FAILED_INVALID_USER } - } { u => - val user = new BotUser(u, msg.chat, bot) - user.init().map { - case Success(user) => if (user.isAdmin || user.isCreator) { - LocalizableString.BAN_FAILED_BAN_ADMIN - } else { - user.ban() + } { user => + new BotUser(user, msg.chat, bot).init().map { + case Success(target) if target.isAdmin || target.isCreator => LocalizableString.BAN_FAILED_BAN_ADMIN + case Success(target) => + target.ban() LocalizableString.BAN_SUCCESSFUL - } case Failure(_) => LocalizableString.BAN_FAILED } } } - futureLocalizedString.map { s => - Some(localizer.getString(s)) - } + futureLocalizedString.map { s => Some(bot.localize(s)) } } } } diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala index 3b46da9..c1201cd 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/EchoPlugin.scala @@ -5,7 +5,7 @@ import org.congressodeiradicali.karlmarx.{Bot, Localizer} import scala.concurrent.{ExecutionContext, Future} -class EchoPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits { +class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits { override val name: String = "Echo Plugin" override val description: String = "An echoing plugin" diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala index 742c063..bd5681a 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/coreplugins/PluginManagerPlugin.scala @@ -2,11 +2,14 @@ package org.congressodeiradicali.karlmarx.coreplugins import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} import com.bot4s.telegram.models.Message -import org.congressodeiradicali.karlmarx.{Bot, LocalizableString, Localizer, Plugin} +import org.congressodeiradicali.karlmarx.{Bot, LocalizableString} import scala.concurrent.{ExecutionContext, Future} -class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits { +class PluginManagerPlugin(bot: Bot) extends CorePlugin with CommandImplicits { + // This is enabled by default for obvious reasons - users need to be able to enable other plugins on the first start. + // Todo: disable it when reasonable + enabled = true override val name = "Plugin Manager" override val description = "Manages other plugins" @@ -32,9 +35,8 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit LocalizableString.PLUGIN_ENABLED else LocalizableString.NO_SUCH_PLUGIN - println(response) - Future { Some(localizer.getString(response)) }(ExecutionContext.global) + Future { Some(bot.localize(response)) }(ExecutionContext.global) }, stringToCommandFilter("disable") -> { (_: Message, argv: Array[String]) => @@ -44,7 +46,7 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit else LocalizableString.NO_SUCH_PLUGIN - Future { Some(localizer.getString(response)) }(ExecutionContext.global) + Future { Some(bot.localize(response)) }(ExecutionContext.global) }, /* stringToCommandFilter("plugins") -> { (_: Message, _: Array[String]) =>