Small fixes

This commit is contained in:
CapacitorSet 2019-07-08 19:30:13 +02:00
parent e960c23d5b
commit 4baedb0804
4 changed files with 20 additions and 21 deletions

View file

@ -36,14 +36,16 @@ class Bot(val token: String) extends TelegramBot
LoggerConfig.level = LogLevel.TRACE
val localizer = new Localizer("it_IT")
def localize(s: LocalizableString.Value) = localizer.getString(s)
override val client: RequestHandler[Future] = new ScalajHttpClient(token)
def getLogger() = logger
var plugins: Map[String, Plugin] = Map(
"pluginmanager" -> new PluginManagerPlugin(localizer, this),
"echo" -> new EchoPlugin(localizer, this)
"pluginmanager" -> new PluginManagerPlugin(this),
"ban" -> new BanPlugin(this),
"echo" -> new EchoPlugin(this)
)
def runAgainst(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command): Boolean = {

View file

@ -6,7 +6,7 @@ import org.congressodeiradicali.karlmarx._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits {
class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
override val name: String = "Ban Plugin"
override val description: String = "Ban users and unban them"
@ -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, argv) =>
stringToCommandFilter("ban") -> { (msg, _) =>
implicit val ec: ExecutionContext = ExecutionContext.global
@ -23,23 +23,18 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandI
} { reply =>
reply.from.fold {
Future { LocalizableString.BAN_FAILED_INVALID_USER }
} { u =>
val user = new BotUser(u, msg.chat, bot)
user.init().map {
case Success(user) => if (user.isAdmin || user.isCreator) {
LocalizableString.BAN_FAILED_BAN_ADMIN
} else {
user.ban()
} { user =>
new BotUser(user, msg.chat, bot).init().map {
case Success(target) if target.isAdmin || target.isCreator => LocalizableString.BAN_FAILED_BAN_ADMIN
case Success(target) =>
target.ban()
LocalizableString.BAN_SUCCESSFUL
}
case Failure(_) => LocalizableString.BAN_FAILED
}
}
}
futureLocalizedString.map { s =>
Some(localizer.getString(s))
}
futureLocalizedString.map { s => Some(bot.localize(s)) }
}
}
}

View file

@ -5,7 +5,7 @@ import org.congressodeiradicali.karlmarx.{Bot, Localizer}
import scala.concurrent.{ExecutionContext, Future}
class EchoPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits {
class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
override val name: String = "Echo Plugin"
override val description: String = "An echoing plugin"

View file

@ -2,11 +2,14 @@ package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
import com.bot4s.telegram.models.Message
import org.congressodeiradicali.karlmarx.{Bot, LocalizableString, Localizer, Plugin}
import org.congressodeiradicali.karlmarx.{Bot, LocalizableString}
import scala.concurrent.{ExecutionContext, Future}
class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits {
class PluginManagerPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
// This is enabled by default for obvious reasons - users need to be able to enable other plugins on the first start.
// Todo: disable it when reasonable
enabled = true
override val name = "Plugin Manager"
override val description = "Manages other plugins"
@ -32,9 +35,8 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit
LocalizableString.PLUGIN_ENABLED
else
LocalizableString.NO_SUCH_PLUGIN
println(response)
Future { Some(localizer.getString(response)) }(ExecutionContext.global)
Future { Some(bot.localize(response)) }(ExecutionContext.global)
},
stringToCommandFilter("disable") -> { (_: Message, argv: Array[String]) =>
@ -44,7 +46,7 @@ class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin wit
else
LocalizableString.NO_SUCH_PLUGIN
Future { Some(localizer.getString(response)) }(ExecutionContext.global)
Future { Some(bot.localize(response)) }(ExecutionContext.global)
},
/*
stringToCommandFilter("plugins") -> { (_: Message, _: Array[String]) =>