Merged master
This commit is contained in:
commit
231661075b
4 changed files with 91 additions and 41 deletions
|
@ -0,0 +1,35 @@
|
|||
package org.congressodeiradicali.karlmarx
|
||||
import com.bot4s.telegram.api.declarative.CommandFilterMagnet
|
||||
import com.bot4s.telegram.models.Message
|
||||
|
||||
import scala.util.{Failure, Success}
|
||||
|
||||
class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin {
|
||||
// override var handlers: Map[CommandFilterMagnet, Handler] = ...
|
||||
def canBanUsers(msg: Message) : Boolean = msg.from.fold { false } {u =>
|
||||
val user = new BotUser(u, msg.chat, bot)
|
||||
// we should wait for the user to be ready
|
||||
user.syncAfterInit{user => user.canBanUsers}{_ => false}
|
||||
}
|
||||
|
||||
onCommand("ban", { msg =>
|
||||
val text = msg.replyToMessage.fold {
|
||||
LocalizableString.BAN_FAILED_REPLY
|
||||
} { message =>
|
||||
message.from.fold {
|
||||
LocalizableString.BAN_FAILED_INVALID_USER
|
||||
} { u =>
|
||||
val user = new BotUser(u, message.chat, bot)
|
||||
user.init() map {
|
||||
case Success(_) if user.isAdmin => LocalizableString.BAN_FAILED_BAN_ADMIN
|
||||
case Success(_) => {
|
||||
user.ban()
|
||||
LocalizableString.BAN_SUCCESSFUL
|
||||
}
|
||||
case Failure(_) => LocalizableString.BAN_FAILED
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(localizer.getString(text))
|
||||
})
|
||||
}
|
|
@ -21,14 +21,13 @@ package org.congressodeiradicali.karlmarx
|
|||
import cats.instances.future._
|
||||
import cats.syntax.functor._
|
||||
import com.bot4s.telegram.api.RequestHandler
|
||||
import com.bot4s.telegram.api.declarative.{Commands, whenOrElse}
|
||||
import com.bot4s.telegram.api.declarative.{Command, CommandFilterMagnet, Commands}
|
||||
import com.bot4s.telegram.clients.ScalajHttpClient
|
||||
import com.bot4s.telegram.future.{Polling, TelegramBot}
|
||||
import com.bot4s.telegram.models.Message
|
||||
import com.bot4s.telegram.models.{Message, User}
|
||||
import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
|
||||
|
||||
import scala.concurrent.Future
|
||||
import scala.util.{Failure, Success}
|
||||
|
||||
class Bot(val token: String) extends TelegramBot
|
||||
with Polling
|
||||
|
@ -64,31 +63,24 @@ class Bot(val token: String) extends TelegramBot
|
|||
user.syncAfterInit{user => user.canBanUsers}{_ => false}
|
||||
}
|
||||
|
||||
whenOrElse(onCommand("ban"), canBanUsers) { implicit msg =>
|
||||
msg.replyToMessage.fold {
|
||||
reply(localizer.getString("ban.reply_to_a_message")).void
|
||||
}
|
||||
{ message =>
|
||||
message.from.fold {
|
||||
reply(localizer.getString("ban.invalid_user")).void
|
||||
}
|
||||
{ u =>
|
||||
Future {
|
||||
val user = new BotUser(u, message.chat, this)
|
||||
user.init() onComplete {
|
||||
case Success(_) => {
|
||||
if (!user.isAdmin) {
|
||||
user.ban()
|
||||
reply(localizer.getString("ban.successful")).void
|
||||
} else reply(localizer.getString("ban.failed_ban_admin")).void
|
||||
}
|
||||
case Failure(_) => reply(localizer.getString("ban.failed")).void
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} /*else*/ { implicit msg =>
|
||||
reply(localizer.getString("ban.unauthorized")).void
|
||||
val plugins: List[Plugin]
|
||||
|
||||
val filter: CommandFilterMagnet = "ban"
|
||||
|
||||
def runAgainst(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command): Boolean = {
|
||||
handlerPair._1
|
||||
.to(botUser.flatMap(_.username)) // Apply filter to bot username
|
||||
.accept(cmd)
|
||||
}
|
||||
|
||||
onExtMessage { case (message, botUser) =>
|
||||
using(command) { cmd =>
|
||||
val promises = plugins
|
||||
.flatMap(_.handlers)
|
||||
.filter(runAgainst(_, botUser, cmd)) // Filter handlers that match the command
|
||||
.flatMap(_._2(message)) // Run the handler
|
||||
.map(reply(_)(message).void) // Send replies
|
||||
Future.sequence(promises).flatMap(_ => Future{unit}) // Discard the content
|
||||
}(message)
|
||||
}
|
||||
}
|
|
@ -17,27 +17,36 @@
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
object Locale {
|
||||
val ENGLISH_LOCALE: Map[String, String] = Map(
|
||||
"ban.unauthorized" -> "Only an administrator can ban users",
|
||||
"ban.successful" -> "User was banned successfully",
|
||||
"ban.failed" -> "Cannot ban, something went wrong",
|
||||
"ban.failed_ban_admin" -> "Cannot ban an admin user",
|
||||
"kick.unauthorized" -> "Only an administrator can kick users"
|
||||
val ENGLISH_LOCALE: Map[LocalizableString.Value, String] = Map(
|
||||
(LocalizableString.BAN_UNAUTHORIZED, "Only an administrator can ban users"),
|
||||
(LocalizableString.BAN_SUCCESSFUL, "User was banned successfully"),
|
||||
(LocalizableString.BAN_FAILED, "Cannot ban, something went wrong"),
|
||||
(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")
|
||||
)
|
||||
|
||||
val ITALIAN_LOCALE: Map[String, String] = Map(
|
||||
"ban.unauthorized" -> "Solo un amministratore può bannare gli utenti",
|
||||
"ban.successful" -> "Utente bannato con successo",
|
||||
"ban.failed" -> "Non sono riuscito a bannare, qualcosa è andato storto",
|
||||
"ban.failed_ban_admin" -> "Non puoi bannare un admin",
|
||||
"kick.unauthorized" -> "Solo un amministratore può kickare gli utenti"
|
||||
val ITALIAN_LOCALE: Map[LocalizableString.Value, String] = Map(
|
||||
(LocalizableString.BAN_UNAUTHORIZED, "Solo un amministratore può bannare gli utenti"),
|
||||
(LocalizableString.BAN_SUCCESSFUL, "Utente bannato con successo"),
|
||||
(LocalizableString.BAN_FAILED, "Non sono riuscito a bannare, qualcosa è andato storto"),
|
||||
(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")
|
||||
)
|
||||
}
|
||||
|
||||
class Localizer(val locale: String) {
|
||||
|
||||
def getString(name: String): String = locale match {
|
||||
def getString(name: LocalizableString.Value): String = locale match {
|
||||
case "en_EN" => Locale.ENGLISH_LOCALE(name)
|
||||
case "it_IT" => Locale.ITALIAN_LOCALE(name)
|
||||
case _ => Locale.ENGLISH_LOCALE(name)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package org.congressodeiradicali.karlmarx
|
||||
|
||||
import com.bot4s.telegram.api.declarative.{Action, CommandFilterMagnet}
|
||||
import com.bot4s.telegram.models.Message
|
||||
|
||||
trait Plugin {
|
||||
type Handler = Message => Option[String]
|
||||
type HandlerPair = (CommandFilterMagnet, Handler)
|
||||
var handlers: Map[CommandFilterMagnet, Handler]
|
||||
def onCommand(filter: CommandFilterMagnet, action: Handler): Unit = {
|
||||
val pair: HandlerPair = (filter, action)
|
||||
handlers += pair
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue