Added BotUser as a wrapper for the bot users

This commit is contained in:
ekardnam 2019-07-05 14:20:30 +02:00
parent 981a546d79
commit c7f841110f
3 changed files with 117 additions and 17 deletions

View file

@ -26,7 +26,7 @@ import com.bot4s.telegram.api.declarative.Commands
import com.bot4s.telegram.clients.ScalajHttpClient
import com.bot4s.telegram.future.{Polling, TelegramBot}
import com.bot4s.telegram.methods.GetChatMember
import com.bot4s.telegram.models.{ChatId, ChatType, MemberStatus, Message}
import com.bot4s.telegram.models.{ChatId, ChatType, MemberStatus, Message, User}
import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
import scala.concurrent.duration.Duration
@ -45,6 +45,7 @@ class Bot(val token: String) extends TelegramBot
override val client: RequestHandler[Future] = new ScalajHttpClient(token)
/**
* Ensures a message is sent from a user and not from the chat
* @param msg the message
@ -70,29 +71,50 @@ class Bot(val token: String) extends TelegramBot
if (fromActualUser(msg) && fromGroup(msg)) {
// it should be an actual user and a message from a group
// check the user member status
msg.from match {
case Some(user) => {
val result = Await.ready(request(GetChatMember(ChatId.fromChat(msg.source), user.id)), Duration.Inf).value.get
result match {
case Success(chatMember) => chatMember.status == MemberStatus.Administrator || chatMember.status == MemberStatus.Creator
case Failure(error) => false
}
}
case _ => false
}
// get is safe as fromActualUser is called first
val user = new BotUser(msg.from.get, msg.chat, this)
// if there is an error we cannot now whether the user is an admin or not
// but it's safe in any case to reply as if the user was not an admin
user.isAdmin.getOrElse(false) || user.isCreator.getOrElse(false)
} else {
false
}
onCommand("license") { implicit msg =>
reply(" karl-marx is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n karl-marx is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with karl-marx. If not, see <https://www.gnu.org/licenses/>.").void
}
whenOrElse(onCommand("ban"), fromAdmin) { implicit msg =>
whenOrElse(onCommand("ban"), fromAdmin) { implicit msg =>
msg.replyToMessage match {
case Some(message) => {
message.from match {
case Some(u) => {
val user = new BotUser(u, message.chat, this)
val isAdmin = user.isAdmin
if (isAdmin.isSuccess) {
if (!isAdmin.get) {
user.ban
reply(localizer.getString("ban.successful")).void
} else reply(localizer.getString("ban.failed_ban_admin")).void
} else reply(localizer.getString("ban.failed")).void
}
}
}
}
} /*else*/ { implicit msg =>
reply(localizer.getString("ban.unauthorized")).void
}
whenOrElse(onCommand("kick"), fromAdmin) { implicit msg =>
} /*else*/ { implicit msg =>
}
}

View file

@ -0,0 +1,74 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.methods.GetChatMember
import com.bot4s.telegram.models.{Chat, ChatId, ChatMember, MemberStatus, User}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}
class BotUser(private val user: User, private val chat: Chat, private val bot: Bot) {
def ban() : Unit = {
}
def kick() : Unit = {
}
// lazy attribute initialized on the first successful request
private var chatMember : Option[ChatMember] = None
def getChatMember() : Try[ChatMember] = chatMember match {
case None => {
Await.ready(bot.request(GetChatMember(ChatId.fromChat(chat.id), user.id)), Duration.Inf).value.get match {
case Success(cm) => {
chatMember = Some(cm)
Success(cm)
}
case Failure(err) => Failure(err)
}
}
case Some(cm) => Success(cm)
}
def isCreator() : Try[Boolean] = {
getChatMember.map(cm => cm.status == MemberStatus.Creator)
}
def isAdmin() : Try[Boolean] = {
getChatMember.map(cm => cm.status == MemberStatus.Administrator)
}
def isKicked() : Try[Boolean] = {
getChatMember.map(cm => cm.status == MemberStatus.Kicked)
}
def isMember() : Try[Boolean] = {
getChatMember.map(cm => cm.status == MemberStatus.Member)
}
def hasLeft() : Try[Boolean] = {
getChatMember.map(cm => cm.status == MemberStatus.Left)
}
}

View file

@ -17,17 +17,21 @@
package org.congressodeiradicali.karlmarx
package object Locale {
object Locale {
val ENGLISH_LOCALE: Map[String, String] = Map(
"ban.unauthorized" -> "Only an administrator can do this action",
"ban.unauthorized" -> "Only an administrator can ban users",
"ban.successful" -> "User was banned successfully",
"ban.failed" -> "Cannot ban this user"
"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 ITALIAN_LOCALE: Map[String, String] = Map(
"ban.unauthorized" -> "Solo un amministratore può compiere questa azione",
"ban.unauthorized" -> "Solo un amministratore può bannare gli utenti",
"ban.successful" -> "Utente bannato con successo",
"ban.failed" -> "Non sono riuscito a bannare questo utente"
"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"
)
}