Make some small functions
This commit is contained in:
parent
fce6b91b97
commit
d416eef144
2 changed files with 31 additions and 45 deletions
|
@ -12,7 +12,6 @@ import discord4j.core.event.EventDispatcher
|
||||||
import discord4j.core.event.domain.PresenceUpdateEvent
|
import discord4j.core.event.domain.PresenceUpdateEvent
|
||||||
import discord4j.core.event.domain.message.MessageCreateEvent
|
import discord4j.core.event.domain.message.MessageCreateEvent
|
||||||
import discord4j.core.event.domain.role.{RoleCreateEvent, RoleDeleteEvent, RoleUpdateEvent}
|
import discord4j.core.event.domain.role.{RoleCreateEvent, RoleDeleteEvent, RoleUpdateEvent}
|
||||||
import reactor.core.publisher.Mono
|
|
||||||
import reactor.core.scala.publisher.SMono
|
import reactor.core.scala.publisher.SMono
|
||||||
|
|
||||||
object CommonListeners {
|
object CommonListeners {
|
||||||
|
@ -29,9 +28,9 @@ object CommonListeners {
|
||||||
*/
|
*/
|
||||||
def register(dispatcher: EventDispatcher) = {
|
def register(dispatcher: EventDispatcher) = {
|
||||||
dispatcher.on(classOf[MessageCreateEvent]).flatMap((event: MessageCreateEvent) => {
|
dispatcher.on(classOf[MessageCreateEvent]).flatMap((event: MessageCreateEvent) => {
|
||||||
def foo(event: MessageCreateEvent) = {
|
def foo(event: MessageCreateEvent): SMono[Boolean] = {
|
||||||
timings.printElapsed("Message received")
|
timings.printElapsed("Message received")
|
||||||
val `def` = Mono.empty
|
val `def` = SMono.empty
|
||||||
if (DiscordPlugin.SafeMode) return `def`
|
if (DiscordPlugin.SafeMode) return `def`
|
||||||
val author = event.getMessage.getAuthor
|
val author = event.getMessage.getAuthor
|
||||||
if (!author.isPresent || author.get.isBot) return `def`
|
if (!author.isPresent || author.get.isBot) return `def`
|
||||||
|
@ -42,30 +41,18 @@ object CommonListeners {
|
||||||
(shouldRun: Boolean) => { //Only 'channelcon' is allowed in other channels
|
(shouldRun: Boolean) => { //Only 'channelcon' is allowed in other channels
|
||||||
def foo(shouldRun: Boolean): SMono[Boolean] = { //Only continue if this doesn't handle the event
|
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
|
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
|
CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = true) //#bot is handled here
|
||||||
}
|
}
|
||||||
|
|
||||||
foo(shouldRun)
|
foo(shouldRun)
|
||||||
}).filterWhen((ch: Any) => {
|
}).filterWhen(_ => {
|
||||||
def foo() = {
|
|
||||||
timings.printElapsed("mcchat")
|
timings.printElapsed("mcchat")
|
||||||
val mcchat = Component.getComponents.get(classOf[MinecraftChatModule])
|
val mcchat = Component.getComponents.get(classOf[MinecraftChatModule])
|
||||||
if (mcchat != null && mcchat.isEnabled) { //ComponentManager.isEnabled() searches the component again
|
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
|
return mcchat.asInstanceOf[MinecraftChatModule].getListener.handleDiscord(event) //Also runs Discord commands in chat channels
|
||||||
}
|
}
|
||||||
SMono.just(true) //Wasn't handled, continue
|
SMono.just(true) //Wasn't handled, continue
|
||||||
}
|
}).filterWhen(_ => CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = false))
|
||||||
|
|
||||||
foo()
|
|
||||||
}).filterWhen((ch: Any) => {
|
|
||||||
def foo(ch: Any) = {
|
|
||||||
timings.printElapsed("Run command 2")
|
|
||||||
CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = false)
|
|
||||||
}
|
|
||||||
|
|
||||||
foo(ch)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foo(event)
|
foo(event)
|
||||||
|
|
|
@ -3,9 +3,8 @@ package buttondevteam.discordplugin.mcchat
|
||||||
import buttondevteam.core.ComponentManager
|
import buttondevteam.core.ComponentManager
|
||||||
import buttondevteam.core.component.channel.Channel
|
import buttondevteam.core.component.channel.Channel
|
||||||
import buttondevteam.discordplugin._
|
import buttondevteam.discordplugin._
|
||||||
import buttondevteam.discordplugin.listeners.{CommandListener, CommonListeners}
|
import buttondevteam.discordplugin.listeners.CommandListener
|
||||||
import buttondevteam.discordplugin.playerfaker.{VanillaCommandListener, VanillaCommandListener14, VanillaCommandListener15}
|
import buttondevteam.discordplugin.playerfaker.{VanillaCommandListener, VanillaCommandListener14, VanillaCommandListener15}
|
||||||
import buttondevteam.discordplugin.util.Timings
|
|
||||||
import buttondevteam.lib._
|
import buttondevteam.lib._
|
||||||
import buttondevteam.lib.chat.{ChatMessage, TBMCChatAPI}
|
import buttondevteam.lib.chat.{ChatMessage, TBMCChatAPI}
|
||||||
import buttondevteam.lib.player.TBMCPlayer
|
import buttondevteam.lib.player.TBMCPlayer
|
||||||
|
@ -246,32 +245,16 @@ class MCChatListener(val module: MinecraftChatModule) extends Listener {
|
||||||
|
|
||||||
// Discord
|
// Discord
|
||||||
def handleDiscord(ev: MessageCreateEvent): SMono[Boolean] = {
|
def handleDiscord(ev: MessageCreateEvent): SMono[Boolean] = {
|
||||||
val timings: Timings = CommonListeners.timings
|
|
||||||
timings.printElapsed("Chat event")
|
|
||||||
val author = Option(ev.getMessage.getAuthor.orElse(null))
|
val author = Option(ev.getMessage.getAuthor.orElse(null))
|
||||||
val hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage.getChannelId)
|
val hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage.getChannelId)
|
||||||
val prefix = DiscordPlugin.getPrefix
|
val prefix = DiscordPlugin.getPrefix
|
||||||
SMono(ev.getMessage.getChannel).filter(channel => {
|
SMono(ev.getMessage.getChannel)
|
||||||
def hasPrivateChat = channel.isInstanceOf[PrivateChannel] &&
|
.filter(channel => isChatEnabled(channel, author, hasCustomChat))
|
||||||
author.exists((u: User) => MCChatPrivate.isMinecraftChatEnabled(u.getId.asString))
|
.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
|
||||||
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
|
|
||||||
.filter(channel => {
|
.filter(channel => {
|
||||||
MCChatUtils.resetLastMessage(channel)
|
MCChatUtils.resetLastMessage(channel)
|
||||||
recevents.add(ev)
|
recevents.add(ev)
|
||||||
timings.printElapsed("Message event added")
|
|
||||||
if (rectask != null) {
|
if (rectask != null) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -287,11 +270,27 @@ class MCChatListener(val module: MinecraftChatModule) extends Listener {
|
||||||
}).map(_ => false).defaultIfEmpty(true)
|
}).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 = {
|
private def processDiscordToMC(): Unit = {
|
||||||
var event: MessageCreateEvent = null
|
var event: MessageCreateEvent = null
|
||||||
try event = recevents.take
|
try event = recevents.take
|
||||||
catch {
|
catch {
|
||||||
case e1: InterruptedException =>
|
case _: InterruptedException =>
|
||||||
rectask.cancel()
|
rectask.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue