Skip to content

Commit

Permalink
Add a /remove command for user restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 committed Jan 4, 2024
1 parent 0e9f6b1 commit b8f5861
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@

package org.quiltmc.community.modes.quilt.extensions.moderation

import com.kotlindiscord.kord.extensions.DiscordRelayedException
import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.enumChoice
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice
import com.kotlindiscord.kord.extensions.commands.converters.Validator
import com.kotlindiscord.kord.extensions.commands.converters.impl.*
import com.kotlindiscord.kord.extensions.utils.getKoin
import com.kotlindiscord.kord.extensions.utils.suggestDoubleMap
import com.kotlindiscord.kord.extensions.utils.suggestIntMap
import com.kotlindiscord.kord.extensions.utils.suggestLongMap
import dev.kord.core.Kord
import dev.kord.core.behavior.RoleBehavior
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.User
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.interaction.AutoCompleteInteraction
import org.quiltmc.community.modes.quilt.extensions.TEXT_CHANNEL_TYPES
Expand All @@ -29,12 +33,31 @@ interface ChannelTargetArguments {
val channel: Channel?
}

open class RequiredUser(mentionableDesc: String, validator: Validator<KordEntity> = null) : Arguments() {
interface UserTargetArguments {
suspend fun user(): User
}

open class RequiredUser(
mentionableDesc: String,
validator: Validator<User> = null
) : Arguments(), UserTargetArguments {
val user by user {
name = "user"
description = mentionableDesc
validate(validator)
}

override suspend fun user() = user
}

class UserBySnowflakeArguments : Arguments(), UserTargetArguments {
val snowflake by snowflake {
name = "user"
description = "The user to perform the action on, as their user ID"
}

override suspend fun user() = getKoin().get<Kord>().getUser(snowflake)
?: throw DiscordRelayedException("No user found with the ID $snowflake")
}

class PurgeArguments : Arguments(), ChannelTargetArguments {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.kotlindiscord.kord.extensions.annotations.DoNotChain
import com.kotlindiscord.kord.extensions.checks.isNotBot
import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext
import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand
import com.kotlindiscord.kord.extensions.commands.application.slash.group
import com.kotlindiscord.kord.extensions.components.forms.ModalForm
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
Expand Down Expand Up @@ -745,6 +746,108 @@ class ModerationExtension(
}
}

ephemeralSlashCommand {
name = "remove"
description = "Remove a user's timeout or ban."

check { hasBaseModeratorRole() }

suspend fun remove(guild: Guild, user: User, isBan: Boolean) {
val restriction = userRestrictions.get(user.id)
if (restriction != null) {
restriction.returningBanTime = null
restriction.isBanned = false
restriction.save()
}

if (isBan) {
guild.unban(user.id)
} else {
user.asMemberOrNull(guild.id)?.removeTimeout()
}
}

group("timeout") {
ephemeralSubCommand(::UserBySnowflakeArguments) {
name = "by-id"
description = "Remove a user's timeout by their ID."

check { hasBaseModeratorRole() }

action {
val user = arguments.user()
val guild = getGuild()?.asGuild()
?: throw DiscordRelayedException("Guild not found. Are you running this in a DM?")

remove(guild, user, false)

respond {
content = "Removed timeout for ${user.mention}."
}
}
}

ephemeralSubCommand({ RequiredUser("The user to remove the timeout from") }) {
name = "by-mention"
description = "Remove a user's timeout by mentioning them."

check { hasBaseModeratorRole() }

action {
val user = arguments.user()
val guild = getGuild()?.asGuild()
?: throw DiscordRelayedException("Guild not found. Are you running this in a DM?")

remove(guild, user, false)

respond {
content = "Removed timeout for ${user.mention}."
}
}
}
}

group("ban") {
ephemeralSubCommand(::UserBySnowflakeArguments) {
name = "by-id"
description = "Remove a user's ban by their ID."

check { hasBaseModeratorRole() }

action {
val user = arguments.user()
val guild = getGuild()?.asGuild()
?: throw DiscordRelayedException("Guild not found. Are you running this in a DM?")

remove(guild, user, true)

respond {
content = "Removed ban for ${user.mention}."
}
}
}

ephemeralSubCommand({ RequiredUser("The user to remove the ban from") }) {
name = "by-mention"
description = "Remove a user's ban by mentioning them."

check { hasBaseModeratorRole() }

action {
val user = arguments.user()
val guild = getGuild()?.asGuild()
?: throw DiscordRelayedException("Guild not found. Are you running this in a DM?")

remove(guild, user, true)

respond {
content = "Removed ban for ${user.mention}."
}
}
}
}
}

event<ButtonInteractionCreateEvent> {
check {
failIfNot { event.interaction.componentId.startsWith("mod:ban-sharing:") }
Expand Down

0 comments on commit b8f5861

Please sign in to comment.