Browse Source

Convert Webhook targets in AuditlogEntry

pull/10109/head
z03h 2 years ago
committed by dolfies
parent
commit
8dc160689c
  1. 15
      discord/audit_logs.py
  2. 7
      discord/guild.py
  3. 1
      discord/state.py

15
discord/audit_logs.py

@ -71,10 +71,7 @@ if TYPE_CHECKING:
from .types.snowflake import Snowflake from .types.snowflake import Snowflake
from .types.automod import AutoModerationTriggerMetadata, AutoModerationAction from .types.automod import AutoModerationTriggerMetadata, AutoModerationAction
from .user import User from .user import User
from .stage_instance import StageInstance from .webhook import Webhook
from .sticker import GuildSticker
from .threads import Thread
from .automod import AutoModRule, AutoModTrigger
TargetType = Union[ TargetType = Union[
Guild, Guild,
@ -89,6 +86,8 @@ if TYPE_CHECKING:
Thread, Thread,
Object, Object,
AutoModRule, AutoModRule,
ScheduledEvent,
Webhook,
None, None,
] ]
@ -542,6 +541,7 @@ class AuditLogEntry(Hashable):
*, *,
users: Mapping[int, User], users: Mapping[int, User],
automod_rules: Mapping[int, AutoModRule], automod_rules: Mapping[int, AutoModRule],
webhooks: Mapping[int, Webhook],
data: AuditLogEntryPayload, data: AuditLogEntryPayload,
guild: Guild, guild: Guild,
): ):
@ -549,6 +549,7 @@ class AuditLogEntry(Hashable):
self.guild: Guild = guild self.guild: Guild = guild
self._users: Mapping[int, User] = users self._users: Mapping[int, User] = users
self._automod_rules: Mapping[int, AutoModRule] = automod_rules self._automod_rules: Mapping[int, AutoModRule] = automod_rules
self._webhooks: Mapping[int, Webhook] = webhooks
self._from_data(data) self._from_data(data)
def _from_data(self, data: AuditLogEntryPayload) -> None: def _from_data(self, data: AuditLogEntryPayload) -> None:
@ -750,3 +751,9 @@ class AuditLogEntry(Hashable):
def _convert_target_auto_moderation(self, target_id: int) -> Union[AutoModRule, Object]: def _convert_target_auto_moderation(self, target_id: int) -> Union[AutoModRule, Object]:
return self._automod_rules.get(target_id) or Object(target_id, type=AutoModRule) return self._automod_rules.get(target_id) or Object(target_id, type=AutoModRule)
def _convert_target_webhook(self, target_id: int) -> Union[Webhook, Object]:
# circular import
from .webhook import Webhook
return self._webhooks.get(target_id) or Object(target_id, type=Webhook)

7
discord/guild.py

@ -4283,6 +4283,9 @@ class Guild(Hashable):
if after: if after:
predicate = lambda m: int(m['id']) > after.id predicate = lambda m: int(m['id']) > after.id
# Circular import
from .webhook import Webhook
while True: while True:
retrieve = 100 if limit is None else min(limit, 100) retrieve = 100 if limit is None else min(limit, 100)
if retrieve < 1: if retrieve < 1:
@ -4302,6 +4305,9 @@ class Guild(Hashable):
) )
automod_rule_map = {rule.id: rule for rule in automod_rules} automod_rule_map = {rule.id: rule for rule in automod_rules}
webhooks = (Webhook.from_state(data=raw_webhook, state=self._state) for raw_webhook in data.get('webhooks', []))
webhook_map = {webhook.id: webhook for webhook in webhooks}
count = 0 count = 0
for count, raw_entry in enumerate(raw_entries, 1): for count, raw_entry in enumerate(raw_entries, 1):
@ -4313,6 +4319,7 @@ class Guild(Hashable):
data=raw_entry, data=raw_entry,
users=user_map, users=user_map,
automod_rules=automod_rule_map, automod_rules=automod_rule_map,
webhooks=webhook_map,
guild=self, guild=self,
) )

1
discord/state.py

@ -2134,6 +2134,7 @@ class ConnectionState:
entry = AuditLogEntry( entry = AuditLogEntry(
users=self._users, users=self._users,
automod_rules={}, automod_rules={},
webhooks={},
data=data, data=data,
guild=guild, guild=guild,
) )

Loading…
Cancel
Save