Repository: Rapptz/discord.py
File: discord/automod.py (Python)

Code before:
        custom_message: Optional[str] = ...,
    ) -> None:
        ...

    def __init__(
        self,
        *,
        type: Optional[AutoModRuleActionType] = None,
        channel_id: Optional[int] = None,
        duration: Optional[datetime.timedelta] = None,
        custom_message: Optional[str] = None,
    ) -> None:
        if sum(v is None for v in (channel_id, duration, custom_message)) < 2:
            raise ValueError('Only one of channel_id, duration, or custom_message can be passed.')

        self.custom_message: Optional[str] = custom_message

        if self.type is AutoModRuleActionType.send_alert_message and channel_id is None:
            raise ValueError('channel_id must be set if AutoModRuleActionType is send_alert_message')
        else:
            self.channel_id: Optional[int] = channel_id

        if self.type is AutoModRuleActionType.timeout and duration is None:
            raise ValueError('duration must be set if AutoModRuleActionType is timeout')
        else:
            self.duration: Optional[datetime.timedelta] = duration

        self.type: AutoModRuleActionType
        if type is not None:
            self.type = type
        elif channel_id is not None:
            self.type = AutoModRuleActionType.send_alert_message
        elif duration is not None:
            self.type = AutoModRuleActionType.timeout
        else:
            self.type = AutoModRuleActionType.block_message

    def __repr__(self) -> str:
        return f'<AutoModRuleAction type={self.type.value} channel={self.channel_id} duration={self.duration}>'

    @classmethod
    def from_data(cls, data: AutoModerationActionPayload) -> Self:
        if data['type'] == AutoModRuleActionType.timeout.value:
            duration_seconds = data['metadata']['duration_seconds']
            return cls(duration=datetime.timedelta(seconds=duration_seconds))
        elif data['type'] == AutoModRuleActionType.send_alert_message.value:
            channel_id = int(data['metadata']['channel_id'])
            return cls(channel_id=channel_id)
        elif data['type'] == AutoModRuleActionType.block_message.value:
            custom_message = data.get('metadata', {}).get('custom_message')
            return cls(type=AutoModRuleActionType.block_message, custom_message=custom_message)

Review comment (suggestion):
from my initial testing of this PR, having these two `if self.type ... else ...` blocks here raises AttributeError since `self.type` is not initialized yet as it is declared/initialized below it.
```py
AttributeError: 'AutoModRuleAction' object has no attribute 'type'
```

Code after:

    def __init__(
        self,
        *,
        type: Optional[AutoModRuleActionType] = None,
        channel_id: Optional[int] = None,
        duration: Optional[datetime.timedelta] = None,
        custom_message: Optional[str] = None,
    ) -> None:
        if sum(v is None for v in (channel_id, duration, custom_message)) < 2:
            raise ValueError('Only one of channel_id, duration, or custom_message can be passed.')

        self.type: AutoModRuleActionType
        if type is not None:
            self.type = type
        elif channel_id is not None:
            self.type = AutoModRuleActionType.send_alert_message
        elif duration is not None:
            self.type = AutoModRuleActionType.timeout
        else:
            self.type = AutoModRuleActionType.block_message

        if self.type is AutoModRuleActionType.send_alert_message:
            if channel_id is None:
                raise ValueError('channel_id cannot be None if type is send_alert_message')
            self.channel_id: Optional[int] = channel_id

        if self.type is AutoModRuleActionType.timeout:
            if duration is None:
                raise ValueError('duration cannot be None set if type is timeout')
            self.duration: Optional[datetime.timedelta] = duration

        self.custom_message: Optional[str] = custom_message

    def __repr__(self) -> str:
        return f'<AutoModRuleAction type={self.type.value} channel={self.channel_id} duration={self.duration}>'

    @classmethod
    def from_data(cls, data: AutoModerationActionPayload) -> Self:
        if data['type'] == AutoModRuleActionType.timeout.value:
            duration_seconds = data['metadata']['duration_seconds']
            return cls(duration=datetime.timedelta(seconds=duration_seconds))
        elif data['type'] == AutoModRuleActionType.send_alert_message.value:
            channel_id = int(data['metadata']['channel_id'])
            return cls(channel_id=channel_id)
        elif data['type'] == AutoModRuleActionType.block_message.value:
            custom_message = data.get('metadata', {}).get('custom_message')
            return cls(type=AutoModRuleActionType.block_message, custom_message=custom_message)

        return cls(type=AutoModRuleActionType.block_member_interactions)