Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check permissions before running phishing actions #360

Open
wants to merge 3 commits into
base: root
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package dev.kordex.modules.func.phishing

import dev.kord.common.asJavaLocale
import dev.kord.common.entity.Permission
import dev.kord.core.behavior.ban
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.entity.Message
Expand All @@ -31,6 +32,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
Expand Down Expand Up @@ -234,34 +236,56 @@ 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
}
DetectionAction.Ban ->
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)
}
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
}
}

logDeletion(message, locale, matches)
logDeletion(message, locale, matches, actionSuccess)
}
}

private suspend fun logDeletion(message: Message, locale: Locale, matches: Set<String>) {
private suspend fun logDeletion(message: Message, locale: Locale, matches: Set<String>, 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
Expand Down Expand Up @@ -325,6 +349,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]
)
}
}
}
}
}
Expand Down
Loading