Skip to content

Commit

Permalink
Fix member_profile triggers not having attributes filled
Browse files Browse the repository at this point in the history
  • Loading branch information
z03h authored Oct 20, 2023
1 parent 7d15992 commit 933460c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
39 changes: 15 additions & 24 deletions discord/audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .mixins import Hashable
from .object import Object
from .permissions import PermissionOverwrite, Permissions
from .automod import AutoModTrigger, AutoModRuleAction, AutoModPresets, AutoModRule
from .automod import AutoModTrigger, AutoModRuleAction, AutoModRule
from .role import Role
from .emoji import Emoji
from .partial_emoji import PartialEmoji
Expand Down Expand Up @@ -234,33 +234,24 @@ def _transform_automod_trigger_metadata(
entry: AuditLogEntry, data: AutoModerationTriggerMetadata
) -> Optional[AutoModTrigger]:

# Try to get trigger type from target.trigger or infer from keys in data
if isinstance(entry.target, AutoModRule):
# Trigger type cannot be changed, so type should be the same before and after updates.
# Avoids checking which keys are in data to guess trigger type
# or returning None if data is empty.
try:
return AutoModTrigger.from_data(type=entry.target.trigger.type.value, data=data)
except Exception:
pass

# Try to infer trigger type from available keys in data
if 'presets' in data:
return AutoModTrigger(
type=enums.AutoModRuleTriggerType.keyword_preset,
presets=AutoModPresets._from_value(data['presets']), # type: ignore
allow_list=data.get('allow_list'),
)
elif 'keyword_filter' in data:
return AutoModTrigger(
type=enums.AutoModRuleTriggerType.keyword,
keyword_filter=data['keyword_filter'], # type: ignore
allow_list=data.get('allow_list'),
regex_patterns=data.get('regex_patterns'),
)
elif 'mention_total_limit' in data:
return AutoModTrigger(type=enums.AutoModRuleTriggerType.mention_spam, mention_limit=data['mention_total_limit']) # type: ignore
_type = entry.target.trigger.type.value
elif not data:
_type = enums.AutoModRuleTriggerType.spam.value
elif 'presets' in data:
_type = enums.AutoModRuleTriggerType.keyword_preset.value
elif 'keyword_filter' in data or 'regex_patterns' in data:
_type = enums.AutoModRuleTriggerType.keyword.value
elif 'mention_total_limit' in data or 'mention_raid_protection_enabled' in data:
_type = enums.AutoModRuleTriggerType.mention_spam.value
else:
return AutoModTrigger(type=enums.AutoModRuleTriggerType.spam)
# some unknown type
_type = -1

return AutoModTrigger.from_data(type=_type, data=data)


def _transform_automod_actions(entry: AuditLogEntry, data: List[AutoModerationAction]) -> List[AutoModRuleAction]:
Expand Down
19 changes: 12 additions & 7 deletions discord/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ class AutoModTrigger:
type: :class:`AutoModRuleTriggerType`
The type of trigger.
keyword_filter: List[:class:`str`]
The list of strings that will trigger the keyword filter. Maximum of 1000.
Keywords can only be up to 60 characters in length.
The list of strings that will trigger the filter.
Maximum of 1000. Keywords can only be up to 60 characters in length.
This could be combined with :attr:`regex_patterns`.
regex_patterns: List[:class:`str`]
Expand Down Expand Up @@ -260,8 +260,11 @@ def __init__(
regex_patterns: Optional[List[str]] = None,
mention_raid_protection: Optional[bool] = None,
) -> None:
if type is None and sum(arg is not None for arg in (keyword_filter or regex_patterns, presets, mention_limit)) > 1:
raise ValueError('Please pass only one of keyword_filter, regex_patterns, presets, or mention_limit.')
unique_args = (keyword_filter or regex_patterns, presets, mention_limit or mention_raid_protection)
if type is None and sum(arg is not None for arg in unique_args) > 1:
raise ValueError(
'Please pass only one of keyword_filter/regex_patterns, presets, or mention_limit/mention_raid_protection.'
)

if type is not None:
self.type = type
Expand All @@ -273,7 +276,7 @@ def __init__(
self.type = AutoModRuleTriggerType.mention_spam
else:
raise ValueError(
'Please pass the trigger type explicitly if not using keyword_filter, presets, or mention_limit.'
'Please pass the trigger type explicitly if not using keyword_filter, regex_patterns, presets, mention_limit, or mention_raid_protection.'
)

self.keyword_filter: List[str] = keyword_filter if keyword_filter is not None else []
Expand All @@ -296,7 +299,7 @@ def from_data(cls, type: int, data: Optional[AutoModerationTriggerMetadataPayloa
type_ = try_enum(AutoModRuleTriggerType, type)
if data is None:
return cls(type=type_)
elif type_ is AutoModRuleTriggerType.keyword:
elif type_ in (AutoModRuleTriggerType.keyword, AutoModRuleTriggerType.member_profile):
return cls(
type=type_,
keyword_filter=data.get('keyword_filter'),
Expand All @@ -317,7 +320,7 @@ def from_data(cls, type: int, data: Optional[AutoModerationTriggerMetadataPayloa
return cls(type=type_)

def to_metadata_dict(self) -> Optional[Dict[str, Any]]:
if self.type is AutoModRuleTriggerType.keyword:
if self.type in (AutoModRuleTriggerType.keyword, AutoModRuleTriggerType.member_profile):
return {
'keyword_filter': self.keyword_filter,
'regex_patterns': self.regex_patterns,
Expand Down Expand Up @@ -355,6 +358,8 @@ class AutoModRule:
The IDs of the roles that are exempt from the rule.
exempt_channel_ids: Set[:class:`int`]
The IDs of the channels that are exempt from the rule.
event_type: :class:`AutoModRuleEventType`
The type of event that will trigger the the rule.
"""

__slots__ = (
Expand Down

0 comments on commit 933460c

Please sign in to comment.