Skip to content

Commit

Permalink
Add a vc mute
Browse files Browse the repository at this point in the history
For when a timeout is too excessive but a user is disruptive in voice
  • Loading branch information
sschr15 committed Feb 3, 2024
1 parent 8b8d5d9 commit 1169525
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ data class ServerSettings(

val exemptUsers: MutableSet<Snowflake> = mutableSetOf(),
val exemptRoles: MutableSet<Snowflake> = mutableSetOf(),

var vcMuteRole: Snowflake? = null,
) : Entity<Snowflake> {
suspend fun save() {
val collection = getKoin().get<ServerSettingsCollection>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ data class UserRestrictions(

var previousTimeouts: MutableList<Instant> = mutableListOf(),

var isMuted: Boolean = false,
// Separate check to allow a timeout at the same time as a mute
var returningMuteTime: Instant? = null,

@Deprecated("No longer used, kept for migration purposes")
var lastProgressiveTimeoutLength: Int = 0,
) : Entity<Snowflake> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,23 @@ object AllMigrations {
)
}
}

suspend fun v28(db: CoroutineDatabase) {
with(db.getCollection<UserRestrictions>(UserRestrictionsCollection.name)) {
updateMany(
UserRestrictions::isMuted exists false,
setValue(UserRestrictions::isMuted, false)
)

updateMany(
UserRestrictions::returningMuteTime exists false,
setValue(UserRestrictions::returningMuteTime, null)
)
}

db.getCollection<ServerSettings>(ServerSettingsCollection.name).updateMany(
ServerSettings::vcMuteRole exists false,
setValue(ServerSettings::vcMuteRole, null)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.kotlindiscord.kord.extensions.commands.application.slash.converters.C
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.builders.ConverterBuilder
import com.kotlindiscord.kord.extensions.commands.converters.impl.*
import com.kotlindiscord.kord.extensions.utils.getKoin
import com.kotlindiscord.kord.extensions.utils.suggestDoubleMap
Expand Down Expand Up @@ -208,83 +209,31 @@ class BanArguments : RequiresReason("The user to ban") {
description = "The length of the ban in seconds, 0 for indefinite, or -1 to end (default indefinite)"
defaultValue = 0L

autoComplete {
val map = mapFrom(
"second" to 1,
"minute" to 60,
"hour" to 3600,
"day" to 86_400,
"week" to 604_800,
"month" to 2_629_746,
"year" to 31_557_600,
defaultMap = mapOf(
"1 minute" to 60,
"5 minutes" to 300,
"10 minutes" to 600,
"30 minutes" to 1_800,
"1 hour" to 3_600,
"2 hours" to 7_200,
"3 hours" to 10_800,
"6 hours" to 21_600,
"1 day" to 86_400,
"2 days" to 172_800,
"3 days" to 259_200,
"1 week" to 604_800,
"2 weeks" to 1_209_600,
"3 weeks" to 1_814_400,
"1 month" to 2_592_000,
"2 months" to 5_184_000,
"3 months" to 7_168_000,
"6 months" to 15_552_000,
"1 year" to 31_556_952,
"forever" to 0,
"unban" to -1
)
)

suggestLongMap(map)
}
restrictionLength(false, "Remove ban")
}

val daysToDelete by banDeleteDaySelector()
}

class VcMuteArguments : RequiresReason("The user to mute") {
@Suppress("MagicNumber")
val length by defaultingLong {
name = "length"
description = "The length of the mute (default 30 minutes)"
defaultValue = 1_800

restrictionLength(false, "Remove mute")
}
}

class TimeoutArguments : RequiresReason("The user to timeout") {
@Suppress("MagicNumber")
val length by defaultingLong {
name = "length"
description = "The length of the timeout (default 5 minutes)"
defaultValue = 300

autoComplete {
val map = mapFrom(
"second" to 1L,
"minute" to 60L,
"hour" to 3600L,
"day" to 86_400L,
"week" to 604_800L,
defaultMap = mapOf(
"1 minute" to 60L,
"5 minutes" to 300L,
"10 minutes" to 600L,
"30 minutes" to 1_800L,
"1 hour" to 3_600L,
"2 hours" to 7_200L,
"3 hours" to 10_800L,
"6 hours" to 21_600L,
"1 day" to 86_400L,
"2 days" to 172_800L,
"3 days" to 259_200L,
"1 week" to 604_800L,
"2 weeks" to 1_209_600L,
"3 weeks" to 1_814_400L,
"1 month" to 2_592_000L,
"Remove timeout" to -1L
)
)

suggestLongMap(map)
}
restrictionLength(true, "Remove timeout")

validate {
failIfNot("length must be between 0 and 28 days") {
Expand Down Expand Up @@ -319,42 +268,7 @@ class ActionArguments : Arguments() {
description = "The length of the action (default 1 month)"
defaultValue = 2_629_746

autoComplete {
val map = mapFrom(
"second" to 1,
"minute" to 60,
"hour" to 3600,
"day" to 86_400,
"week" to 604_800,
"month" to 2_629_746,
"year" to 31_557_600,
defaultMap = mapOf(
"1 minute" to 60,
"5 minutes" to 300,
"10 minutes" to 600,
"30 minutes" to 1_800,
"1 hour" to 3_600,
"2 hours" to 7_200,
"3 hours" to 10_800,
"6 hours" to 21_600,
"1 day" to 86_400,
"2 days" to 172_800,
"3 days" to 259_200,
"1 week" to 604_800,
"2 weeks" to 1_209_600,
"3 weeks" to 1_814_400,
"1 month" to 2_592_000,
"2 months" to 5_184_000,
"3 months" to 7_168_000,
"6 months" to 15_552_000,
"1 year" to 31_556_952,
"forever" to 0,
"remove" to -1
)
)

suggestLongMap(map)
}
restrictionLength(false, "Remove restriction")
}

val banDeleteDays by banDeleteDaySelector()
Expand Down Expand Up @@ -403,7 +317,7 @@ internal fun Arguments.banDeleteDaySelector() = defaultingDecimal {
}

internal fun AutoCompleteInteraction.mapFrom(
vararg conversions: Pair<String, Long>,
conversions: List<Pair<String, Long>>,
defaultMap: Map<String, Long> = mapOf(),
): Map<String, Long> {
val specifiedLength = focusedOption.value.substringBefore(' ').toLongOrNull()
Expand All @@ -418,3 +332,51 @@ internal fun AutoCompleteInteraction.mapFrom(
defaultMap
}
}

@Suppress("MagicNumber")
internal fun ConverterBuilder<Long>.restrictionLength(timeout: Boolean, removeName: String) {
val options = listOf(
"second" to 1,
"minute" to 60,
"hour" to 3600,
"day" to 86_400,
"week" to 604_800,
"month" to 2_629_746,
"year" to 31_557_600,
).filter { !timeout || it.second <= 2_592_000 }
.map { (s, i) -> s to i.toLong() }

val defaultMapValues = mapOf(
"1 minute" to 60,
"5 minutes" to 300,
"10 minutes" to 600,
"30 minutes" to 1_800,
"1 hour" to 3_600,
"2 hours" to 7_200,
"3 hours" to 10_800,
"6 hours" to 21_600,
"1 day" to 86_400,
"2 days" to 172_800,
"3 days" to 259_200,
"1 week" to 604_800,
"2 weeks" to 1_209_600,
"3 weeks" to 1_814_400,
"1 month" to 2_592_000,
"2 months" to 5_184_000,
"3 months" to 7_168_000,
"6 months" to 15_552_000,
"1 year" to 31_556_952,
"forever" to 0,
removeName to -1
).mapValues { it.value.toLong() }

val defaultMap = if (timeout) {
defaultMapValues.filterKeys { it != "forever" }.filterValues { it <= 2_592_000 }
} else {
defaultMapValues
}

autoComplete {
suggestLongMap(mapFrom(options, defaultMap))
}
}
Loading

0 comments on commit 1169525

Please sign in to comment.