Using futures for command handlers

This commit is contained in:
ekardnam 2019-07-07 08:38:25 +02:00
parent 231661075b
commit ac827cebf6
3 changed files with 38 additions and 24 deletions

View file

@ -1,8 +1,7 @@
package org.congressodeiradicali.karlmarx package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.api.declarative.CommandFilterMagnet
import com.bot4s.telegram.models.Message import com.bot4s.telegram.models.Message
import scala.util.{Failure, Success} import scala.concurrent.{ExecutionContext, Future, blocking}
class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin { class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin {
// override var handlers: Map[CommandFilterMagnet, Handler] = ... // override var handlers: Map[CommandFilterMagnet, Handler] = ...
@ -12,7 +11,8 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin {
user.syncAfterInit{user => user.canBanUsers}{_ => false} user.syncAfterInit{user => user.canBanUsers}{_ => false}
} }
onCommand("ban", { msg => onCommand("ban") { implicit msg =>
Future {
val text = msg.replyToMessage.fold { val text = msg.replyToMessage.fold {
LocalizableString.BAN_FAILED_REPLY LocalizableString.BAN_FAILED_REPLY
} { message => } { message =>
@ -20,16 +20,20 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin {
LocalizableString.BAN_FAILED_INVALID_USER LocalizableString.BAN_FAILED_INVALID_USER
} { u => } { u =>
val user = new BotUser(u, message.chat, bot) val user = new BotUser(u, message.chat, bot)
user.init() map { blocking {
case Success(_) if user.isAdmin => LocalizableString.BAN_FAILED_BAN_ADMIN user syncAfterInit { botuser =>
case Success(_) => { if (botuser.isAdmin || botuser.isCreator) {
LocalizableString.BAN_FAILED_BAN_ADMIN
} else {
user.ban() user.ban()
LocalizableString.BAN_SUCCESSFUL LocalizableString.BAN_SUCCESSFUL
} }
case Failure(_) => LocalizableString.BAN_FAILED } { _ => LocalizableString.BAN_FAILED }
} }
} }
} }
Some(localizer.getString(text)) Some(localizer.getString(text))
}) }(ExecutionContext.global)
}
} }

View file

@ -74,12 +74,18 @@ class Bot(val token: String) extends TelegramBot
} }
onExtMessage { case (message, botUser) => onExtMessage { case (message, botUser) =>
using(command) { cmd => using(command) { cmd =>
val promises = plugins val promises = plugins
.flatMap(_.handlers) .flatMap(_.handlers)
.filter(runAgainst(_, botUser, cmd)) // Filter handlers that match the command .filter(runAgainst(_, botUser, cmd)) // Filter handlers that match the command
.flatMap(_._2(message)) // Run the handler .map(_._2(message)) // Run the handler
.map(reply(_)(message).void) // Send replies .map {
_.map {
case Some(s) => reply(s)(message).void
}
} // Send replies
Future.sequence(promises).flatMap(_ => Future{unit}) // Discard the content Future.sequence(promises).flatMap(_ => Future{unit}) // Discard the content
}(message) }(message)
} }

View file

@ -1,13 +1,17 @@
package org.congressodeiradicali.karlmarx package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.api.declarative.{Action, CommandFilterMagnet} import com.bot4s.telegram.api.declarative.CommandFilterMagnet
import com.bot4s.telegram.models.Message import com.bot4s.telegram.models.Message
import scala.concurrent.Future
trait Plugin { trait Plugin {
type Handler = Message => Option[String] type Handler = Message => Future[Option[String]]
type HandlerPair = (CommandFilterMagnet, Handler) type HandlerPair = (CommandFilterMagnet, Handler)
var handlers: Map[CommandFilterMagnet, Handler] var handlers: Map[CommandFilterMagnet, Handler]
def onCommand(filter: CommandFilterMagnet, action: Handler): Unit = {
def onCommand(filter: CommandFilterMagnet)(action: Handler): Unit = {
val pair: HandlerPair = (filter, action) val pair: HandlerPair = (filter, action)
handlers += pair handlers += pair
} }