Skip to content

Commit adff41d

Browse files
committed
feat: Implement GUILD_AUDIT_LOG_ENTRY_CREATE event
1 parent 1159112 commit adff41d

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

discord/audit_logs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def _handle_role(self, first, second, entry, elem):
188188

189189
setattr(second, 'roles', data)
190190

191+
191192
class AuditLogEntry(Hashable):
192193
r"""Represents an Audit Log entry.
193194

discord/enums.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2424
DEALINGS IN THE SOFTWARE.
2525
"""
26+
from __future__ import annotations
2627

2728
import types
2829
from typing import Union, Any, Type
@@ -79,22 +80,22 @@
7980
)
8081

8182

82-
def _create_value_cls(name):
83+
def _create_value_cls(name) -> Type[tuple]:
8384
cls = namedtuple(f'_EnumValue_' + name, 'name value')
8485
cls.__repr__ = lambda self: '<%s.%s: %r>' % (name, self.name, self.value)
8586
cls.__str__ = lambda self: '%s.%s' % (name, self.name)
8687

8788
def __getattribute__(self, n) -> Union[bool, Any]:
89+
# With this you can use something like some_channel.type.text to check if it is of this type
90+
# It is similar to "some_channel.type == ChannelType.text"
8891
if n in dir(cls):
8992
return super(cls, self).__getattribute__(n)
9093
else:
9194
if n in self._actual_enum_cls_.__members__:
9295
return self.name == n
9396
else:
9497
raise AttributeError(f'{self.__class__.__name__} has no attribute {n}.')
95-
96-
# With this you can use something like some_channel.type.text to check if it is of this type
97-
# It is similar to "some_channel.type == ChannelType.text"
98+
9899
cls.__getattribute__ = __getattribute__
99100
return cls
100101

@@ -104,7 +105,7 @@ def _is_descriptor(obj):
104105

105106

106107
class EnumMeta(type):
107-
def __new__(cls, name, bases, attrs):
108+
def __new__(mcs, name, bases, attrs):
108109
value_mapping = {}
109110
member_mapping = {}
110111
member_names = []
@@ -127,45 +128,44 @@ def __new__(cls, name, bases, attrs):
127128
try:
128129
new_value = value_mapping[value]
129130
except KeyError:
130-
new_value = value_cls(name=key, value=value)
131+
new_value = value_cls(name=key, value=value) # type: ignore
131132
value_mapping[value] = new_value
132133
member_names.append(key)
133134

134135
member_mapping[key] = new_value
135136
attrs[key] = new_value
136137

137-
138138
attrs['_enum_value_map_'] = value_mapping
139139
attrs['_enum_member_map_'] = member_mapping
140140
attrs['_enum_member_names_'] = member_names
141-
actual_cls = super().__new__(cls, name, bases, attrs)
141+
actual_cls = super().__new__(mcs, name, bases, attrs)
142142
value_cls._actual_enum_cls_ = actual_cls
143143
return actual_cls
144144

145145
def __iter__(cls):
146-
return (cls._enum_member_map_[name] for name in cls._enum_member_names_)
146+
return (cls._enum_member_map_[name] for name in cls._enum_member_names_) # type: ignore
147147

148148
def __reversed__(cls):
149-
return (cls._enum_member_map_[name] for name in reversed(cls._enum_member_names_))
149+
return (cls._enum_member_map_[name] for name in reversed(cls._enum_member_names_)) # type: ignore
150150

151151
def __len__(cls):
152-
return len(cls._enum_member_names_)
152+
return len(cls._enum_member_names_) # type: ignore
153153

154154
def __repr__(cls):
155155
return '<enum %r>' % cls.__name__
156156

157157
@property
158158
def __members__(cls):
159-
return types.MappingProxyType(cls._enum_member_map_)
159+
return types.MappingProxyType(cls._enum_member_map_) # type: ignore
160160

161161
def __call__(cls, value):
162162
try:
163-
return cls._enum_value_map_[value]
163+
return cls._enum_value_map_[value] # type: ignore
164164
except (KeyError, TypeError):
165165
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
166166

167167
def __getitem__(cls, key):
168-
return cls._enum_member_map_[key]
168+
return cls._enum_member_map_[key] # type: ignore
169169

170170
def __setattr__(cls, name, value):
171171
raise TypeError('Enums are immutable.')
@@ -177,7 +177,7 @@ def __instancecheck__(self, instance):
177177
# isinstance(x, Y)
178178
# -> __instancecheck__(Y, x)
179179
try:
180-
return instance._actual_enum_cls_ is self
180+
return instance._actual_enum_cls_ is self # type: ignore
181181
except AttributeError:
182182
return False
183183

@@ -186,7 +186,7 @@ class Enum(metaclass=EnumMeta):
186186
@classmethod
187187
def try_value(cls, value):
188188
try:
189-
return cls._enum_value_map_[value]
189+
return cls._enum_value_map_[value] # type: ignore
190190
except (KeyError, TypeError):
191191
return value
192192

discord/state.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
from .guild import Guild
4343
from .activity import BaseActivity
44+
from .audit_logs import AuditLogEntry
4445
from .scheduled_event import GuildScheduledEvent
4546
from .user import User, ClientUser
4647
from .emoji import Emoji
@@ -1330,7 +1331,15 @@ def parse_auto_moderation_action_execution(self, data):
13301331
self.dispatch('automod_action', payload)
13311332
else:
13321333
log.debug('AUTO_MODERATION_ACTION_EXECUTION referencing an unknown guild ID: %s. Discarding.', data['guild_id'])
1333-
1334+
1335+
def parse_guild_audit_log_entry_create(self, data):
1336+
guild = self._get_guild(int(data['guild_id']))
1337+
if guild is not None:
1338+
entry = AuditLogEntry(guild=guild, data=data, users=self._users)
1339+
self.dispatch('audit_log_entry_create', guild, entry)
1340+
else:
1341+
log.debug('GUILD_AUDIT_LOG_ENTRY_CREATE referencing an unknown guild ID: %s. Discarding.', data['guild_id'])
1342+
13341343
def _get_reaction_user(self, channel, user_id):
13351344
if isinstance(channel, TextChannel):
13361345
return channel.guild.get_member(user_id)

docs/api.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ to handle it, which defaults to print a traceback and ignoring the exception.
303303
:param exception: The exception that was raised
304304
:type exception: :class:`Exception`
305305

306+
.. function:: on_audit_log_entry_create(guild, entry)
307+
308+
Called whenever a guild audit log entry is created.
309+
310+
.. note::
311+
This event is only sent to bots with the :attr:`~Permissions.view_audit_log` permission.
312+
313+
.. versionadded:: 2.0
314+
315+
:param guild: The guild that the audit log entry was created in.
316+
:type guild: :class:`Guild`
317+
:param entry: The audit log entry that was created.
318+
:type entry: :class:`AuditLogEntry`
319+
306320
.. function:: on_socket_raw_receive(msg)
307321

308322
Called whenever a message is received from the WebSocket, before

0 commit comments

Comments
 (0)