Merged master

This commit is contained in:
ekardnam 2019-07-07 07:54:08 +02:00
commit 231661075b
4 changed files with 91 additions and 41 deletions

View file

@ -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))
})
}

View file

@ -21,14 +21,13 @@ package org.congressodeiradicali.karlmarx
import cats.instances.future._ import cats.instances.future._
import cats.syntax.functor._ import cats.syntax.functor._
import com.bot4s.telegram.api.RequestHandler 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.clients.ScalajHttpClient
import com.bot4s.telegram.future.{Polling, TelegramBot} 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 slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
import scala.concurrent.Future import scala.concurrent.Future
import scala.util.{Failure, Success}
class Bot(val token: String) extends TelegramBot class Bot(val token: String) extends TelegramBot
with Polling with Polling
@ -64,31 +63,24 @@ class Bot(val token: String) extends TelegramBot
user.syncAfterInit{user => user.canBanUsers}{_ => false} user.syncAfterInit{user => user.canBanUsers}{_ => false}
} }
whenOrElse(onCommand("ban"), canBanUsers) { implicit msg => val plugins: List[Plugin]
msg.replyToMessage.fold {
reply(localizer.getString("ban.reply_to_a_message")).void val filter: CommandFilterMagnet = "ban"
}
{ message => def runAgainst(handlerPair: Plugin#HandlerPair, botUser: Option[User], cmd: Command): Boolean = {
message.from.fold { handlerPair._1
reply(localizer.getString("ban.invalid_user")).void .to(botUser.flatMap(_.username)) // Apply filter to bot username
} .accept(cmd)
{ 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
} }
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)
}
} }

View file

@ -17,27 +17,36 @@
package org.congressodeiradicali.karlmarx 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 { object Locale {
val ENGLISH_LOCALE: Map[String, String] = Map( val ENGLISH_LOCALE: Map[LocalizableString.Value, String] = Map(
"ban.unauthorized" -> "Only an administrator can ban users", (LocalizableString.BAN_UNAUTHORIZED, "Only an administrator can ban users"),
"ban.successful" -> "User was banned successfully", (LocalizableString.BAN_SUCCESSFUL, "User was banned successfully"),
"ban.failed" -> "Cannot ban, something went wrong", (LocalizableString.BAN_FAILED, "Cannot ban, something went wrong"),
"ban.failed_ban_admin" -> "Cannot ban an admin user", (LocalizableString.BAN_FAILED_INVALID_USER, "Cannot ban, invalid user"),
"kick.unauthorized" -> "Only an administrator can kick users" (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( val ITALIAN_LOCALE: Map[LocalizableString.Value, String] = Map(
"ban.unauthorized" -> "Solo un amministratore può bannare gli utenti", (LocalizableString.BAN_UNAUTHORIZED, "Solo un amministratore può bannare gli utenti"),
"ban.successful" -> "Utente bannato con successo", (LocalizableString.BAN_SUCCESSFUL, "Utente bannato con successo"),
"ban.failed" -> "Non sono riuscito a bannare, qualcosa è andato storto", (LocalizableString.BAN_FAILED, "Non sono riuscito a bannare, qualcosa è andato storto"),
"ban.failed_ban_admin" -> "Non puoi bannare un admin", (LocalizableString.BAN_FAILED_INVALID_USER, "Impossibile bannare, utente non valido"),
"kick.unauthorized" -> "Solo un amministratore può kickare gli utenti" (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) { 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 "en_EN" => Locale.ENGLISH_LOCALE(name)
case "it_IT" => Locale.ITALIAN_LOCALE(name) case "it_IT" => Locale.ITALIAN_LOCALE(name)
case _ => Locale.ENGLISH_LOCALE(name) case _ => Locale.ENGLISH_LOCALE(name)

View file

@ -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
}
}