From cafd8096fa7af175aa2a7a13c482119343c03803 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 31 Dec 2021 00:38:28 +0100 Subject: [PATCH] Fix even more returns and Discord->MC chat --- .../buttondevteam/discordplugin/DPUtils.scala | 18 ++---- .../discordplugin/DiscordPlugin.scala | 10 ++-- .../discordplugin/DiscordSender.scala | 4 +- .../discordplugin/DiscordSenderBase.scala | 11 +--- .../listeners/CommonListeners.scala | 60 +++++++++---------- .../discordplugin/mcchat/MCChatCommand.scala | 4 +- .../discordplugin/mcchat/MCListener.scala | 4 +- 7 files changed, 47 insertions(+), 64 deletions(-) diff --git a/src/main/scala/buttondevteam/discordplugin/DPUtils.scala b/src/main/scala/buttondevteam/discordplugin/DPUtils.scala index b024832..be512b0 100644 --- a/src/main/scala/buttondevteam/discordplugin/DPUtils.scala +++ b/src/main/scala/buttondevteam/discordplugin/DPUtils.scala @@ -53,17 +53,10 @@ object DPUtils { private def escape(message: String) = { //var ts = new TreeSet<>(); val ts = new util.TreeSet[Array[Int]](Comparator.comparingInt((a: Array[Int]) => a(0)): Comparator[Array[Int]]) //Compare the start, then check the end var matcher = URL_PATTERN.matcher(message) - while ( { - matcher.find - }) ts.add(Array[Int](matcher.start, matcher.end)) + while (matcher.find) ts.add(Array[Int](matcher.start, matcher.end)) matcher = FORMAT_PATTERN.matcher(message) - /*Function aFunctionalInterface = result -> - Optional.ofNullable(ts.floor(new int[]{result.start(), 0})).map(a -> a[1]).orElse(0) < result.start() - ? "\\\\" + result.group() : result.group(); - return matcher.replaceAll(aFunctionalInterface); //Find nearest URL match and if it's not reaching to the char then escape*/ val sb = new StringBuffer - while ( { - matcher.find - }) matcher.appendReplacement(sb, if (Option(ts.floor(Array[Int](matcher.start, 0))).map( //Find a URL start <= our start + val sb = new StringBuffer + while (matcher.find) matcher.appendReplacement(sb, if (Option(ts.floor(Array[Int](matcher.start, 0))).map( //Find a URL start <= our start (a: Array[Int]) => a(1)).getOrElse(-1) < matcher.start //Check if URL end < our start ) "\\\\" + matcher.group else matcher.group) matcher.appendTail(sb) @@ -71,13 +64,14 @@ object DPUtils { } def getLogger: Logger = { - if (DiscordPlugin.plugin == null || DiscordPlugin.plugin.getLogger == null) return Logger.getLogger("DiscordPlugin") - DiscordPlugin.plugin.getLogger + if (DiscordPlugin.plugin == null || DiscordPlugin.plugin.getLogger == null) Logger.getLogger("DiscordPlugin") + else DiscordPlugin.plugin.getLogger } def channelData(config: IHaveConfig, key: String): ReadOnlyConfigData[SMono[MessageChannel]] = config.getReadOnlyDataPrimDef(key, 0L, (id: Any) => getMessageChannel(key, Snowflake.of(id.asInstanceOf[Long])), (_: SMono[MessageChannel]) => 0L) //We can afford to search for the channel in the cache once (instead of using mainServer) + def roleData(config: IHaveConfig, key: String, defName: String): ReadOnlyConfigData[SMono[Role]] = roleData(config, key, defName, SMono.just(DiscordPlugin.mainServer)) diff --git a/src/main/scala/buttondevteam/discordplugin/DiscordPlugin.scala b/src/main/scala/buttondevteam/discordplugin/DiscordPlugin.scala index 921a59b..8e03848 100644 --- a/src/main/scala/buttondevteam/discordplugin/DiscordPlugin.scala +++ b/src/main/scala/buttondevteam/discordplugin/DiscordPlugin.scala @@ -42,8 +42,8 @@ import java.util.Optional private[discordplugin] var SafeMode = true def getPrefix: Char = { - if (plugin == null) return '/' - plugin.prefix.get + if (plugin == null) '/' + else plugin.prefix.get } private[discordplugin] var mainServer: Guild = null @@ -174,7 +174,7 @@ import java.util.Optional getLogger.severe("Main server not found! Invite the bot and do /discord restart") DiscordPlugin.dc.getApplicationInfo.subscribe((info: ApplicationInfo) => getLogger.severe("Click here: https://discordapp.com/oauth2/authorize?client_id=" + info.getId.asString + "&scope=bot&permissions=268509264")) saveConfig() //Put default there - return //We should have all guilds by now, no need to retry + return () //We should have all guilds by now, no need to retry } DiscordPlugin.mainServer = event.get(0).getGuild getLogger.warning("Main server set to first one: " + DiscordPlugin.mainServer.getName) @@ -186,7 +186,7 @@ import java.util.Optional //Won't disable, just prints the warning here if (MinecraftChatModule.state eq DPState.STOPPING_SERVER) { stopStarting() - return //Reusing that field to check if stopping while still initializing + return () //Reusing that field to check if stopping while still initializing } CommonListeners.register(DiscordPlugin.dc.getEventDispatcher) TBMCCoreAPI.RegisterEventsForExceptions(new MCListener, this) @@ -236,7 +236,7 @@ import java.util.Optional e.printStackTrace() } } - if (!ChromaBot.enabled) return //Failed to load + if (!ChromaBot.enabled) return () //Failed to load val timings = new Timings timings.printElapsed("Disable start") timings.printElapsed("Updating player list") diff --git a/src/main/scala/buttondevteam/discordplugin/DiscordSender.scala b/src/main/scala/buttondevteam/discordplugin/DiscordSender.scala index 0462990..a77979e 100644 --- a/src/main/scala/buttondevteam/discordplugin/DiscordSender.scala +++ b/src/main/scala/buttondevteam/discordplugin/DiscordSender.scala @@ -27,8 +27,8 @@ class DiscordSender(user: User, channel: MessageChannel, pname: String) extends override def isPermissionSet(perm: Permission): Boolean = this.perm.isPermissionSet(perm) override def hasPermission(name: String): Boolean = { - if (name.contains("essentials") && !(name == "essentials.list")) return false - perm.hasPermission(name) + if (name.contains("essentials") && !(name == "essentials.list")) false + else perm.hasPermission(name) } override def hasPermission(perm: Permission): Boolean = this.perm.hasPermission(perm) diff --git a/src/main/scala/buttondevteam/discordplugin/DiscordSenderBase.scala b/src/main/scala/buttondevteam/discordplugin/DiscordSenderBase.scala index c5625ad..9d35368 100644 --- a/src/main/scala/buttondevteam/discordplugin/DiscordSenderBase.scala +++ b/src/main/scala/buttondevteam/discordplugin/DiscordSenderBase.scala @@ -47,14 +47,9 @@ abstract class DiscordSenderBase protected(var user: User, var channel: MessageC this synchronized { msgtosend += "\n" + sendmsg if (sendtask == null) sendtask = Bukkit.getScheduler.runTaskLaterAsynchronously(DiscordPlugin.plugin, () => { - def foo(): Unit = { - channel.createMessage((if (user != null) user.getMention + "\n" - else "") + msgtosend.trim).subscribe() - sendtask = null - msgtosend = "" - } - - foo() + channel.createMessage((if (user != null) user.getMention + "\n" else "") + msgtosend.trim).subscribe() + sendtask = null + msgtosend = "" }, 4) // Waits a 0.2 second to gather all/most of the different messages } } catch { diff --git a/src/main/scala/buttondevteam/discordplugin/listeners/CommonListeners.scala b/src/main/scala/buttondevteam/discordplugin/listeners/CommonListeners.scala index 467ee8b..c72be51 100644 --- a/src/main/scala/buttondevteam/discordplugin/listeners/CommonListeners.scala +++ b/src/main/scala/buttondevteam/discordplugin/listeners/CommonListeners.scala @@ -7,11 +7,13 @@ import buttondevteam.discordplugin.util.Timings import buttondevteam.discordplugin.{DPUtils, DiscordPlugin} import buttondevteam.lib.TBMCCoreAPI import buttondevteam.lib.architecture.Component +import discord4j.core.`object`.entity.Message import discord4j.core.`object`.entity.channel.{MessageChannel, PrivateChannel} 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.Disposable import reactor.core.scala.publisher.{SFlux, SMono} object CommonListeners { @@ -26,44 +28,20 @@ object CommonListeners { - Minecraft chat (is enabled in the channel and message isn't [/]mcchat) - CommandListener (with the correct prefix in #bot, or in private) */ - def register(dispatcher: EventDispatcher) = { + def register(dispatcher: EventDispatcher): Unit = { dispatcher.on(classOf[MessageCreateEvent]).flatMap((event: MessageCreateEvent) => { - def foo(event: MessageCreateEvent): SMono[Boolean] = { - timings.printElapsed("Message received") - val `def` = SMono.empty - if (DiscordPlugin.SafeMode) return `def` - val author = event.getMessage.getAuthor - if (!author.isPresent || author.get.isBot) return `def` - if (FunModule.executeMemes(event.getMessage)) return `def` - val commandChannel = DiscordPlugin.plugin.commandChannel.get - SMono(event.getMessage.getChannel).map((mch: MessageChannel) => (commandChannel != null && mch.getId.asLong == commandChannel.asLong) //If mentioned, that's higher than chat - || mch.isInstanceOf[PrivateChannel] || event.getMessage.getContent.contains("channelcon")).flatMap( - (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 - CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = true) //#bot is handled here - } - - foo(shouldRun) - }).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 - } - SMono.just(true) //Wasn't handled, continue - }).filterWhen(_ => CommandListener.runCommand(event.getMessage, commandChannel, mentionedonly = false)) - } - - foo(event) - }).onErrorContinue((err: Throwable, _) => TBMCCoreAPI.SendException("An error occured while handling a message!", err, DiscordPlugin.plugin)).subscribe() + SMono.just(event.getMessage).filter(_ => !DiscordPlugin.SafeMode) + .filter(message => message.getAuthor.filter(!_.isBot).isPresent) + .filter(message => !FunModule.executeMemes(message)) + .flatMap(handleMessage(event)) + }).onErrorContinue((err, _) => TBMCCoreAPI.SendException("An error occured while handling a message!", err, DiscordPlugin.plugin)).subscribe() dispatcher.on(classOf[PresenceUpdateEvent]).subscribe((event: PresenceUpdateEvent) => { if (!DiscordPlugin.SafeMode) FunModule.handleFullHouse(event) }) - SFlux(dispatcher.on(classOf[RoleCreateEvent])).subscribe(GameRoleModule.handleRoleEvent _) - SFlux(dispatcher.on(classOf[RoleDeleteEvent])).subscribe(GameRoleModule.handleRoleEvent _) - SFlux(dispatcher.on(classOf[RoleUpdateEvent])).subscribe(GameRoleModule.handleRoleEvent _) + SFlux(dispatcher.on(classOf[RoleCreateEvent])).subscribe(GameRoleModule.handleRoleEvent) + SFlux(dispatcher.on(classOf[RoleDeleteEvent])).subscribe(GameRoleModule.handleRoleEvent) + SFlux(dispatcher.on(classOf[RoleUpdateEvent])).subscribe(GameRoleModule.handleRoleEvent) } var debug = false @@ -71,4 +49,20 @@ object CommonListeners { def debug(debug: String): Unit = if (CommonListeners.debug) { //Debug DPUtils.getLogger.info(debug) } + + private def handleMessage(event: MessageCreateEvent) = { + (message: Message) => { + val commandChannel = Option(DiscordPlugin.plugin.commandChannel.get) + SMono(message.getChannel).filter(mch => commandChannel.isDefined && mch.getId.asLong() == commandChannel.get.asLong() //If mentioned, that's higher than chat + || mch.isInstanceOf[PrivateChannel] || message.getContent.contains("channelcon")).flatMap(_ => { //Only 'channelcon' is allowed in other channels + //Only continue if this doesn't handle the event + CommandListener.runCommand(message, commandChannel.get, mentionedonly = true) //#bot is handled here + }).`then`(SMono.just(true)) //The condition is only for the first command execution, not mcchat + .filterWhen(_ => { + Option(Component.getComponents.get(classOf[MinecraftChatModule])).filter(_.isEnabled) + .map(_.asInstanceOf[MinecraftChatModule].getListener.handleDiscord(event)) //Also runs Discord commands in chat channels + .getOrElse(SMono.just(true)) //Wasn't handled, continue + }).filterWhen(_ => CommandListener.runCommand(event.getMessage, commandChannel.get, mentionedonly = false)) + } + } } \ No newline at end of file diff --git a/src/main/scala/buttondevteam/discordplugin/mcchat/MCChatCommand.scala b/src/main/scala/buttondevteam/discordplugin/mcchat/MCChatCommand.scala index 9b84215..74c27f0 100644 --- a/src/main/scala/buttondevteam/discordplugin/mcchat/MCChatCommand.scala +++ b/src/main/scala/buttondevteam/discordplugin/mcchat/MCChatCommand.scala @@ -14,14 +14,14 @@ import discord4j.core.`object`.entity.channel.PrivateChannel )) class MCChatCommand(private val module: MinecraftChatModule) extends ICommand2DC { @Command2.Subcommand override def `def`(sender: Command2DCSender): Boolean = { - if (!(module.allowPrivateChat.get)) { + if (!module.allowPrivateChat.get) { sender.sendMessage("using the private chat is not allowed on this Minecraft server.") return true } val message = sender.getMessage val channel = message.getChannel.block @SuppressWarnings(Array("OptionalGetWithoutIsPresent")) val author = message.getAuthor.get - if (!((channel.isInstanceOf[PrivateChannel]))) { + if (!channel.isInstanceOf[PrivateChannel]) { DPUtils.reply(message, channel, "this command can only be issued in a direct message with the bot.").subscribe() return true } diff --git a/src/main/scala/buttondevteam/discordplugin/mcchat/MCListener.scala b/src/main/scala/buttondevteam/discordplugin/mcchat/MCListener.scala index 6e40149..5782f0e 100644 --- a/src/main/scala/buttondevteam/discordplugin/mcchat/MCListener.scala +++ b/src/main/scala/buttondevteam/discordplugin/mcchat/MCListener.scala @@ -28,7 +28,7 @@ class MCListener(val module: MinecraftChatModule) extends Listener { } @EventHandler(priority = EventPriority.MONITOR) def onPlayerJoin(e: PlayerJoinEvent): Unit = { - if (e.getPlayer.isInstanceOf[DiscordConnectedPlayer]) return // Don't show the joined message for the fake player + if (e.getPlayer.isInstanceOf[DiscordConnectedPlayer]) return () // Don't show the joined message for the fake player Bukkit.getScheduler.runTaskAsynchronously(DiscordPlugin.plugin, () => { def foo(): Unit = { val p = e.getPlayer @@ -54,7 +54,7 @@ class MCListener(val module: MinecraftChatModule) extends Listener { MCChatUtils.forAllowedCustomAndAllMCChat(MCChatUtils.send(message), player, ChannelconBroadcast.JOINLEAVE, hookmsg = true).subscribe() @EventHandler(priority = EventPriority.MONITOR) def onPlayerLeave(e: PlayerQuitEvent): Unit = { - if (e.getPlayer.isInstanceOf[DiscordConnectedPlayer]) return // Only care about real users + if (e.getPlayer.isInstanceOf[DiscordConnectedPlayer]) return () // Only care about real users MCChatUtils.OnlineSenders.filterInPlace((_, userMap) => userMap.entrySet.stream.noneMatch(_.getValue.getUniqueId.equals(e.getPlayer.getUniqueId))) Bukkit.getScheduler.runTaskAsynchronously(DiscordPlugin.plugin, () => MCChatUtils.LoggedInPlayers.get(e.getPlayer.getUniqueId).foreach(MCChatUtils.callLoginEvents)) Bukkit.getScheduler.runTaskLaterAsynchronously(DiscordPlugin.plugin, () => ChromaBot.updatePlayerList(), 5)