Skip to content

Commit f0ee490

Browse files
authored
fix(automod): improve validation for invalid actions (#1030)
This improves validation in `Guild.create_automod_rule` and `AutoModRule.edit`, now raising a proper `TypeError` for invalid action types in the most common error cases. ref: https://canary.discord.com/channels/808030843078836254/1093894067135463475
1 parent 473bafb commit f0ee490

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

changelog/1030.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

disnake/automod.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ async def edit(
544544
545545
All fields are optional.
546546
547+
.. versionchanged:: 2.9
548+
Now raises a :exc:`TypeError` if given ``actions`` have an invalid type.
549+
547550
Examples
548551
--------
549552
Edit name and enable rule:
@@ -598,6 +601,8 @@ async def edit(
598601
------
599602
ValueError
600603
When editing the list of actions, at least one action must be provided.
604+
TypeError
605+
The specified ``actions`` are of an invalid type.
601606
Forbidden
602607
You do not have proper permissions to edit the rule.
603608
NotFound
@@ -619,8 +624,13 @@ async def edit(
619624
if trigger_metadata is not MISSING:
620625
payload["trigger_metadata"] = trigger_metadata.to_dict()
621626
if actions is not MISSING:
622-
if len(actions) == 0:
627+
if not actions:
623628
raise ValueError("At least one action must be provided.")
629+
for action in actions:
630+
if not isinstance(action, AutoModAction):
631+
raise TypeError(
632+
f"actions must be of type `AutoModAction` (or subtype), not {type(action)!r}"
633+
)
624634
payload["actions"] = [a.to_dict() for a in actions]
625635
if enabled is not MISSING:
626636
payload["enabled"] = enabled

disnake/guild.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from . import abc, utils
2727
from .app_commands import GuildApplicationCommandPermissions
2828
from .asset import Asset
29-
from .automod import AutoModRule
29+
from .automod import AutoModAction, AutoModRule
3030
from .bans import BanEntry
3131
from .channel import (
3232
CategoryChannel,
@@ -90,7 +90,7 @@
9090
from .abc import Snowflake, SnowflakeTime
9191
from .app_commands import APIApplicationCommand
9292
from .asset import AssetBytes
93-
from .automod import AutoModAction, AutoModTriggerMetadata
93+
from .automod import AutoModTriggerMetadata
9494
from .permissions import Permissions
9595
from .state import ConnectionState
9696
from .template import Template
@@ -4565,6 +4565,9 @@ async def create_automod_rule(
45654565
45664566
.. versionadded:: 2.6
45674567
4568+
.. versionchanged:: 2.9
4569+
Now raises a :exc:`TypeError` if given ``actions`` have an invalid type.
4570+
45684571
Parameters
45694572
----------
45704573
name: :class:`str`
@@ -4594,8 +4597,10 @@ async def create_automod_rule(
45944597
Raises
45954598
------
45964599
ValueError
4597-
The specified trigger type requires `trigger_metadata` to be set,
4600+
The specified trigger type requires ``trigger_metadata`` to be set,
45984601
or no actions have been provided.
4602+
TypeError
4603+
The specified ``actions`` are of an invalid type.
45994604
Forbidden
46004605
You do not have proper permissions to create auto moderation rules.
46014606
HTTPException
@@ -4614,8 +4619,13 @@ async def create_automod_rule(
46144619
):
46154620
raise ValueError("Specified trigger type requires `trigger_metadata` to not be empty")
46164621

4617-
if len(actions) == 0:
4622+
if not actions:
46184623
raise ValueError("At least one action must be provided.")
4624+
for action in actions:
4625+
if not isinstance(action, AutoModAction):
4626+
raise TypeError(
4627+
f"actions must be of type `AutoModAction` (or subtype), not {type(action)!r}"
4628+
)
46194629

46204630
data = await self._state.http.create_auto_moderation_rule(
46214631
self.id,

0 commit comments

Comments
 (0)