Skip to content

Commit

Permalink
Add mention raid protection to Auto Moderation (kordlib#830)
Browse files Browse the repository at this point in the history
The DSL looks like this:
val rule = guild.createMentionSpamAutoModerationRule("name") {
    mentionRaidProtectionEnabled = true
    blockMessage()
    enabled = true
}
println(rule.isMentionRaidProtectionEnabled)

see discord/discord-api-docs#5778 and
discord/discord-api-docs#6133
  • Loading branch information
lukellmann authored Jun 17, 2023
1 parent 4bca754 commit c47d45c
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 27 deletions.
12 changes: 7 additions & 5 deletions common/api/common.api
Original file line number Diff line number Diff line change
Expand Up @@ -2690,19 +2690,21 @@ public final class dev/kord/common/entity/DiscordAutoModerationRule$Companion {
public final class dev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata {
public static final field Companion Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata$Companion;
public fun <init> ()V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ldev/kord/common/entity/optional/Optional;
public final fun component2 ()Ldev/kord/common/entity/optional/Optional;
public final fun component3 ()Ldev/kord/common/entity/optional/Optional;
public final fun component4 ()Ldev/kord/common/entity/optional/Optional;
public final fun component5 ()Ldev/kord/common/entity/optional/OptionalInt;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;)Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;
public static synthetic fun copy$default (Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;ILjava/lang/Object;)Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;
public final fun component6 ()Ldev/kord/common/entity/optional/OptionalBoolean;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;)Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;
public static synthetic fun copy$default (Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;ILjava/lang/Object;)Ldev/kord/common/entity/DiscordAutoModerationRuleTriggerMetadata;
public fun equals (Ljava/lang/Object;)Z
public final fun getAllowList ()Ldev/kord/common/entity/optional/Optional;
public final fun getKeywordFilter ()Ldev/kord/common/entity/optional/Optional;
public final fun getMentionRaidProtectionEnabled ()Ldev/kord/common/entity/optional/OptionalBoolean;
public final fun getMentionTotalLimit ()Ldev/kord/common/entity/optional/OptionalInt;
public final fun getPresets ()Ldev/kord/common/entity/optional/Optional;
public final fun getRegexPatterns ()Ldev/kord/common/entity/optional/Optional;
Expand Down
3 changes: 3 additions & 0 deletions common/src/commonMain/kotlin/entity/AutoModeration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
package dev.kord.common.entity

import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.serialization.DurationInSeconds
Expand Down Expand Up @@ -100,6 +101,8 @@ public data class DiscordAutoModerationRuleTriggerMetadata(
val allowList: Optional<List<String>> = Optional.Missing(),
@SerialName("mention_total_limit")
val mentionTotalLimit: OptionalInt = OptionalInt.Missing,
@SerialName("mention_raid_protection_enabled")
val mentionRaidProtectionEnabled: OptionalBoolean = OptionalBoolean.Missing,
)

@Serializable
Expand Down
15 changes: 10 additions & 5 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ public final class dev/kord/core/behavior/GuildBehaviorKt {
public static final fun createKeywordPresetAutoModerationRule (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun createKeywordPresetAutoModerationRule$default (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun createMentionSpamAutoModerationRule (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;ILkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun createMentionSpamAutoModerationRule (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun createMentionSpamAutoModerationRule$default (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;ILkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun createMentionSpamAutoModerationRule$default (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Ldev/kord/common/entity/AutoModerationRuleEventType;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun createMessageCommand (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun createMessageCommand$default (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun createNewsChannel (Ldev/kord/core/behavior/GuildBehavior;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -2825,19 +2827,21 @@ public final class dev/kord/core/cache/data/AutoModerationRuleData$Companion {
public final class dev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData {
public static final field Companion Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData$Companion;
public fun <init> ()V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ldev/kord/common/entity/optional/Optional;
public final fun component2 ()Ldev/kord/common/entity/optional/Optional;
public final fun component3 ()Ldev/kord/common/entity/optional/Optional;
public final fun component4 ()Ldev/kord/common/entity/optional/Optional;
public final fun component5 ()Ldev/kord/common/entity/optional/OptionalInt;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;)Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;
public static synthetic fun copy$default (Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;ILjava/lang/Object;)Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;
public final fun component6 ()Ldev/kord/common/entity/optional/OptionalBoolean;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;)Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;
public static synthetic fun copy$default (Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;ILjava/lang/Object;)Ldev/kord/core/cache/data/AutoModerationRuleTriggerMetadataData;
public fun equals (Ljava/lang/Object;)Z
public final fun getAllowList ()Ldev/kord/common/entity/optional/Optional;
public final fun getKeywordFilter ()Ldev/kord/common/entity/optional/Optional;
public final fun getMentionRaidProtectionEnabled ()Ldev/kord/common/entity/optional/OptionalBoolean;
public final fun getMentionTotalLimit ()Ldev/kord/common/entity/optional/OptionalInt;
public final fun getPresets ()Ldev/kord/common/entity/optional/Optional;
public final fun getRegexPatterns ()Ldev/kord/common/entity/optional/Optional;
Expand Down Expand Up @@ -7750,6 +7754,7 @@ public final class dev/kord/core/entity/automoderation/MentionSpamAutoModeration
public final fun getMentionLimit ()I
public fun getTriggerType ()Ldev/kord/common/entity/AutoModerationRuleTriggerType$MentionSpam;
public synthetic fun getTriggerType ()Ldev/kord/common/entity/AutoModerationRuleTriggerType;
public final fun isMentionRaidProtectionEnabled ()Z
public fun toString ()Ljava/lang/String;
public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/behavior/automoderation/AutoModerationRuleBehavior;
public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/behavior/automoderation/MentionSpamAutoModerationRuleBehavior;
Expand Down
26 changes: 22 additions & 4 deletions core/src/commonMain/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1068,18 +1068,36 @@ public suspend inline fun GuildBehavior.createKeywordPresetAutoModerationRule(
*
* @param name the rule name.
* @param eventType the rule [event type][AutoModerationRuleEventType].
* @param mentionLimit total number of unique role and user mentions allowed per message (maximum of 50).
*
* @throws RestRequestException if something went wrong during the request.
*/
public suspend inline fun GuildBehavior.createMentionSpamAutoModerationRule(
name: String,
eventType: AutoModerationRuleEventType = MessageSend,
mentionLimit: Int,
builder: MentionSpamAutoModerationRuleCreateBuilder.() -> Unit,
): MentionSpamAutoModerationRule {
contract { callsInPlace(builder, EXACTLY_ONCE) }
val rule = kord.rest.autoModeration
.createMentionSpamAutoModerationRule(guildId = id, name, eventType, mentionLimit, builder)
val rule = kord.rest.autoModeration.createMentionSpamAutoModerationRule(guildId = id, name, eventType, builder)
return MentionSpamAutoModerationRule(AutoModerationRuleData.from(rule), kord, supplier)
}

@Deprecated(
"The 'mentionLimit' parameter is optional, only 'mentionLimit' OR 'mentionRaidProtectionEnabled' is required.",
ReplaceWith(
"this.createMentionSpamAutoModerationRule(name, eventType) { this@createMentionSpamAutoModerationRule" +
".mentionLimit = mentionLimit\nbuilder() }"
),
DeprecationLevel.WARNING,
)
public suspend inline fun GuildBehavior.createMentionSpamAutoModerationRule(
name: String,
eventType: AutoModerationRuleEventType = MessageSend,
mentionLimit: Int,
builder: MentionSpamAutoModerationRuleCreateBuilder.() -> Unit,
): MentionSpamAutoModerationRule {
contract { callsInPlace(builder, EXACTLY_ONCE) }
return createMentionSpamAutoModerationRule(name, eventType) {
this.mentionLimit = mentionLimit
builder()
}
}
7 changes: 3 additions & 4 deletions core/src/commonMain/kotlin/cache/data/AutoModeration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package dev.kord.core.cache.data
import dev.kord.cache.api.data.DataDescription
import dev.kord.cache.api.data.description
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.*
import dev.kord.common.serialization.DurationInSeconds
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -53,6 +50,7 @@ public data class AutoModerationRuleTriggerMetadataData(
val presets: Optional<List<AutoModerationRuleKeywordPresetType>> = Optional.Missing(),
val allowList: Optional<List<String>> = Optional.Missing(),
val mentionTotalLimit: OptionalInt = OptionalInt.Missing,
val mentionRaidProtectionEnabled: OptionalBoolean = OptionalBoolean.Missing,
) {
public companion object {
public fun from(metadata: DiscordAutoModerationRuleTriggerMetadata): AutoModerationRuleTriggerMetadataData =
Expand All @@ -63,6 +61,7 @@ public data class AutoModerationRuleTriggerMetadataData(
presets = presets,
allowList = allowList,
mentionTotalLimit = mentionTotalLimit,
mentionRaidProtectionEnabled = mentionRaidProtectionEnabled,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ public class MentionSpamAutoModerationRule(data: AutoModerationRuleData, kord: K
/** Total number of unique role and user mentions allowed per message. */
public val mentionLimit: Int get() = data.triggerMetadata.mentionTotalLimit.value!!

/** Whether to automatically detect mention raids. */
public val isMentionRaidProtectionEnabled: Boolean
get() = data.triggerMetadata.mentionRaidProtectionEnabled.orElse(false)

override suspend fun asAutoModerationRuleOrNull(): MentionSpamAutoModerationRule = this
override suspend fun asAutoModerationRule(): MentionSpamAutoModerationRule = this

Expand Down
Loading

0 comments on commit c47d45c

Please sign in to comment.