From 45fc1017f911d357e23d855ab2977eec9116e7dc Mon Sep 17 00:00:00 2001 From: NoComment Date: Wed, 15 Jan 2025 11:29:39 +0000 Subject: [PATCH 1/2] Conduct permission checks before running the phishing detection actions to avoid errors --- .../func/phishing/PhishingExtension.kt | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt b/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt index e0fba5200a..74f4d3ea1a 100644 --- a/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt +++ b/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt @@ -11,6 +11,8 @@ package dev.kordex.modules.func.phishing import dev.kord.common.asJavaLocale +import dev.kord.common.entity.Permission +import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.ban import dev.kord.core.behavior.channel.createMessage import dev.kord.core.entity.Message @@ -31,6 +33,7 @@ import dev.kordex.core.extensions.event import dev.kordex.core.i18n.generated.CoreTranslations.Extensions.Help.Paginator.Title.arguments import dev.kordex.core.utils.dm import dev.kordex.core.utils.getJumpUrl +import dev.kordex.core.utils.hasPermissions import dev.kordex.core.utils.kordExUserAgent import dev.kordex.modules.func.phishing.i18n.generated.PhishingTranslations import dev.kordex.modules.func.phishing.i18n.generated.PhishingTranslations.Actions.logMessage @@ -234,20 +237,37 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension() .withLocale(message.getGuildOrNull()?.preferredLocale?.asJavaLocale()) .translate() + var actionSuccess = true + val selfAsMember = kord.getSelf().asMemberOrNull(message.getGuild().id) + when (settings.detectionAction) { DetectionAction.Ban -> { - message.getAuthorAsMemberOrNull()!!.ban { - reason = translatedLogMessage - } + if (selfAsMember?.hasPermissions(Permission.BanMembers, Permission.ManageMessages) == true) { + message.getAuthorAsMemberOrNull()!!.ban { + reason = translatedLogMessage + } - message.delete(translatedLogMessage) + message.delete(translatedLogMessage) + } else { + actionSuccess = false + } } - DetectionAction.Delete -> message.delete(translatedLogMessage) + DetectionAction.Delete -> { + if (selfAsMember?.hasPermissions(Permission.ManageMessages) == true) { + message.delete(translatedLogMessage) + } else { + actionSuccess = false + } + } DetectionAction.Kick -> { - message.getAuthorAsMemberOrNull()!!.kick(translatedLogMessage) - message.delete(translatedLogMessage) + if (selfAsMember?.hasPermissions(Permission.KickMembers, Permission.BanMembers) == true) { + message.getAuthorAsMemberOrNull()!!.kick(translatedLogMessage) + message.delete(translatedLogMessage) + } else { + actionSuccess = false + } } DetectionAction.LogOnly -> { @@ -255,13 +275,21 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension() } } - logDeletion(message, locale, matches) + logDeletion(message, locale, matches, actionSuccess) } } - private suspend fun logDeletion(message: Message, locale: Locale, matches: Set) { + private suspend fun logDeletion(message: Message, locale: Locale, matches: Set, actionSuccess: Boolean) { val guild = message.getGuild() + if (!actionSuccess) { + logger.warn { + "Unable to run ${ + settings.detectionAction.toString().split(".")[1] + } action on ${guild.name} (${guild.id.value}) due to missing permissions" + } + } + val channel = message .getGuild() .channels @@ -325,6 +353,18 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension() name = PhishingTranslations.Fields.totalMatches.translateLocale(locale) value = matches.size.toString() } + + if (!actionSuccess) { + field { + inline = true + + name = PhishingTranslations.Fields.ActionFailed.name.translateLocale(locale) + value = PhishingTranslations.Fields.ActionFailed.value.translateLocale( + locale, + settings.detectionAction.toString().split(".")[1] + ) + } + } } } } From c3cc66694056959eff981a87486cdcd6a48f2732 Mon Sep 17 00:00:00 2001 From: NoComment Date: Wed, 15 Jan 2025 11:54:39 +0000 Subject: [PATCH 2/2] Make detekt happy --- .../kordex/modules/func/phishing/PhishingExtension.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt b/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt index 74f4d3ea1a..10911b49c5 100644 --- a/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt +++ b/modules/functionality/func-phishing/src/main/kotlin/dev/kordex/modules/func/phishing/PhishingExtension.kt @@ -12,7 +12,6 @@ package dev.kordex.modules.func.phishing import dev.kord.common.asJavaLocale import dev.kord.common.entity.Permission -import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.ban import dev.kord.core.behavior.channel.createMessage import dev.kord.core.entity.Message @@ -241,7 +240,7 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension() val selfAsMember = kord.getSelf().asMemberOrNull(message.getGuild().id) when (settings.detectionAction) { - DetectionAction.Ban -> { + DetectionAction.Ban -> if (selfAsMember?.hasPermissions(Permission.BanMembers, Permission.ManageMessages) == true) { message.getAuthorAsMemberOrNull()!!.ban { reason = translatedLogMessage @@ -251,24 +250,21 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension() } else { actionSuccess = false } - } - DetectionAction.Delete -> { + DetectionAction.Delete -> if (selfAsMember?.hasPermissions(Permission.ManageMessages) == true) { message.delete(translatedLogMessage) } else { actionSuccess = false } - } - DetectionAction.Kick -> { + DetectionAction.Kick -> if (selfAsMember?.hasPermissions(Permission.KickMembers, Permission.BanMembers) == true) { message.getAuthorAsMemberOrNull()!!.kick(translatedLogMessage) message.delete(translatedLogMessage) } else { actionSuccess = false } - } DetectionAction.LogOnly -> { // Do nothing, we always log