From f52c07887214ae277839ce5c8f7154d87e2db2ee Mon Sep 17 00:00:00 2001 From: dkim19375 Date: Sun, 16 May 2021 13:27:47 -0400 Subject: [PATCH] bugfix attempt... --- build.gradle | 7 +- .../managers/SpecialEventsManager.kt | 87 +++++++++++++++---- 2 files changed, 74 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 219af61..1572691 100644 --- a/build.gradle +++ b/build.gradle @@ -8,10 +8,13 @@ sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8 compileJava.options.encoding 'UTF-8' group 'me.dkim19375' -version '2.2.2' +version '2.2.3' //noinspection GrUnresolvedAccess -compileKotlin.kotlinOptions.jvmTarget = '1.8' +compileKotlin.kotlinOptions { + languageVersion = "1.5" + jvmTarget = '1.8' +} repositories { mavenCentral() diff --git a/src/main/java/me/dkim19375/dkim19375jdautils/managers/SpecialEventsManager.kt b/src/main/java/me/dkim19375/dkim19375jdautils/managers/SpecialEventsManager.kt index df03a41..0d86a6c 100644 --- a/src/main/java/me/dkim19375/dkim19375jdautils/managers/SpecialEventsManager.kt +++ b/src/main/java/me/dkim19375/dkim19375jdautils/managers/SpecialEventsManager.kt @@ -14,24 +14,38 @@ import net.dv8tion.jda.api.events.message.priv.react.PrivateMessageReactionAddEv import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent import net.dv8tion.jda.api.hooks.ListenerAdapter import net.dv8tion.jda.api.requests.RestAction +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Executors +import java.util.concurrent.Future import kotlin.reflect.KClass open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { - private val events = mutableListOf<(Event) -> Unit>() - private val singleEvents = mutableMapOf Unit>>() + private val executor = Executors.newSingleThreadExecutor() + + private val events = mutableListOf<(Event) -> Future>() + private val singleEvents = mutableMapOf Future>>() override fun onGuildMessageReactionAdd(event: GuildMessageReactionAddEvent) = onEvent(Type.REACTION_ADD, event) override fun onPrivateMessageReactionAdd(event: PrivateMessageReactionAddEvent) = onEvent(Type.REACTION_ADD, event) override fun onMessageReactionAdd(event: MessageReactionAddEvent) = onEvent(Type.REACTION_ADD, event) + @Synchronized private fun onEvent(@Suppress("SameParameterValue") type: Type, event: Event) { events.forEach { it(event) } for ((otherType, list) in singleEvents.toMap()) { if (otherType != type) { continue } - singleEvents.remove(otherType) - list.forEach { it(event) } + for (expression in list.toList()) { + executor.submit { + if (expression(event).get()) { + list.removeIf { it === expression } + } + } + } + if (singleEvents.getOrDefault(otherType, mutableListOf()).isEmpty()) { + singleEvents.remove(otherType) + } } } @@ -49,24 +63,40 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { reaction: MessageReaction.ReactionEmote? = null, debug: Boolean = false ) { - val actionVar: (Event) -> Unit = action@{ event -> + val actionVar: (Event) -> Future = actionLabel@{ event -> + val future = CompletableFuture() if (debug) { println("called ------------") } val jda = event.jda when (event) { - is GuildMessageReactionAddEvent -> if (eventType != EventType.GUILD) return@action - is MessageReactionAddEvent -> if (eventType != EventType.GENERIC) return@action - is PrivateMessageReactionAddEvent -> if (eventType != EventType.PRIVATE) return@action + is GuildMessageReactionAddEvent -> if (eventType != EventType.GUILD) { + future.complete(false) + return@actionLabel future + } + is MessageReactionAddEvent -> if (eventType != EventType.GENERIC) { + future.complete(false) + return@actionLabel future + } + is PrivateMessageReactionAddEvent -> if (eventType != EventType.PRIVATE) { + future.complete(false) + return@actionLabel future + } } if (debug) { println("passed EventType test") } - val messageId: Long = event.getMessageId() ?: return@action + val messageId: Long = event.getMessageId() ?: let { + future.complete(false) + return@actionLabel future + } if (debug) { println("passed messageId 1") } - val userId: Long = event.getUserId() ?: return@action + val userId: Long = event.getUserId() ?: let { + future.complete(false) + return@actionLabel future + } if (debug) { println("passed userId test") } @@ -78,7 +108,10 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { is GuildMessageReactionAddEvent -> event.reactionEmote is MessageReactionAddEvent -> event.reactionEmote is PrivateMessageReactionAddEvent -> event.reactionEmote - else -> return@action + else -> { + future.complete(false) + return@actionLabel future + } } if (debug) { println("passed emoji test") @@ -87,7 +120,10 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { is GuildMessageReactionAddEvent -> event.channel is MessageReactionAddEvent -> event.channel is PrivateMessageReactionAddEvent -> event.channel - else -> return@action + else -> { + future.complete(false) + return@actionLabel future + } } if (debug) { println("passed channel test") @@ -96,7 +132,8 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { if (debug) { println("stopped - requiredChannel: $requiredChannel, channel: ${channel.idLong}") } - return@action + future.complete(false) + return@actionLabel future } if (debug) { println("passed channel id test") @@ -106,7 +143,8 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { if (debug) { println("stopped - reaction name: ${reaction.name}, emoji name: ${emoji.name}") } - return@action + future.complete(false) + return@actionLabel future } } if (debug) { @@ -116,7 +154,10 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { is GuildMessageReactionAddEvent -> event.retrieveMessage() is MessageReactionAddEvent -> event.retrieveMessage() is PrivateMessageReactionAddEvent -> event.channel.retrieveMessageById(event.messageIdLong) - else -> return@action + else -> { + future.complete(false) + return@actionLabel future + } } if (debug) { println("passed message test") @@ -125,7 +166,8 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { if (debug) { println("stopped - requiredGuild: $requiredGuild, guild: ${guild.idLong}") } - return@action + future.complete(false) + return@actionLabel future } if (debug) { println("passed requiredGuild test") @@ -134,7 +176,8 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { if (debug) { println("stopped 2 - requiredMessage: $requiredMessage, messageId: $messageId") } - return@action + future.complete(false) + return@actionLabel future } if (debug) { println("passed requiredMessage test") @@ -146,12 +189,14 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { if (debug) { println("no whitelist") } + future.complete(false) return@userQ } when { emoji.isEmoji -> msg.removeReaction(emoji.emoji, user).queue() emoji.isEmote -> msg.removeReaction(emoji.emote, user).queue() } + future.complete(false) return@userQ } guild?.retrieveMemberById(userId)?.queue memberQueue@{ member -> @@ -160,19 +205,23 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { println("no permissions") } if (!removeIfNoPerms) { + future.complete(false) return@memberQueue } when { emoji.isEmoji -> msg.removeReaction(emoji.emoji, user).queue() emoji.isEmote -> msg.removeReaction(emoji.emote, user).queue() } + future.complete(false) return@memberQueue } if (debug) { println("called action - guild") } + future.complete(true) action(event, guild, emoji, channel, user, msg, member) } ?: let { + future.complete(true) action(event, guild, emoji, channel, user, msg, null) if (debug) { println("called action - null guild") @@ -180,6 +229,7 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { } } } + return@actionLabel future } if (debug) { println("added to variables") @@ -188,7 +238,8 @@ open class SpecialEventsManager(private val bot: BotBase) : ListenerAdapter() { events.add(actionVar) return } - singleEvents.getOrPut(Type.REACTION_ADD) { mutableListOf() }.add(actionVar) + singleEvents.getOrPut(Type.REACTION_ADD) + { mutableListOf() }.add(actionVar) } enum class Type(@API val classes: Set>) {