Skip to content

Commit

Permalink
Slightly redo purge to maybe work a bit better
Browse files Browse the repository at this point in the history
also remove deprecation things
  • Loading branch information
sschr15 committed Sep 19, 2023
1 parent a0e9660 commit 10b826b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/quiltmc/community/_Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ suspend fun Guild.getFilterLogChannel(): GuildMessageChannel? {
suspend fun EmbedBuilder.userField(user: UserBehavior, role: String? = null, inline: Boolean = false) {
field {
name = role ?: "User"
value = "${user.mention} (`${user.id}` / `${user.asUser().tag}`)"
value = "${user.mention} (`${user.id}` / `${user.asUser().identifier}`)"

this.inline = inline
}
Expand All @@ -284,7 +284,7 @@ fun EmbedBuilder.channelField(channel: MessageChannelBehavior, title: String, in
}
}

suspend inline fun UserBehavior.softMention() = "@${asUser().tag}"
suspend inline fun UserBehavior.softMention() = asUser().identifier

suspend inline fun Snowflake.asGuild(): Guild? = getKoin().get<Kord>().getGuildOrNull(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import dev.kord.core.behavior.RoleBehavior
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.interaction.AutoCompleteInteraction
import org.quiltmc.community.modes.quilt.extensions.TEXT_CHANNEL_TYPES
import org.quiltmc.community.modes.quilt.extensions.THREAD_CHANNEL_TYPES
import org.quiltmc.community.modes.quilt.extensions.converters.mentionable

// This is just to clean up ModerationExtension.kt a bit more.
Expand Down Expand Up @@ -53,6 +55,13 @@ class PurgeArguments : Arguments(), ChannelTargetArguments {
override val channel by optionalChannel {
name = "channel"
description = "The channel to purge messages from (current if omitted)"

validate {
failIf(
value != null && value!!.data.type !in (TEXT_CHANNEL_TYPES + THREAD_CHANNEL_TYPES),
"You must select a text channel"
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.soywiz.korio.async.toChannel
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.optional
import dev.kord.core.behavior.ban
import dev.kord.core.behavior.channel.asChannelOf
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.behavior.edit
import dev.kord.core.behavior.interaction.modal
Expand Down Expand Up @@ -100,18 +101,26 @@ class ModerationExtension(
check { hasBaseModeratorRole() }

action {
val channel = arguments.channel ?: channel.asChannel()
val channel = (arguments.channel ?: channel).asChannelOf<GuildMessageChannel>()
val checkUsers = arguments.user != null
// checked in verifier
val targetUser = arguments.user as Member?
if (channel !is GuildMessageChannel) {
val deletedMessageList = mutableListOf<Snowflake>()

val mostRecentMessage = channel.lastMessage?.fetchMessage()
if (mostRecentMessage == null) {
respond {
content = "This command can only target a text channel, and ${channel.mention} is not one."
content = "Channel is empty."
}
return@action
}
val deletedMessageList = mutableListOf<Snowflake>()
val messages = channel.getMessagesBefore(channel.lastMessage!!.id).toChannel()

// the most recent message is missed in the loop below, so we add it here
if (!checkUsers || mostRecentMessage.author == targetUser) {
deletedMessageList.add(mostRecentMessage.id)
}

val messages = channel.getMessagesBefore(channel.lastMessageId!!).toChannel()
for (message in messages) {
if (message.data.flags.value?.contains(MessageFlag.Ephemeral) == true) {
continue // skip ephemeral messages
Expand Down

0 comments on commit 10b826b

Please sign in to comment.