Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1030.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise :exc:`TypeError` in :meth:`Guild.create_automod_rule` and :meth:`AutoModRule.edit` when an action has an invalid type, instead of a rather cryptic error.
12 changes: 11 additions & 1 deletion disnake/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ async def edit(

All fields are optional.

.. versionchanged:: 2.9
Now raises a :exc:`TypeError` if given ``actions`` have an invalid type.

Examples
--------
Edit name and enable rule:
Expand Down Expand Up @@ -598,6 +601,8 @@ async def edit(
------
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
Expand All @@ -619,8 +624,13 @@ async def edit(
if trigger_metadata is not MISSING:
payload["trigger_metadata"] = trigger_metadata.to_dict()
if actions is not MISSING:
if len(actions) == 0:
if not actions:
raise ValueError("At least one action must be provided.")
for action in actions:
if not isinstance(action, AutoModAction):
raise TypeError(
f"actions must be of type `AutoModAction` (or subtype), not {type(action)!r}"
)
payload["actions"] = [a.to_dict() for a in actions]
if enabled is not MISSING:
payload["enabled"] = enabled
Expand Down
18 changes: 14 additions & 4 deletions disnake/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from . import abc, utils
from .app_commands import GuildApplicationCommandPermissions
from .asset import Asset
from .automod import AutoModRule
from .automod import AutoModAction, AutoModRule
from .bans import BanEntry
from .channel import (
CategoryChannel,
Expand Down Expand Up @@ -90,7 +90,7 @@
from .abc import Snowflake, SnowflakeTime
from .app_commands import APIApplicationCommand
from .asset import AssetBytes
from .automod import AutoModAction, AutoModTriggerMetadata
from .automod import AutoModTriggerMetadata
from .permissions import Permissions
from .state import ConnectionState
from .template import Template
Expand Down Expand Up @@ -4565,6 +4565,9 @@ async def create_automod_rule(

.. versionadded:: 2.6

.. versionchanged:: 2.9
Now raises a :exc:`TypeError` if given ``actions`` have an invalid type.

Parameters
----------
name: :class:`str`
Expand Down Expand Up @@ -4594,8 +4597,10 @@ async def create_automod_rule(
Raises
------
ValueError
The specified trigger type requires `trigger_metadata` to be set,
The specified trigger type requires ``trigger_metadata`` to be set,
or no actions have been provided.
TypeError
The specified ``actions`` are of an invalid type.
Forbidden
You do not have proper permissions to create auto moderation rules.
HTTPException
Expand All @@ -4614,8 +4619,13 @@ async def create_automod_rule(
):
raise ValueError("Specified trigger type requires `trigger_metadata` to not be empty")

if len(actions) == 0:
if not actions:
raise ValueError("At least one action must be provided.")
for action in actions:
if not isinstance(action, AutoModAction):
raise TypeError(
f"actions must be of type `AutoModAction` (or subtype), not {type(action)!r}"
)

data = await self._state.http.create_auto_moderation_rule(
self.id,
Expand Down