Skip to content

Commit

Permalink
Use Asset for AuditLogChanges and add more entries
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz authored Apr 25, 2021
1 parent 368fda7 commit 1765cdf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
60 changes: 55 additions & 5 deletions discord/audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from .colour import Colour
from .invite import Invite
from .mixins import Hashable
from .asset import Asset

__all__ = (
'AuditLogDiff',
Expand All @@ -50,36 +51,45 @@
def _transform_verification_level(entry, data):
return enums.try_enum(enums.VerificationLevel, data)


def _transform_default_notifications(entry, data):
return enums.try_enum(enums.NotificationLevel, data)


def _transform_explicit_content_filter(entry, data):
return enums.try_enum(enums.ContentFilter, data)


def _transform_permissions(entry, data):
return Permissions(int(data))


def _transform_color(entry, data):
return Colour(data)


def _transform_snowflake(entry, data):
return int(data)


def _transform_channel(entry, data):
if data is None:
return None
return entry.guild.get_channel(int(data)) or Object(id=data)


def _transform_owner_id(entry, data):
if data is None:
return None
return entry._get_member(int(data))


def _transform_inviter_id(entry, data):
if data is None:
return None
return entry._get_member(int(data))


def _transform_overwrites(entry, data):
overwrites = []
for elem in data:
Expand All @@ -102,12 +112,40 @@ def _transform_overwrites(entry, data):

return overwrites


def _transform_channeltype(entry, data):
return enums.try_enum(enums.ChannelType, data)


def _transform_voiceregion(entry, data):
return enums.try_enum(enums.VoiceRegion, data)


def _transform_video_quality_mode(entry, data):
return enums.try_enum(enums.VideoQualityMode, data)


def _transform_icon(entry, data):
if data is None:
return None
return Asset._from_guild_icon(entry._state, entry.guild.id, data)


def _transform_avatar(entry, data):
if data is None:
return None
return Asset._from_avatar(entry._state, entry._target_id, data)


def _guild_hash_transformer(path):
def _transform(entry, data):
if data is None:
return None
return Asset._from_guild_image(entry._state, entry.guild.id, data, path=path)

return _transform


class AuditLogDiff:
def __len__(self):
return len(self.__dict__)
Expand All @@ -119,7 +157,9 @@ def __repr__(self):
values = ' '.join('%s=%r' % item for item in self.__dict__.items())
return f'<AuditLogDiff {values}>'


class AuditLogChanges:
# fmt: off
TRANSFORMERS = {
'verification_level': (None, _transform_verification_level),
'explicit_content_filter': (None, _transform_explicit_content_filter),
Expand All @@ -135,15 +175,19 @@ class AuditLogChanges:
'system_channel_id': ('system_channel', _transform_channel),
'widget_channel_id': ('widget_channel', _transform_channel),
'permission_overwrites': ('overwrites', _transform_overwrites),
'splash_hash': ('splash', None),
'icon_hash': ('icon', None),
'avatar_hash': ('avatar', None),
'splash_hash': ('splash', _guild_hash_transformer('splashes')),
'banner_hash': ('banner', _guild_hash_transformer('banners')),
'discovery_splash_hash': ('discovery_splash', _guild_hash_transformer('discovery-splashes')),
'icon_hash': ('icon', _transform_icon),
'avatar_hash': ('avatar', _transform_avatar),
'rate_limit_per_user': ('slowmode_delay', None),
'default_message_notifications': ('default_notifications', _transform_default_notifications),
'region': (None, _transform_voiceregion),
'rtc_region': (None, _transform_voiceregion),
'video_quality_mode': (None, _transform_video_quality_mode),
'type': (None, _transform_channeltype),
}
# fmt: on

def __init__(self, entry, data: List[AuditLogChangePayload]):
self.before = AuditLogDiff()
Expand Down Expand Up @@ -190,6 +234,9 @@ def __init__(self, entry, data: List[AuditLogChangePayload]):
if hasattr(self.after, 'colour'):
self.after.color = self.after.colour
self.before.color = self.before.colour
if hasattr(self.after, 'expire_behavior'):
self.after.expire_behaviour = self.after.expire_behavior
self.before.expire_behaviour = self.before.expire_behavior

def __repr__(self):
return f'<AuditLogChanges before={self.before!r} after={self.after!r}>'
Expand All @@ -207,12 +254,13 @@ def _handle_role(self, first, second, entry, elem):

if role is None:
role = Object(id=role_id)
role.name = e['name'] # type: ignore
role.name = e['name'] # type: ignore

data.append(role)

setattr(second, 'roles', data)


class AuditLogEntry(Hashable):
r"""Represents an Audit Log entry.
Expand Down Expand Up @@ -278,7 +326,7 @@ def _from_data(self, data):
channel_id = int(self.extra['channel_id'])
elems = {
'count': int(self.extra['count']),
'channel': self.guild.get_channel(channel_id) or Object(id=channel_id)
'channel': self.guild.get_channel(channel_id) or Object(id=channel_id),
}
self.extra = type('_AuditLogProxy', (), elems)()
elif self.action is enums.AuditLogAction.member_disconnect:
Expand All @@ -291,10 +339,12 @@ def _from_data(self, data):
# the pin actions have a dict with some information
channel_id = int(self.extra['channel_id'])
message_id = int(self.extra['message_id'])
# fmt: off
elems = {
'channel': self.guild.get_channel(channel_id) or Object(id=channel_id),
'message_id': message_id
}
# fmt: on
self.extra = type('_AuditLogProxy', (), elems)()
elif self.action.name.startswith('overwrite_'):
# the overwrite_ actions have a dict with some information
Expand Down
30 changes: 23 additions & 7 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ of :class:`enum.Enum`.
- Changing the guild invite splash
- Changing the guild AFK channel or timeout
- Changing the guild voice server region
- Changing the guild icon
- Changing the guild icon, banner, or discovery splash
- Changing the guild moderation settings
- Changing things related to the guild widget

Expand All @@ -1365,6 +1365,9 @@ of :class:`enum.Enum`.
- :attr:`~AuditLogDiff.name`
- :attr:`~AuditLogDiff.owner`
- :attr:`~AuditLogDiff.splash`
- :attr:`~AuditLogDiff.discovery_splash`
- :attr:`~AuditLogDiff.icon`
- :attr:`~AuditLogDiff.banner`
- :attr:`~AuditLogDiff.vanity_url_code`

.. attribute:: channel_create
Expand Down Expand Up @@ -1696,6 +1699,7 @@ of :class:`enum.Enum`.

- :attr:`~AuditLogDiff.channel`
- :attr:`~AuditLogDiff.name`
- :attr:`~AuditLogDiff.avatar`

.. attribute:: webhook_delete

Expand Down Expand Up @@ -2198,15 +2202,27 @@ AuditLogDiff

.. attribute:: icon

A guild's icon hash. See also :attr:`Guild.icon`.
A guild's icon. See also :attr:`Guild.icon`.

:type: :class:`str`
:type: :class:`Asset`

.. attribute:: splash

The guild's invite splash hash. See also :attr:`Guild.splash`.
The guild's invite splash. See also :attr:`Guild.splash`.

:type: :class:`str`
:type: :class:`Asset`

.. attribute:: discovery_splash

The guild's discovery splash. See also :attr:`Guild.discovery_splash`.

:type: :class:`Asset`

.. attribute:: banner

The guild's banner. See also :attr:`Guild.banner`.

:type: :class:`Asset`

.. attribute:: owner

Expand Down Expand Up @@ -2492,11 +2508,11 @@ AuditLogDiff

.. attribute:: avatar

The avatar hash of a member.
The avatar of a member.

See also :attr:`User.avatar`.

:type: :class:`str`
:type: :class:`Asset`

.. attribute:: slowmode_delay

Expand Down

0 comments on commit 1765cdf

Please sign in to comment.