Add argv support

This commit is contained in:
CapacitorSet 2019-07-08 15:58:09 +02:00
parent 11cef30d8e
commit 13325e37cd
6 changed files with 42 additions and 38 deletions

View file

@ -55,21 +55,24 @@ class Bot(val token: String) extends TelegramBot
.accept(cmd) .accept(cmd)
} }
def runHandler(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command, message: Message): Future[String] = { def runHandler(handlerPair: Plugin#HandlerPair, message: Message, argv: Array[String]): Future[String] = {
if (runAgainst(handlerPair, botUser, cmd)) handlerPair._2(message, argv).flatMap {
handlerPair._2(message).flatMap { case Some(s) => Future(s)
case Some(s) => Future(s) case None => Future.failed(new RuntimeException("Handler did not return a message"))
case None => Future.failed(new RuntimeException("Handler did not return a message")) }
}
else
Future.failed(new RuntimeException("Filter does not apply"))
} }
onExtMessage { case (message, botUser) => onExtMessage { case (message, botUser) =>
val argv: Array[String] = message.text match {
case Some(text) => text.split(" ")
case None => Array()
}
using(command) { cmd => using(command) { cmd =>
Future.sequence(plugins Future.sequence(plugins
.flatMap(_.handlers) .flatMap(_.handlers)
.map(runHandler(_, botUser, cmd, message)) .filter(pair => runAgainst(pair, botUser, cmd))
.map(runHandler(_, message, argv))
.map(it => it.flatMap { it => reply(it)(message) })
).flatMap(_ => Future{unit}) // Discard the content ).flatMap(_ => Future{unit}) // Discard the content
}(message) }(message)
} }

View file

@ -20,6 +20,7 @@ package org.congressodeiradicali.karlmarx
object LocalizableString extends Enumeration { object LocalizableString extends Enumeration {
type LocalizableString = Value type LocalizableString = Value
val BAN_UNAUTHORIZED, BAN_SUCCESSFUL, BAN_FAILED, BAN_FAILED_INVALID_USER, BAN_FAILED_BAN_ADMIN, BAN_FAILED_REPLY, KICK_UNAUTHORIZED = Value val BAN_UNAUTHORIZED, BAN_SUCCESSFUL, BAN_FAILED, BAN_FAILED_INVALID_USER, BAN_FAILED_BAN_ADMIN, BAN_FAILED_REPLY, KICK_UNAUTHORIZED = Value
val NO_SUCH_PLUGIN, PLUGIN_ENABLED, PLUGIN_DISABLED = Value
} }
object Locale { object Locale {
@ -30,7 +31,11 @@ object Locale {
(LocalizableString.BAN_FAILED_INVALID_USER, "Cannot ban, invalid user"), (LocalizableString.BAN_FAILED_INVALID_USER, "Cannot ban, invalid user"),
(LocalizableString.BAN_FAILED_BAN_ADMIN, "Cannot ban an admin user"), (LocalizableString.BAN_FAILED_BAN_ADMIN, "Cannot ban an admin user"),
(LocalizableString.BAN_FAILED_REPLY, "Please reply to a message to ban someone"), (LocalizableString.BAN_FAILED_REPLY, "Please reply to a message to ban someone"),
(LocalizableString.KICK_UNAUTHORIZED, "Only an administrator can kick users") (LocalizableString.KICK_UNAUTHORIZED, "Only an administrator can kick users"),
(LocalizableString.NO_SUCH_PLUGIN, "No such plugin"),
(LocalizableString.PLUGIN_ENABLED, "Plugin enabled"),
(LocalizableString.PLUGIN_DISABLED, "Plugin disabled")
) )
val ITALIAN_LOCALE: Map[LocalizableString.Value, String] = Map( val ITALIAN_LOCALE: Map[LocalizableString.Value, String] = Map(
@ -40,7 +45,11 @@ object Locale {
(LocalizableString.BAN_FAILED_INVALID_USER, "Impossibile bannare, utente non valido"), (LocalizableString.BAN_FAILED_INVALID_USER, "Impossibile bannare, utente non valido"),
(LocalizableString.BAN_FAILED_BAN_ADMIN, "Non puoi bannare un admin"), (LocalizableString.BAN_FAILED_BAN_ADMIN, "Non puoi bannare un admin"),
(LocalizableString.BAN_FAILED_REPLY, "Rispondi a un messaggio per bannare qualcuno"), (LocalizableString.BAN_FAILED_REPLY, "Rispondi a un messaggio per bannare qualcuno"),
(LocalizableString.KICK_UNAUTHORIZED, "Solo un amministratore può kickare gli utenti") (LocalizableString.KICK_UNAUTHORIZED, "Solo un amministratore può kickare gli utenti"),
(LocalizableString.NO_SUCH_PLUGIN, "Il plugin selezionato non esiste"),
(LocalizableString.PLUGIN_ENABLED, "Plugin abilitato"),
(LocalizableString.PLUGIN_DISABLED, "Plugin disabilitato")
) )
} }

View file

@ -6,7 +6,7 @@ import com.bot4s.telegram.models.Message
import scala.concurrent.Future import scala.concurrent.Future
trait Plugin { trait Plugin {
type Handler = Message => Future[Option[String]] type Handler = (Message, Array[String]) => Future[Option[String]]
type HandlerPair = (CommandFilterMagnet, Handler) type HandlerPair = (CommandFilterMagnet, Handler)
val name: String val name: String

View file

@ -14,7 +14,7 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandI
override val identifier: String = "ban" override val identifier: String = "ban"
override var handlers: Map[CommandFilterMagnet, Handler] = Map { override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("ban") -> { msg => stringToCommandFilter("ban") -> { (msg, argv) =>
implicit val ec: ExecutionContext = ExecutionContext.global implicit val ec: ExecutionContext = ExecutionContext.global

View file

@ -13,6 +13,6 @@ class EchoPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with Command
override val identifier: String = "echo" override val identifier: String = "echo"
override var handlers: Map[CommandFilterMagnet, Handler] = Map { override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("echo") -> { msg => Future { msg.text }(ExecutionContext.global) } stringToCommandFilter("echo") -> { (_, argv) => Future { Some(argv.drop(1).mkString(" ")) }(ExecutionContext.global) }
} }
} }

View file

@ -38,45 +38,37 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit
} }
override var handlers: Map[CommandFilterMagnet, Handler] = Map { override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("enable") -> { msg : Message => stringToCommandFilter("enable") -> { (_: Message, argv: Array[String]) =>
val response = msg.text.fold { val name = argv(1)
"Enter a plugin name" val response = if (enableByIdentifier(name)) {
} { text => "Enabled!"
if (enableByIdentifier(text)) { } else {
"Enabled!" "Plugin not found"
} else {
"Plugin not found"
}
} }
Future { Some(response) }(ExecutionContext.global) Future { Some(response) }(ExecutionContext.global)
} }
stringToCommandFilter("disable") -> { msg : Message => stringToCommandFilter("disable") -> { (_: Message, argv: Array[String]) =>
val response = msg.text.fold { val name = argv(1)
"Enter a plugin name" val response = if (disableByIdentifier(name)) {
} { text => "Disabled!"
if (disableByIdentifier(text)) { } else {
"Disabled!" "Plugin not found"
} else {
"Plugin not found"
}
} }
Future { Some(response) }(ExecutionContext.global) Future { Some(response) }(ExecutionContext.global)
} }
stringToCommandFilter("status") -> { _ => stringToCommandFilter("status") -> { (_, _) =>
var response = "" val response = pluginStatus.map { pair =>
managedPlugins.get(pair._1).fold {
pluginStatus.foreach { pair =>
response += managedPlugins.get(pair._1).fold {
// ignore and do the rest // ignore and do the rest
"" ""
} { plugin => } { plugin =>
s"${plugin.name}. Status: ${if (pair._2) "UP" else "DOWN"}" s"${plugin.name}. Status: ${if (pair._2) "UP" else "DOWN"}"
} }
} }.mkString("\n")
Future { Some(response) }(ExecutionContext.global) Future { Some(response) }(ExecutionContext.global)
} }