Make some small functions

This commit is contained in:
Norbi Peti 2021-03-02 01:40:58 +01:00
parent fce6b91b97
commit d416eef144
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 31 additions and 45 deletions

View file

@ -12,7 +12,6 @@ import discord4j.core.event.EventDispatcher
import discord4j.core.event.domain.PresenceUpdateEvent
import discord4j.core.event.domain.message.MessageCreateEvent
import discord4j.core.event.domain.role.{RoleCreateEvent, RoleDeleteEvent, RoleUpdateEvent}
import reactor.core.publisher.Mono
import reactor.core.scala.publisher.SMono
object CommonListeners {
@ -29,9 +28,9 @@ object CommonListeners {
*/
def register(dispatcher: EventDispatcher) = {
dispatcher.on(classOf[MessageCreateEvent]).flatMap((event: MessageCreateEvent) => {
def foo(event: MessageCreateEvent) = {
def foo(event: MessageCreateEvent): SMono[Boolean] = {
timings.printElapsed("Message received")
val `def` = Mono.empty
val `def` = SMono.empty
if (DiscordPlugin.SafeMode) return `def`
val author = event.getMessage.getAuthor
if (!author.isPresent || author.get.isBot) return `def`
@ -42,30 +41,18 @@ object CommonListeners {
(shouldRun: Boolean) => { //Only 'channelcon' is allowed in other channels
def foo(shouldRun: Boolean): SMono[Boolean] = { //Only continue if this doesn't handle the event
if (!shouldRun) return SMono.just(true) //The condition is only for the first command execution, not mcchat
timings.printElapsed("Run command 1")
CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = true) //#bot is handled here
}
foo(shouldRun)
}).filterWhen((ch: Any) => {
def foo() = {
timings.printElapsed("mcchat")
val mcchat = Component.getComponents.get(classOf[MinecraftChatModule])
if (mcchat != null && mcchat.isEnabled) { //ComponentManager.isEnabled() searches the component again
return mcchat.asInstanceOf[MinecraftChatModule].getListener.handleDiscord(event) //Also runs Discord commands in chat channels
}
SMono.just(true) //Wasn't handled, continue
}).filterWhen(_ => {
timings.printElapsed("mcchat")
val mcchat = Component.getComponents.get(classOf[MinecraftChatModule])
if (mcchat != null && mcchat.isEnabled) { //ComponentManager.isEnabled() searches the component again
return mcchat.asInstanceOf[MinecraftChatModule].getListener.handleDiscord(event) //Also runs Discord commands in chat channels
}
foo()
}).filterWhen((ch: Any) => {
def foo(ch: Any) = {
timings.printElapsed("Run command 2")
CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = false)
}
foo(ch)
})
SMono.just(true) //Wasn't handled, continue
}).filterWhen(_ => CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = false))
}
foo(event)

View file

@ -3,9 +3,8 @@ package buttondevteam.discordplugin.mcchat
import buttondevteam.core.ComponentManager
import buttondevteam.core.component.channel.Channel
import buttondevteam.discordplugin._
import buttondevteam.discordplugin.listeners.{CommandListener, CommonListeners}
import buttondevteam.discordplugin.listeners.CommandListener
import buttondevteam.discordplugin.playerfaker.{VanillaCommandListener, VanillaCommandListener14, VanillaCommandListener15}
import buttondevteam.discordplugin.util.Timings
import buttondevteam.lib._
import buttondevteam.lib.chat.{ChatMessage, TBMCChatAPI}
import buttondevteam.lib.player.TBMCPlayer
@ -246,32 +245,16 @@ class MCChatListener(val module: MinecraftChatModule) extends Listener {
// Discord
def handleDiscord(ev: MessageCreateEvent): SMono[Boolean] = {
val timings: Timings = CommonListeners.timings
timings.printElapsed("Chat event")
val author = Option(ev.getMessage.getAuthor.orElse(null))
val hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage.getChannelId)
val prefix = DiscordPlugin.getPrefix
SMono(ev.getMessage.getChannel).filter(channel => {
def hasPrivateChat = channel.isInstanceOf[PrivateChannel] &&
author.exists((u: User) => MCChatPrivate.isMinecraftChatEnabled(u.getId.asString))
def hasPublicChat = ev.getMessage.getChannelId.asLong == module.chatChannel.get.asLong
timings.printElapsed("Filter 1")
val chatEnabled = hasPublicChat || hasPrivateChat || hasCustomChat
chatEnabled
}).filter(channel => {
timings.printElapsed("Filter 2")
!(channel.isInstanceOf[PrivateChannel] //Only in private chat
&& ev.getMessage.getContent.length < "/mcchat<>".length
&& ev.getMessage.getContent.replace(prefix + "", "").equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat
//Allow disabling the chat if needed
}).filterWhen(_ =>
CommandListener.runCommand(ev.getMessage, DiscordPlugin.plugin.commandChannel.get, mentionedonly = true)) //Allow running commands in chat channels
SMono(ev.getMessage.getChannel)
.filter(channel => isChatEnabled(channel, author, hasCustomChat))
.filter(channel => !isRunningMCChatCommand(channel, ev.getMessage.getContent, prefix))
.filterWhen(_ => CommandListener.runCommand(ev.getMessage, DiscordPlugin.plugin.commandChannel.get, mentionedonly = true)) //Allow running commands in chat channels
.filter(channel => {
MCChatUtils.resetLastMessage(channel)
recevents.add(ev)
timings.printElapsed("Message event added")
if (rectask != null) {
return true
}
@ -287,11 +270,27 @@ class MCChatListener(val module: MinecraftChatModule) extends Listener {
}).map(_ => false).defaultIfEmpty(true)
}
private def isChatEnabled(channel: MessageChannel, author: Option[User], hasCustomChat: Boolean) = {
def hasPrivateChat = channel.isInstanceOf[PrivateChannel] &&
author.exists((u: User) => MCChatPrivate.isMinecraftChatEnabled(u.getId.asString))
def hasPublicChat = channel.getId.asLong == module.chatChannel.get.asLong
hasPublicChat || hasPrivateChat || hasCustomChat
}
private def isRunningMCChatCommand(channel: MessageChannel, content: String, prefix: Char) = {
(channel.isInstanceOf[PrivateChannel] //Only in private chat
&& content.length < "/mcchat<>".length
&& content.replace(prefix + "", "").equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat
//Allow disabling the chat if needed
}
private def processDiscordToMC(): Unit = {
var event: MessageCreateEvent = null
try event = recevents.take
catch {
case e1: InterruptedException =>
case _: InterruptedException =>
rectask.cancel()
return
}