|
| 1 | +from enum import IntEnum |
| 2 | +from typing import TYPE_CHECKING, List, Optional, TypeVar |
| 3 | + |
| 4 | +from .attrs_utils import DictSerializerMixin, convert_list, define, field |
| 5 | +from .channel import Channel |
| 6 | +from .misc import Snowflake |
| 7 | +from .user import User |
| 8 | +from .webhook import Webhook |
| 9 | + |
| 10 | +__all__ = ( |
| 11 | + "AuditLogEntry", |
| 12 | + "AuditLogEvents", |
| 13 | + "AuditLogs", |
| 14 | + "AuditLogChange", |
| 15 | + "OptionalAuditEntryInfo", |
| 16 | +) |
| 17 | + |
| 18 | +_T = TypeVar("_T") |
| 19 | + |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from .guild import Integration, ScheduledEvents |
| 23 | + from .gw import AutoModerationRule |
| 24 | + |
| 25 | + |
| 26 | +class AuditLogEvents(IntEnum): |
| 27 | + """ |
| 28 | + A class object representing the different types of AuditLogEvents. |
| 29 | +
|
| 30 | + :ivar int GUILD_UPDATE: 1 - Server settings were updated |
| 31 | + :ivar int CHANNEL_CREATE: 10 - Channel was created |
| 32 | + :ivar int CHANNEL_UPDATE: 11 - Channel settings were updated |
| 33 | + :ivar int CHANNEL_DELETE: 12 - Channel was deleted |
| 34 | + :ivar int CHANNEL_OVERWRITE_CREATE: 13 - Permission overwrite was added to a channel |
| 35 | + :ivar int CHANNEL_OVERWRITE_UPDATE: 14 - Permission overwrite was updated for a channel |
| 36 | + :ivar int CHANNEL_OVERWRITE_DELETE: 15 - Permission overwrite was deleted from a channel |
| 37 | + :ivar int MEMBER_KICK: 20 - Member was removed from server |
| 38 | + :ivar int MEMBER_PRUNE: 21 - Members were pruned from server |
| 39 | + :ivar int MEMBER_BAN_ADD: 22 - Member was banned from server |
| 40 | + :ivar int MEMBER_BAN_REMOVE: 23 - Server ban was lifted for a member |
| 41 | + :ivar int MEMBER_UPDATE: 24 - Member was updated in server |
| 42 | + :ivar int MEMBER_ROLE_UPDATE: 25 - Member was added or removed from a role |
| 43 | + :ivar int MEMBER_MOVE: 26 - Member was moved to a different voice channel |
| 44 | + :ivar int MEMBER_DISCONNECT: 27 - Member was disconnected from a voice channel |
| 45 | + :ivar int BOT_ADD: 28 - Bot user was added to server |
| 46 | + :ivar int ROLE_CREATE: 30 - Role was created |
| 47 | + :ivar int ROLE_UPDATE: 31 - Role was updated |
| 48 | + :ivar int ROLE_DELETE: 32 - Role was deleted |
| 49 | + :ivar int INVITE_CREATE: 40 - Server invite was created |
| 50 | + :ivar int INVITE_UPDATE: 41 - Server invite was updated |
| 51 | + :ivar int INVITE_DELETE: 42 - Server invite was deleted |
| 52 | + :ivar int WEBHOOK_CREATE: 50 - Webhook was created |
| 53 | + :ivar int WEBHOOK_UPDATE: 51 - Webhook properties or channel were updated |
| 54 | + :ivar int WEBHOOK_DELETE: 52 - Webhook was deleted |
| 55 | + :ivar int EMOJI_CREATE: 60 - Emoji was created |
| 56 | + :ivar int EMOJI_UPDATE: 61 - Emoji name was updated |
| 57 | + :ivar int EMOJI_DELETE: 62 - Emoji was deleted |
| 58 | + :ivar int MESSAGE_DELETE: 72 - Single message was deleted |
| 59 | + :ivar int MESSAGE_BULK_DELETE: 73 - Multiple messages were deleted |
| 60 | + :ivar int MESSAGE_PIN: 74 - Message was pinned to a channel |
| 61 | + :ivar int MESSAGE_UNPIN: 75 - Message was unpinned from a channel |
| 62 | + :ivar int INTEGRATION_CREATE: 80 - App was added to server |
| 63 | + :ivar int INTEGRATION_UPDATE: 81 - App was updated (as an example, its scopes were updated) |
| 64 | + :ivar int INTEGRATION_DELETE: 82 - App was removed from server |
| 65 | + :ivar int STAGE_INSTANCE_CREATE: 83 - Stage instance was created (stage channel becomes live) |
| 66 | + :ivar int STAGE_INSTANCE_UPDATE: 84 - Stage instance details were updated |
| 67 | + :ivar int STAGE_INSTANCE_DELETE: 85 - Stage instance was deleted (stage channel no longer live) |
| 68 | + :ivar int STICKER_CREATE: 90 - Sticker was created |
| 69 | + :ivar int STICKER_UPDATE: 91 - Sticker details were updated |
| 70 | + :ivar int STICKER_DELETE: 92 - Sticker was deleted |
| 71 | + :ivar int GUILD_SCHEDULED_EVENT_CREATE: 100 - Event was created |
| 72 | + :ivar int GUILD_SCHEDULED_EVENT_UPDATE: 101 - Event was updated |
| 73 | + :ivar int GUILD_SCHEDULED_EVENT_DELETE: 102 - Event was cancelled |
| 74 | + :ivar int THREAD_CREATE: 110 - Thread was created in a channel |
| 75 | + :ivar int THREAD_UPDATE: 111 - Thread was updated |
| 76 | + :ivar int THREAD_DELETE: 112 - Thread was deleted |
| 77 | + :ivar int APPLICATION_COMMAND_PERMISSION_UPDATE: 121 - Permissions were updated for a command |
| 78 | + :ivar int AUTO_MODERATION_RULE_CREATE: 140 - Auto Moderation rule was created |
| 79 | + :ivar int AUTO_MODERATION_RULE_UPDATE: 141 - Auto Moderation rule was updated |
| 80 | + :ivar int AUTO_MODERATION_RULE_DELETE: 142 - Auto Moderation rule was deleted |
| 81 | + :ivar int AUTO_MODERATION_BLOCK_MESSAGE: 143 - Message was blocked by AutoMod (according to a rule) |
| 82 | + """ |
| 83 | + |
| 84 | + # guild related |
| 85 | + GUILD_UPDATE = 1 |
| 86 | + |
| 87 | + # channel related |
| 88 | + CHANNEL_CREATE = 10 |
| 89 | + CHANNEL_UPDATE = 11 |
| 90 | + CHANNEL_DELETE = 12 |
| 91 | + CHANNEL_OVERWRITE_CREATE = 13 |
| 92 | + CHANNEL_OVERWRITE_UPDATE = 14 |
| 93 | + CHANNEL_OVERWRITE_DELETE = 15 |
| 94 | + |
| 95 | + # member related |
| 96 | + MEMBER_KICK = 20 |
| 97 | + MEMBER_PRUNE = 21 |
| 98 | + MEMBER_BAN_ADD = 22 |
| 99 | + MEMBER_BAN_REMOVE = 23 |
| 100 | + MEMBER_UPDATE = 24 |
| 101 | + MEMBER_ROLE_UPDATE = 25 |
| 102 | + MEMBER_MOVE = 26 |
| 103 | + MEMBER_DISCONNECT = 27 |
| 104 | + BOT_ADD = 28 |
| 105 | + |
| 106 | + # role related |
| 107 | + ROLE_CREATE = 30 |
| 108 | + ROLE_UPDATE = 31 |
| 109 | + ROLE_DELETE = 32 |
| 110 | + |
| 111 | + # invite related |
| 112 | + INVITE_CREATE = 40 |
| 113 | + INVITE_UPDATE = 41 |
| 114 | + INVITE_DELETE = 42 |
| 115 | + |
| 116 | + # webhook related |
| 117 | + WEBHOOK_CREATE = 50 |
| 118 | + WEBHOOK_UPDATE = 51 |
| 119 | + WEBHOOK_DELETE = 52 |
| 120 | + |
| 121 | + # emoji related |
| 122 | + EMOJI_CREATE = 60 |
| 123 | + EMOJI_UPDATE = 61 |
| 124 | + EMOJI_DELETE = 62 |
| 125 | + |
| 126 | + # message related |
| 127 | + MESSAGE_DELETE = 72 |
| 128 | + MESSAGE_BULK_DELETE = 73 |
| 129 | + MESSAGE_PIN = 74 |
| 130 | + MESSAGE_UNPIN = 75 |
| 131 | + |
| 132 | + # integration related |
| 133 | + INTEGRATION_CREATE = 80 |
| 134 | + INTEGRATION_UPDATE = 81 |
| 135 | + INTEGRATION_DELETE = 82 |
| 136 | + |
| 137 | + # stage instance related |
| 138 | + STAGE_INSTANCE_CREATE = 83 |
| 139 | + STAGE_INSTANCE_UPDATE = 84 |
| 140 | + STAGE_INSTANCE_DELETE = 85 |
| 141 | + |
| 142 | + # sticker related |
| 143 | + STICKER_CREATE = 90 |
| 144 | + STICKER_UPDATE = 91 |
| 145 | + STICKER_DELETE = 92 |
| 146 | + |
| 147 | + # guild scheduled event related |
| 148 | + GUILD_SCHEDULED_EVENT_CREATE = 100 |
| 149 | + GUILD_SCHEDULED_EVENT_UPDATE = 101 |
| 150 | + GUILD_SCHEDULED_EVENT_DELETE = 102 |
| 151 | + |
| 152 | + # thread related |
| 153 | + THREAD_CREATE = 110 |
| 154 | + THREAD_UPDATE = 111 |
| 155 | + THREAD_DELETE = 112 |
| 156 | + |
| 157 | + # app-command permissions related |
| 158 | + APPLICATION_COMMAND_PERMISSION_UPDATE = 121 |
| 159 | + |
| 160 | + # auto mod related |
| 161 | + AUTO_MODERATION_RULE_CREATE = 140 |
| 162 | + AUTO_MODERATION_RULE_UPDATE = 141 |
| 163 | + AUTO_MODERATION_RULE_DELETE = 142 |
| 164 | + AUTO_MODERATION_BLOCK_MESSAGE = 143 |
| 165 | + |
| 166 | + |
| 167 | +@define() |
| 168 | +class AuditLogChange(DictSerializerMixin): |
| 169 | + """ |
| 170 | + A class object representing an AuditLogChange. |
| 171 | +
|
| 172 | + :ivar Optional[_T] new_value?: New value of the key |
| 173 | + :ivar Optional[_T] old_value?: Old value of the key |
| 174 | + :ivar str key: Name of the changed entity, with a few [exceptions](https://discord.com/developers/docs/resources/audit-log#audit-log-change-object-audit-log-change-exceptions) |
| 175 | + """ |
| 176 | + |
| 177 | + new_value: Optional[_T] = field(default=None) |
| 178 | + old_value: Optional[_T] = field(default=None) |
| 179 | + key: str = field() |
| 180 | + |
| 181 | + |
| 182 | +@define() |
| 183 | +class OptionalAuditEntryInfo(DictSerializerMixin): |
| 184 | + """ |
| 185 | + A class object representing OptionalAuditEntryInfo. |
| 186 | +
|
| 187 | + :ivar Snowflake application_id: ID of the app whose permissions were targeted. ``AuditLogEvents``-type: 121 |
| 188 | + :ivar Snowflake channel_id: Channel in which the entities were targeted. ``AuditLogEvents``-types: 26, 74, 75, 72, 83, 84, 85 |
| 189 | + :ivar str count: Number of entities that were targeted. ``AuditLogEvents``-types: 72, 73, 27, 26 |
| 190 | + :ivar str delete_member_days: Number of days after which inactive members were kicked. ``AuditLogEvents``-types: 21 |
| 191 | + :ivar Snowflake id: ID of the overwritten entity. ``AuditLogEvents``-types: 13, 14, 15 |
| 192 | + :ivar str members_removed: Number of members removed by the prune. ``AuditLogEvents``-types: 21 |
| 193 | + :ivar Snowflake message_id: ID of the message that was targeted. ``AuditLogEvents``-types: 74, 75 |
| 194 | + :ivar Optional[str] role_name: Name of the role if type is "0" (not present if type is "1"). ``AuditLogEvents``-types: 13, 14, 15 |
| 195 | + :ivar str type: Type of overwritten entity - role ("0") or member ("1"). ``AuditLogEvents``-types: 13, 14, 15 |
| 196 | + """ |
| 197 | + |
| 198 | + application_id: Snowflake = field(converter=Snowflake) |
| 199 | + channel_id: Snowflake = field(converter=Snowflake) |
| 200 | + count: str = field() |
| 201 | + delete_member_days: str = field() |
| 202 | + id: Snowflake = field(converter=Snowflake) |
| 203 | + members_removed: str = field() |
| 204 | + message_id: Snowflake = field(converter=Snowflake) |
| 205 | + role_name: Optional[str] = field(default=None) |
| 206 | + type: str = field() |
| 207 | + |
| 208 | + |
| 209 | +@define() |
| 210 | +class AuditLogEntry(DictSerializerMixin): |
| 211 | + """ |
| 212 | + A class object representing an AuditLogEntry. |
| 213 | +
|
| 214 | + :ivar Optional[str] target_id?: ID of the affected entity (webhook, user, role, etc.) |
| 215 | + :ivar Optional[List[AuditLogChange]] changes?: Changes made to the target_id |
| 216 | + :ivar Optional[Snowflake] user_id?: User or app that made the changes |
| 217 | + :ivar Snowflake id: ID of the entry |
| 218 | + :ivar AuditLogEvents action_type: Type of action that occurred |
| 219 | + :ivar OptionalAuditEntryInfo options?: Additional info for certain event types |
| 220 | + :ivar str reason?: Reason for the change (1-512 characters) |
| 221 | + """ |
| 222 | + |
| 223 | + target_id: Optional[str] = field(default=None) |
| 224 | + changes: Optional[List[AuditLogChange]] = field( |
| 225 | + converter=convert_list(AuditLogChange), default=None |
| 226 | + ) |
| 227 | + user_id: Optional[Snowflake] = field(converter=Snowflake, default=None) |
| 228 | + id: Snowflake = field(converter=Snowflake) |
| 229 | + action_type: AuditLogEvents = field(converter=AuditLogEvents) |
| 230 | + options: Optional[OptionalAuditEntryInfo] = field( |
| 231 | + converter=OptionalAuditEntryInfo, default=None |
| 232 | + ) |
| 233 | + reason: Optional[str] = field(default=None) |
| 234 | + |
| 235 | + |
| 236 | +@define() |
| 237 | +class AuditLogs(DictSerializerMixin): |
| 238 | + """ |
| 239 | + A class object representing the audit logs of a guild. |
| 240 | +
|
| 241 | + :ivar List[AuditLogEntry] audit_log_entries: List of audit log entries, sorted from most to least recent. |
| 242 | + :ivar List[AutoModerationRule] auto_moderation_rules: List of auto moderation rules referenced in the audit log. |
| 243 | + :ivar List[ScheduledEvents] guild_scheduled_events: List of guild scheduled events referenced in the audit log |
| 244 | + :ivar List[Integration] integrations: List of partial integration objects |
| 245 | + :ivar List[Channel] threads: List of threads referenced in the audit log |
| 246 | + :ivar List[User] users: List of users referenced in the audit log |
| 247 | + :ivar List[Webhook] webhooks: List of webhooks referenced in the audit log |
| 248 | + """ |
| 249 | + |
| 250 | + audit_log_entries: List[AuditLogEntry] = field( |
| 251 | + converter=convert_list(AuditLogEntry), default=None |
| 252 | + ) |
| 253 | + auto_moderation_rules: List["AutoModerationRule"] = field(default=None) |
| 254 | + guild_scheduled_events: List["ScheduledEvents"] = field(default=None) |
| 255 | + integrations: List["Integration"] = field(default=None) |
| 256 | + threads: List[Channel] = field(converter=convert_list(Channel), default=None) |
| 257 | + users: List[User] = field(converter=convert_list(User), default=None) |
| 258 | + webhooks: List[Webhook] = field(converter=convert_list(Webhook), default=None) |
| 259 | + |
| 260 | + def __attrs_post__init(self): |
| 261 | + if self.guild_scheduled_events: |
| 262 | + from .guild import ScheduledEvents |
| 263 | + |
| 264 | + self.guild_scheduled_events = [ |
| 265 | + ScheduledEvents(**event) for event in self.guild_scheduled_events |
| 266 | + ] |
| 267 | + if self.integrations: |
| 268 | + from .guild import Integration |
| 269 | + |
| 270 | + self.integrations = [Integration(**integration) for integration in self.integrations] |
| 271 | + |
| 272 | + if self.auto_moderation_rules: |
| 273 | + from .gw import AutoModerationRule |
| 274 | + |
| 275 | + self.auto_moderation_rules = [ |
| 276 | + AutoModerationRule(**rule) for rule in self.auto_moderation_rules |
| 277 | + ] |
0 commit comments