Skip to content

Commit

Permalink
bugfix attempt...
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim19375 committed May 16, 2021
1 parent 0490620 commit f52c078
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 20 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, MutableList<(Event) -> Unit>>()
private val executor = Executors.newSingleThreadExecutor()

private val events = mutableListOf<(Event) -> Future<Boolean>>()
private val singleEvents = mutableMapOf<Type, MutableList<(Event) -> Future<Boolean>>>()

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)
}
}
}

Expand All @@ -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<Boolean> = actionLabel@{ event ->
val future = CompletableFuture<Boolean>()
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")
}
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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 ->
Expand All @@ -160,26 +205,31 @@ 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")
}
}
}
}
return@actionLabel future
}
if (debug) {
println("added to variables")
Expand All @@ -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<KClass<out Event>>) {
Expand Down

0 comments on commit f52c078

Please sign in to comment.