From c82e325d28f9846faffc5de27ac75fe69644be70 Mon Sep 17 00:00:00 2001 From: CapacitorSet <9286933+CapacitorSet@users.noreply.github.com> Date: Sun, 7 Jul 2019 11:39:07 +0200 Subject: [PATCH] Improve plugin interface --- .../karlmarx/BanPlugin.scala | 16 +++++++--------- .../karlmarx/EchoPlugin.scala | 12 ++++++++++++ .../congressodeiradicali/karlmarx/Plugin.scala | 5 ----- 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 src/main/scala/org/congressodeiradicali/karlmarx/EchoPlugin.scala diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/BanPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/BanPlugin.scala index 092b927..d4c2124 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/BanPlugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/BanPlugin.scala @@ -5,23 +5,21 @@ import com.bot4s.telegram.models.Message import scala.concurrent.{ExecutionContext, Future, blocking} class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits { - override var handlers: Map[CommandFilterMagnet, Handler] = _ - def canBanUsers(msg: Message) : Boolean = msg.from.fold { false } {u => val user = new BotUser(u, msg.chat, bot) // we should wait for the user to be ready user.syncAfterInit{user => user.canBanUsers}{_ => false} } - onCommand("ban") { implicit msg => - Future { + override var handlers: Map[CommandFilterMagnet, Handler] = Map { + stringToCommandFilter("ban") -> { msg => val text = msg.replyToMessage.fold { LocalizableString.BAN_FAILED_REPLY - } { message => - message.from.fold { + } { reply => + reply.from.fold { LocalizableString.BAN_FAILED_INVALID_USER } { u => - val user = new BotUser(u, message.chat, bot) + val user = new BotUser(u, msg.chat, bot) blocking { user.syncAfterInit { botuser => if (botuser.isAdmin || botuser.isCreator) { @@ -35,7 +33,7 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImpli } } - Some(localizer.getString(text)) - }(ExecutionContext.global) + Future {Some(localizer.getString(text))}(ExecutionContext.global) + } } } diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/EchoPlugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/EchoPlugin.scala new file mode 100644 index 0000000..0bb1445 --- /dev/null +++ b/src/main/scala/org/congressodeiradicali/karlmarx/EchoPlugin.scala @@ -0,0 +1,12 @@ +package org.congressodeiradicali.karlmarx + +import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits} +import com.bot4s.telegram.models.Message + +import scala.concurrent.{ExecutionContext, Future, blocking} + +class EchoPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits { + override var handlers: Map[CommandFilterMagnet, Handler] = Map { + stringToCommandFilter("echo") -> { msg => Future { msg.text }(ExecutionContext.global) } + } +} diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala index fca1baa..5175b6a 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Plugin.scala @@ -10,9 +10,4 @@ trait Plugin { type HandlerPair = (CommandFilterMagnet, Handler) var handlers: Map[CommandFilterMagnet, Handler] - - def onCommand(filter: CommandFilterMagnet)(action: Handler): Unit = { - val pair: HandlerPair = (filter, action) - handlers += pair - } } \ No newline at end of file