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,24 +11,29 @@ 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 =>
val text = msg.replyToMessage.fold { Future {
LocalizableString.BAN_FAILED_REPLY val text = msg.replyToMessage.fold {
} { message => LocalizableString.BAN_FAILED_REPLY
message.from.fold { } { message =>
LocalizableString.BAN_FAILED_INVALID_USER message.from.fold {
} { u => LocalizableString.BAN_FAILED_INVALID_USER
val user = new BotUser(u, message.chat, bot) } { u =>
user.init() map { val user = new BotUser(u, message.chat, bot)
case Success(_) if user.isAdmin => LocalizableString.BAN_FAILED_BAN_ADMIN blocking {
case Success(_) => { user syncAfterInit { botuser =>
user.ban() if (botuser.isAdmin || botuser.isCreator) {
LocalizableString.BAN_SUCCESSFUL LocalizableString.BAN_FAILED_BAN_ADMIN
} else {
user.ban()
LocalizableString.BAN_SUCCESSFUL
}
} { _ => LocalizableString.BAN_FAILED }
} }
case Failure(_) => 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
} }