Improve plugin interface

This commit is contained in:
CapacitorSet 2019-07-07 11:39:07 +02:00
parent a5169746b0
commit c82e325d28
3 changed files with 19 additions and 14 deletions

View file

@ -5,23 +5,21 @@ import com.bot4s.telegram.models.Message
import scala.concurrent.{ExecutionContext, Future, blocking}
class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits {
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") { implicit msg =>
Future {
override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("ban") -> { msg =>
val text = msg.replyToMessage.fold {
LocalizableString.BAN_FAILED_REPLY
} { message =>
message.from.fold {
} { reply =>
reply.from.fold {
LocalizableString.BAN_FAILED_INVALID_USER
} { u =>
val user = new BotUser(u, message.chat, bot)
val user = new BotUser(u, msg.chat, bot)
blocking {
user.syncAfterInit { botuser =>
if (botuser.isAdmin || botuser.isCreator) {
@ -35,7 +33,7 @@ class BanPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImpli
}
}
Some(localizer.getString(text))
}(ExecutionContext.global)
Future {Some(localizer.getString(text))}(ExecutionContext.global)
}
}
}

View file

@ -0,0 +1,12 @@
package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
import com.bot4s.telegram.models.Message
import scala.concurrent.{ExecutionContext, Future, blocking}
class EchoPlugin(localizer: Localizer, bot: Bot) extends Plugin with CommandImplicits {
override var handlers: Map[CommandFilterMagnet, Handler] = Map {
stringToCommandFilter("echo") -> { msg => Future { msg.text }(ExecutionContext.global) }
}
}

View file

@ -10,9 +10,4 @@ trait Plugin {
type HandlerPair = (CommandFilterMagnet, Handler)
var handlers: Map[CommandFilterMagnet, Handler]
def onCommand(filter: CommandFilterMagnet)(action: Handler): Unit = {
val pair: HandlerPair = (filter, action)
handlers += pair
}
}