This commit is contained in:
ekardnam 2019-07-22 09:07:39 +02:00
parent 16b3629362
commit 4eb4db55ef
7 changed files with 36 additions and 39 deletions

View file

@ -42,10 +42,8 @@ class Bot(val token: String) extends TelegramBot
val redis = new RedisAsyncClient(sys.env("REDIS_HOST"), 6379)
def getLogger() = logger
private def plugin(plugin: Plugin) = plugin.identifier -> plugin
var plugins: Map[String, Plugin] = Map(
plugin(new PluginManagerPlugin(this)),
plugin(new BanPlugin(this)),

View file

@ -24,7 +24,24 @@ import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future, blocking}
import scala.util.{Failure, Success, Try}
class BotUser(private val user: User, private val chat: Chat, private val bot: Bot) {
object BotUserRequest {
def requestBotUser(user: User, chat: Chat, bot: Bot) = Future[Try[BotUser]] {
blocking {
Await.ready(bot.request(GetChatMember(ChatId.fromChat(chat.id), user.id)), Duration.Inf).value.get match {
case Success(chatMember) => Success(new BotUser(user, chat, bot, chatMember))
case Failure(err) => Failure(err)
}
}
}(ExecutionContext.global)
}
class BotUser(
private val user: User,
private val chat: Chat,
private val bot: Bot,
private val chatMember: ChatMember) {
def ban() : Unit = {
// ban works by sending the kick user request
@ -37,32 +54,13 @@ class BotUser(private val user: User, private val chat: Chat, private val bot: B
bot.request(UnbanChatMember(ChatId.fromChat(chat.id), user.id))
}
var chatMember: Option[ChatMember] = None
def init() : Future[Try[BotUser]] = Future {
blocking {
Await.ready(bot.request(GetChatMember(ChatId.fromChat(chat.id), user.id)), Duration.Inf).value.get match {
case Success(cm) => {
chatMember = Some(cm)
Success(this)
}
case Failure(err) => {
/*
bot.getLogger().error(s"Could not retrieve information about user ${user.id}")
bot.getLogger().error(err.getLocalizedMessage)
*/
Failure(err)
}
}
}
}(ExecutionContext.global)
def isCreator: Boolean = chatMember.status == MemberStatus.Creator
def isAdmin: Boolean = chatMember.status == MemberStatus.Administrator
def isMember: Boolean = chatMember.status == MemberStatus.Member
def isKicked: Boolean = chatMember.status == MemberStatus.Kicked
def isCreator: Boolean = chatMember.exists {_.status == MemberStatus.Creator}
def isAdmin: Boolean = chatMember.exists {_.status == MemberStatus.Administrator}
def isMember: Boolean = chatMember.exists {_.status == MemberStatus.Member}
def isKicked: Boolean = chatMember.exists {_.status == MemberStatus.Kicked}
def canBanUsers : Boolean = chatMember.exists {_.canRestrictMembers.getOrElse(false)}
def canBanUsers : Boolean = chatMember.canRestrictMembers.getOrElse(false)
}

View file

@ -20,7 +20,7 @@ package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
import org.congressodeiradicali.karlmarx._
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
import scala.util.{Failure, Success}
class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
@ -33,15 +33,13 @@ class BanPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
override var commandHandlers: Map[CommandFilterMagnet, CommandHandler] = Map {
stringToCommandFilter("ban") -> { (msg, _) =>
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 }
} { user =>
new BotUser(user, msg.chat, bot).init().map {
BotUserRequest.requestBotUser(user, msg.chat, bot).map {
case Success(target) if target.isAdmin || target.isCreator => LocalizableString.BAN_FAILED_BAN_ADMIN
case Success(target) =>
target.ban()

View file

@ -19,8 +19,12 @@ package org.congressodeiradicali.karlmarx.coreplugins
import org.congressodeiradicali.karlmarx.Plugin
import scala.concurrent.ExecutionContext
trait CorePlugin extends Plugin {
protected implicit val ec: ExecutionContext = ExecutionContext.global
override val license = "GNU AGPL"
override val author = "Congresso dei Radicali"

View file

@ -20,7 +20,8 @@ package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
import org.congressodeiradicali.karlmarx.Bot
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
@ -30,7 +31,7 @@ class EchoPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
override val identifier: String = "echo"
override var commandHandlers: Map[CommandFilterMagnet, CommandHandler] = Map(
stringToCommandFilter("echo") -> { (_, argv) => Future { Some(argv.drop(1).mkString(" ")) }(ExecutionContext.global) }
stringToCommandFilter("echo") -> { (_, argv) => Future { Some(argv.drop(1).mkString(" ")) } }
)
override var messageHandlers: List[MessageHandler] = List()
}

View file

@ -21,15 +21,13 @@ import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits
import com.bot4s.telegram.models.Message
import org.congressodeiradicali.karlmarx.{Bot, LocalizableString}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
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
private implicit val ec: ExecutionContext = ExecutionContext.global
override val name = "Plugin Manager"
override val description = "Manages other plugins"

View file

@ -21,7 +21,7 @@ import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits
import com.bot4s.telegram.models.Message
import org.congressodeiradicali.karlmarx.{Bot, LocalizableString}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
override val name: String = "Set Plugin"
@ -35,7 +35,7 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
val response = argv.drop(2).mkString(" ") // Drop command + trigger
bot.redis.set("PLUGIN_SET_" + trigger, response).map { _ =>
Some(bot.localize(LocalizableString.DONE))
}(ExecutionContext.global)
}
}
)
override var messageHandlers: List[MessageHandler] = List(
@ -43,7 +43,7 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
msg.text.map { text =>
val firstWord = text.takeWhile(_ != ' ')
bot.redis.get("PLUGIN_SET_" + firstWord)
}.getOrElse(Future { None }(ExecutionContext.global))
}.getOrElse(Future { None })
}
)
}