pubsub: add intents logic for Guild Member Update

This commit is contained in:
Luna 2021-07-14 15:14:51 -03:00
parent a51840487f
commit f3af9b5f11
1 changed files with 19 additions and 6 deletions

View File

@ -36,6 +36,22 @@ class GuildFlags(ChannelFlags):
presence: bool presence: bool
def can_dispatch(event_type, event_data, state) -> bool:
# If we're sending to the same user for this kind of event,
# bypass event logic (always send)
if event_type == "GUILD_MEMBER_UPDATE":
user_id = int(event_data["user"])
return user_id == state.user_id
# TODO Guild Create and Req Guild Members have specific
# logic regarding the presence intent.
wanted_intent = EVENTS_TO_INTENTS.get(event_type)
if wanted_intent is not None:
state_has_intent = (state.intents & wanted_intent) == wanted_intent
return state_has_intent
class GuildDispatcher( class GuildDispatcher(
DispatcherWithFlags[int, str, GatewayEvent, List[str], GuildFlags] DispatcherWithFlags[int, str, GatewayEvent, List[str], GuildFlags]
): ):
@ -53,7 +69,7 @@ class GuildDispatcher(
): ):
session_ids = self.state[guild_id] session_ids = self.state[guild_id]
sessions: List[str] = [] sessions: List[str] = []
event_type, _ = event event_type, event_data = event
for session_id in set(session_ids): for session_id in set(session_ids):
if not filter_function(session_id): if not filter_function(session_id):
@ -69,11 +85,8 @@ class GuildDispatcher(
await self.unsub(guild_id, session_id) await self.unsub(guild_id, session_id)
continue continue
wanted_intent = EVENTS_TO_INTENTS.get(event_type) if not can_dispatch(event_type, event_data, state):
if wanted_intent is not None: continue
state_has_intent = (state.intents & wanted_intent) == wanted_intent
if not state_has_intent:
continue
try: try:
await state.ws.dispatch(*event) await state.ws.dispatch(*event)