|
@@ -1,41 +1,38 @@
|
|
|
package org.congressodeiradicali.karlmarx
|
|
|
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
|
|
|
-import com.bot4s.telegram.models.Message
|
|
|
|
|
|
-import scala.concurrent.{ExecutionContext, Future, blocking}
|
|
|
+import scala.concurrent.{ExecutionContext, Future}
|
|
|
+import scala.util.{Failure, Success}
|
|
|
|
|
|
class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits {
|
|
|
- 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}
|
|
|
- }
|
|
|
|
|
|
override var handlers: Map[CommandFilterMagnet, Handler] = Map {
|
|
|
stringToCommandFilter("ban") -> { msg =>
|
|
|
- Future {
|
|
|
- val text = msg.replyToMessage.fold {
|
|
|
- LocalizableString.BAN_FAILED_REPLY
|
|
|
- } { reply =>
|
|
|
- reply.from.fold {
|
|
|
- LocalizableString.BAN_FAILED_INVALID_USER
|
|
|
- } { u =>
|
|
|
- val user = new BotUser(u, msg.chat, bot)
|
|
|
- blocking {
|
|
|
- user.syncAfterInit { botuser =>
|
|
|
- if (botuser.isAdmin || botuser.isCreator) {
|
|
|
- LocalizableString.BAN_FAILED_BAN_ADMIN
|
|
|
- } else {
|
|
|
- user.ban()
|
|
|
- LocalizableString.BAN_SUCCESSFUL
|
|
|
- }
|
|
|
- } { _ => LocalizableString.BAN_FAILED }
|
|
|
+
|
|
|
+ implicit val ec: ExecutionContext = ExecutionContext.global
|
|
|
+
|
|
|
+ val futureLocalizedString = msg.replyToMessage.fold {
|
|
|
+ Future { LocalizableString.BAN_FAILED_REPLY }
|
|
|
+ } { 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()
|
|
|
+ LocalizableString.BAN_SUCCESSFUL
|
|
|
}
|
|
|
+ case Failure(_) => LocalizableString.BAN_FAILED
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- Some(localizer.getString(text))
|
|
|
- }(ExecutionContext.global)
|
|
|
+ futureLocalizedString.map { s =>
|
|
|
+ Some(localizer.getString(s))
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|