Browse Source

Fix member_profile triggers not having attributes filled

pull/9619/head
z03h 1 year ago
committed by GitHub
parent
commit
933460c3d4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      discord/audit_logs.py
  2. 19
      discord/automod.py

39
discord/audit_logs.py

@ -33,7 +33,7 @@ from .invite import Invite
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
@ -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]:

19
discord/automod.py

@ -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`]
@ -260,8 +260,11 @@ class AutoModTrigger:
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
@ -273,7 +276,7 @@ class AutoModTrigger:
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 []
@ -296,7 +299,7 @@ class AutoModTrigger:
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'),
@ -317,7 +320,7 @@ class AutoModTrigger:
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,
@ -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__ = (

Loading…
Cancel
Save