Add argv support
This commit is contained in:
parent
11cef30d8e
commit
13325e37cd
6 changed files with 42 additions and 38 deletions
|
@ -55,21 +55,24 @@ class Bot(val token: String) extends TelegramBot
|
|||
.accept(cmd)
|
||||
}
|
||||
|
||||
def runHandler(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command, message: Message): Future[String] = {
|
||||
if (runAgainst(handlerPair, botUser, cmd))
|
||||
handlerPair._2(message).flatMap {
|
||||
case Some(s) => Future(s)
|
||||
case None => Future.failed(new RuntimeException("Handler did not return a message"))
|
||||
}
|
||||
else
|
||||
Future.failed(new RuntimeException("Filter does not apply"))
|
||||
def runHandler(handlerPair: Plugin#HandlerPair, message: Message, argv: Array[String]): Future[String] = {
|
||||
handlerPair._2(message, argv).flatMap {
|
||||
case Some(s) => Future(s)
|
||||
case None => Future.failed(new RuntimeException("Handler did not return a message"))
|
||||
}
|
||||
}
|
||||
|
||||
onExtMessage { case (message, botUser) =>
|
||||
val argv: Array[String] = message.text match {
|
||||
case Some(text) => text.split(" ")
|
||||
case None => Array()
|
||||
}
|
||||
using(command) { cmd =>
|
||||
Future.sequence(plugins
|
||||
.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
|
||||
}(message)
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.congressodeiradicali.karlmarx
|
|||
object LocalizableString extends Enumeration {
|
||||
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 NO_SUCH_PLUGIN, PLUGIN_ENABLED, PLUGIN_DISABLED = Value
|
||||
}
|
||||
|
||||
object Locale {
|
||||
|
@ -30,7 +31,11 @@ object Locale {
|
|||
(LocalizableString.BAN_FAILED_INVALID_USER, "Cannot ban, invalid user"),
|
||||
(LocalizableString.BAN_FAILED_BAN_ADMIN, "Cannot ban an admin user"),
|
||||
(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(
|
||||
|
@ -40,7 +45,11 @@ object Locale {
|
|||
(LocalizableString.BAN_FAILED_INVALID_USER, "Impossibile bannare, utente non valido"),
|
||||
(LocalizableString.BAN_FAILED_BAN_ADMIN, "Non puoi bannare un admin"),
|
||||
(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")
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.bot4s.telegram.models.Message
|
|||
import scala.concurrent.Future
|
||||
|
||||
trait Plugin {
|
||||
type Handler = Message => Future[Option[String]]
|
||||
type Handler = (Message, Array[String]) => Future[Option[String]]
|
||||
type HandlerPair = (CommandFilterMagnet, Handler)
|
||||
|
||||
val name: String
|
||||
|
|
|
@ -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 =>
|
||||
stringToCommandFilter("ban") -> { (msg, argv) =>
|
||||
|
||||
implicit val ec: ExecutionContext = ExecutionContext.global
|
||||
|
||||
|
|
|
@ -13,6 +13,6 @@ class EchoPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with Command
|
|||
override val identifier: String = "echo"
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,45 +38,37 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit
|
|||
}
|
||||
|
||||
override var handlers: Map[CommandFilterMagnet, Handler] = Map {
|
||||
stringToCommandFilter("enable") -> { msg : Message =>
|
||||
val response = msg.text.fold {
|
||||
"Enter a plugin name"
|
||||
} { text =>
|
||||
if (enableByIdentifier(text)) {
|
||||
"Enabled!"
|
||||
} else {
|
||||
"Plugin not found"
|
||||
}
|
||||
stringToCommandFilter("enable") -> { (_: Message, argv: Array[String]) =>
|
||||
val name = argv(1)
|
||||
val response = if (enableByIdentifier(name)) {
|
||||
"Enabled!"
|
||||
} else {
|
||||
"Plugin not found"
|
||||
}
|
||||
|
||||
Future { Some(response) }(ExecutionContext.global)
|
||||
}
|
||||
|
||||
stringToCommandFilter("disable") -> { msg : Message =>
|
||||
val response = msg.text.fold {
|
||||
"Enter a plugin name"
|
||||
} { text =>
|
||||
if (disableByIdentifier(text)) {
|
||||
"Disabled!"
|
||||
} else {
|
||||
"Plugin not found"
|
||||
}
|
||||
stringToCommandFilter("disable") -> { (_: Message, argv: Array[String]) =>
|
||||
val name = argv(1)
|
||||
val response = if (disableByIdentifier(name)) {
|
||||
"Disabled!"
|
||||
} else {
|
||||
"Plugin not found"
|
||||
}
|
||||
|
||||
Future { Some(response) }(ExecutionContext.global)
|
||||
}
|
||||
|
||||
stringToCommandFilter("status") -> { _ =>
|
||||
var response = ""
|
||||
|
||||
pluginStatus.foreach { pair =>
|
||||
response += managedPlugins.get(pair._1).fold {
|
||||
stringToCommandFilter("status") -> { (_, _) =>
|
||||
val response = pluginStatus.map { pair =>
|
||||
managedPlugins.get(pair._1).fold {
|
||||
// ignore and do the rest
|
||||
""
|
||||
} { plugin =>
|
||||
s"${plugin.name}. Status: ${if (pair._2) "UP" else "DOWN"}"
|
||||
}
|
||||
}
|
||||
}.mkString("\n")
|
||||
|
||||
Future { Some(response) }(ExecutionContext.global)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue