AutoMod

This section documents everything related to Discord’s AutoMod features.

Discord Models

AutoModRule

class disnake.AutoModRule[source]

Represents an auto moderation rule.

New in version 2.6.

id

The rule ID.

Type:

int

name

The rule name.

Type:

str

enabled

Whether this rule is enabled.

Type:

bool

guild

The guild of the rule.

Type:

Guild

creator_id

The rule creator’s ID. See also creator.

Type:

int

event_type

The event type this rule is applied to.

Type:

AutoModEventType

trigger_type

The type of trigger that determines whether this rule’s actions should run for a specific event.

Type:

AutoModTriggerType

trigger_metadata

Additional metadata associated with this rule’s trigger_type.

Type:

AutoModTriggerMetadata

exempt_role_ids

The role IDs that are exempt from this rule.

Type:

FrozenSet[int]

exempt_channel_ids

The channel IDs that are exempt from this rule.

Type:

FrozenSet[int]

property created_at[source]

Returns the rule’s creation time in UTC.

New in version 2.10.

Type:

datetime.datetime

property actions[source]

List[Union[AutoModBlockMessageAction, AutoModSendAlertAction, AutoModTimeoutAction, AutoModAction]]: The list of actions that will execute if a matching event triggered this rule.

property creator[source]

The guild member that created this rule. May be None if the member cannot be found. See also creator_id.

Type:

Optional[Member]

property exempt_roles[source]

The list of roles that are exempt from this rule.

Type:

List[Role]

property exempt_channels[source]

The list of channels that are exempt from this rule.

Type:

List[abc.GuildChannel]

await edit(*, name=..., event_type=..., trigger_metadata=..., actions=..., enabled=..., exempt_roles=..., exempt_channels=..., reason=None)[source]

This function is a coroutine.

Edits the auto moderation rule.

You must have Permissions.manage_guild permission to do this.

All fields are optional.

Changed in version 2.9: Now raises a TypeError if given actions have an invalid type.

Examples

Edit name and enable rule:

await rule.edit(name="cool new rule", enabled=True)

Add an action:

await rule.edit(
    actions=rule.actions + [AutoModTimeoutAction(3600)],
)

Add a keyword to a keyword filter rule:

meta = rule.trigger_metadata
await rule.edit(
    trigger_metadata=meta.with_changes(
        keyword_filter=meta.keyword_filter + ["stuff"],
    ),
)
Parameters:
  • name (str) – The rule’s new name.

  • event_type (AutoModEventType) – The rule’s new event type.

  • trigger_metadata (AutoModTriggerMetadata) – The rule’s new associated trigger metadata.

  • actions (Sequence[Union[AutoModBlockMessageAction, AutoModSendAlertAction, AutoModTimeoutAction, AutoModAction]]) – The rule’s new actions. If provided, must contain at least one action.

  • enabled (bool) – Whether to enable the rule.

  • exempt_roles (Optional[Iterable[abc.Snowflake]]) – The rule’s new exempt roles, up to 20. If [] or None is passed then all role exemptions are removed.

  • exempt_channels (Optional[Iterable[abc.Snowflake]]) – The rule’s new exempt channels, up to 50. Can also include categories, in which case all channels inside that category will be exempt. If [] or None is passed then all channel exemptions are removed.

  • reason (Optional[str]) – The reason for editing the rule. Shows up on the audit log.

Raises:
  • ValueError – When editing the list of actions, at least one action must be provided.

  • TypeError – The specified actions are of an invalid type.

  • Forbidden – You do not have proper permissions to edit the rule.

  • NotFound – The rule does not exist.

  • HTTPException – Editing the rule failed.

Returns:

The newly updated auto moderation rule.

Return type:

AutoModRule

await delete(*, reason=None)[source]

This function is a coroutine.

Deletes the auto moderation rule.

You must have Permissions.manage_guild permission to do this.

Parameters:

reason (Optional[str]) – The reason for deleting this rule. Shows up on the audit log.

Raises:
  • Forbidden – You do not have proper permissions to delete the rule.

  • NotFound – The rule does not exist.

  • HTTPException – Deleting the rule failed.

AutoModActionExecution

class disnake.AutoModActionExecution[source]

Represents the data for an on_automod_action_execution() event.

New in version 2.6.

action

The action that was executed.

Type:

Union[AutoModBlockMessageAction, AutoModSendAlertAction, AutoModTimeoutAction, AutoModAction]

guild

The guild this action was executed in.

Type:

Guild

rule_id

The ID of the rule that matched.

Type:

int

rule_trigger_type

The trigger type of the rule that matched.

Type:

AutoModTriggerType

user_id

The ID of the user that triggered this action. See also user.

Type:

int

channel_id

The channel or thread ID in which the event occurred, if any. See also channel.

Type:

Optional[int]

message_id

The ID of the message that matched. None if the message was blocked, or if the content was not part of a message. See also message.

Type:

Optional[int]

alert_message_id

The ID of the alert message sent as a result of this action, if any. See also alert_message.

Type:

Optional[int]

content

The content that matched.

Requires Intents.message_content to be enabled, otherwise this field will be empty.

Type:

str

matched_keyword

The keyword or regex that matched.

Type:

Optional[str]

matched_content

The substring of content that matched the rule/keyword.

Requires Intents.message_content to be enabled, otherwise this field will be empty.

Type:

Optional[str]

property user[source]

The guild member that triggered this action. May be None if the member cannot be found. See also user_id.

Type:

Optional[Member]

property channel[source]

Optional[Union[abc.GuildChannel, Thread]]: The channel or thread in which the event occurred, if any.

property message[source]

The message that matched, if any. Not available if the message was blocked, if the content was not part of a message, or if the message was not found in the message cache.

Type:

Optional[Message]

property alert_message[source]

The alert message sent as a result of this action, if any. Only available if action.type is send_alert_message and the message was found in the message cache.

Type:

Optional[Message]

Data Classes

AutoModKeywordPresets

class disnake.AutoModKeywordPresets(**kwargs)[source]

Wraps up the pre-defined auto moderation keyword lists, provided by Discord.

x == y

Checks if two AutoModKeywordPresets instances are equal.

x != y

Checks if two AutoModKeywordPresets instances are not equal.

x <= y

Checks if an AutoModKeywordPresets instance is a subset of another AutoModKeywordPresets instance.

x >= y

Checks if an AutoModKeywordPresets instance is a superset of another AutoModKeywordPresets instance.

x < y

Checks if an AutoModKeywordPresets instance is a strict subset of another AutoModKeywordPresets instance.

x > y

Checks if an AutoModKeywordPresets instance is a strict superset of another AutoModKeywordPresets instance.

x | y, x |= y

Returns a new AutoModKeywordPresets instance with all enabled flags from both x and y. (Using |= will update in place).

x & y, x &= y

Returns a new AutoModKeywordPresets instance with only flags enabled on both x and y. (Using &= will update in place).

x ^ y, x ^= y

Returns a new AutoModKeywordPresets instance with only flags enabled on one of x or y, but not both. (Using ^= will update in place).

~x

Returns a new AutoModKeywordPresets instance with all flags from x inverted.

hash(x)

Return the flag’s hash.

iter(x)

Returns an iterator of (name, value) pairs. This allows it to be, for example, constructed as a dict or a list of pairs. Note that aliases are not shown.

Additionally supported are a few operations on class attributes.

AutoModKeywordPresets.y | AutoModKeywordPresets.z, AutoModKeywordPresets(y=True) | AutoModKeywordPresets.z

Returns a AutoModKeywordPresets instance with all provided flags enabled.

~AutoModKeywordPresets.y

Returns a AutoModKeywordPresets instance with all flags except y inverted from their default value.

New in version 2.6.

values[source]

The raw values. You should query flags via the properties rather than using these raw values.

Type:

int

classmethod all()[source]

A factory method that creates a AutoModKeywordPresets with everything enabled.

classmethod none()[source]

A factory method that creates a AutoModKeywordPresets with everything disabled.

profanity

Returns True if the profanity preset is enabled (contains words that may be considered swearing or cursing).

Type:

bool

sexual_content

Returns True if the sexual content preset is enabled (contains sexually explicit words).

Type:

bool

slurs

Returns True if the slurs preset is enabled (contains insults or words that may be considered hate speech).

Type:

bool

AutoModTriggerMetadata

class disnake.AutoModTriggerMetadata(*, keyword_filter=None, regex_patterns=None, presets=None, allow_list=None, mention_total_limit=None, mention_raid_protection_enabled=None)[source]

Metadata for an auto moderation trigger.

Based on the trigger type, different fields can be used with various limits:

Trigger Type

keyword_filter

regex_patterns

presets

allow_list

mention_total_limit

mention_raid_protection_enabled

keyword

✅ (x1000)

✅ (x10)

✅ (x100)

spam

keyword_preset

✅ (x1000)

mention_spam

New in version 2.6.

keyword_filter

The list of keywords to check for, up to 1000 keywords. Used with AutoModTriggerType.keyword.

See api docs for details about how keyword matching works. Each keyword must be 60 characters or less.

Type:

Optional[Sequence[str]]

regex_patterns

The list of regular expressions to check for. Used with AutoModTriggerType.keyword.

A maximum of 10 regexes can be added, each with up to 260 characters.

Note

Only Rust flavored regex is currently supported, which can be tested in online editors such as Rustexp.

New in version 2.7.

Type:

Optional[Sequence[str]]

presets

The keyword presets. Used with AutoModTriggerType.keyword_preset.

Type:

Optional[AutoModKeywordPresets]

allow_list

The keywords that should be exempt from a preset. Used with AutoModTriggerType.keyword (up to 100 exemptions) and AutoModTriggerType.keyword_preset (up to 1000 exemptions).

Each keyword must be 60 characters or less.

Type:

Optional[Sequence[str]]

mention_total_limit

The maximum number of mentions (members + roles) allowed, between 1 and 50. Used with AutoModTriggerType.mention_spam.

Type:

Optional[int]

mention_raid_protection_enabled

Whether to automatically detect mention raids. Used with AutoModTriggerType.mention_spam.

Defaults to False.

New in version 2.9.

Type:

Optional[bool]

with_changes(*, keyword_filter=..., regex_patterns=..., presets=..., allow_list=..., mention_total_limit=..., mention_raid_protection_enabled=...)[source]

Returns a new instance with the given changes applied. All other fields will be kept intact.

Returns:

The new metadata instance.

Return type:

AutoModTriggerMetadata

AutoModAction

Attributes
class disnake.AutoModAction[source]

A base class for auto moderation actions.

This class is not meant to be instantiated by the user. The user-constructible subclasses are:

Actions received from the API may be of this type (and not one of the subtypes above) if the action type is not implemented yet.

New in version 2.6.

type

The action type.

Type:

AutoModActionType

AutoModBlockMessageAction

Attributes
class disnake.AutoModBlockMessageAction(custom_message=None)[source]

Represents an auto moderation action that blocks content from being sent.

New in version 2.6.

Parameters:

custom_message (Optional[str]) –

The custom message to show to the user when the rule is triggered. Maximum length is 150 characters.

New in version 2.9.

type

The action type. Always set to block_message.

Type:

AutoModActionType

property custom_message[source]

The custom message to show to the user when the rule is triggered.

New in version 2.9.

Type:

Optional[str]

AutoModSendAlertAction

Attributes
class disnake.AutoModSendAlertAction(channel)[source]

Represents an auto moderation action that sends an alert to a channel.

New in version 2.6.

Parameters:

channel (abc.Snowflake) – The channel to send an alert in when the rule is triggered.

type

The action type. Always set to send_alert_message.

Type:

AutoModActionType

property channel_id[source]

The channel ID to send an alert in when the rule is triggered.

Type:

int

AutoModTimeoutAction

Attributes
class disnake.AutoModTimeoutAction(duration)[source]

Represents an auto moderation action that times out the user.

New in version 2.6.

Parameters:

duration (Union[int, datetime.timedelta]) – The duration (seconds or timedelta) for which to timeout the user when the rule is triggered.

type

The action type. Always set to timeout.

Type:

AutoModActionType

property duration[source]

The duration (in seconds) for which to timeout the user when the rule is triggered.

Type:

int

Enumerations

AutoModActionType

class disnake.AutoModActionType[source]

Represents the type of action an auto moderation rule will take upon execution.

New in version 2.6.

block_message

The rule will prevent matching messages from being posted.

send_alert_message

The rule will send an alert to a specified channel.

timeout

The rule will timeout the user that sent the message.

Note

This action type is only available for rules with trigger type keyword or mention_spam, and moderate_members permissions are required to use it.

AutoModEventType

class disnake.AutoModEventType[source]

Represents the type of event/context an auto moderation rule will be checked in.

New in version 2.6.

message_send

The rule will apply when a member sends or edits a message in the guild.

AutoModTriggerType

class disnake.AutoModTriggerType[source]

Represents the type of content that can trigger an auto moderation rule.

New in version 2.6.

Changed in version 2.9: Removed obsolete harmful_link type.

keyword

The rule will filter messages based on a custom keyword list.

This trigger type requires additional metadata.

spam

The rule will filter messages suspected of being spam.

keyword_preset

The rule will filter messages based on predefined lists containing commonly flagged words.

This trigger type requires additional metadata.

mention_spam

The rule will filter messages based on the number of member/role mentions they contain.

This trigger type requires additional metadata.

Events