Browse Source

Further improve thread events

pull/10109/head
dolfies 3 years ago
parent
commit
13b6ab07fd
  1. 7
      discord/channel.py
  2. 37
      discord/http.py
  3. 14
      discord/state.py

7
discord/channel.py

@ -1960,7 +1960,6 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
:class:`Permissions`
The resolved permissions for the user.
"""
base = Permissions.text()
base.read_messages = True
base.send_tts_messages = False
@ -1999,7 +1998,6 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
HTTPException
Adding a recipient to this group failed.
"""
# TODO: wait for the corresponding WS event
await self._get_channel()
req = self._state.http.add_group_recipient
@ -2021,7 +2019,6 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
HTTPException
Removing a recipient from this group failed.
"""
# TODO: wait for the corresponding WS event
await self._get_channel()
req = self._state.http.remove_group_recipient
@ -2060,7 +2057,6 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
HTTPException
Editing the group failed.
"""
await self._get_channel()
try:
@ -2073,7 +2069,7 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
data = await self._state.http.edit_group(self.id, **fields)
if data is not None:
# the payload will always be the proper channel payload
# The payload will always be the proper channel payload
return self.__class__(me=self.me, state=self._state, data=payload) # type: ignore
async def leave(self) -> None:
@ -2088,7 +2084,6 @@ class GroupChannel(discord.abc.Messageable, discord.abc.Connectable, Hashable):
HTTPException
Leaving the group failed.
"""
await self._state.http.delete_channel(self.id)

37
discord/http.py

@ -298,9 +298,9 @@ class HTTPClient:
kwargs['data'] = utils._to_json(kwargs.pop('json'))
if 'context_properties' in kwargs:
context_properties = kwargs.pop('context_properties')
if isinstance(context_properties, ContextProperties):
headers['X-Context-Properties'] = context_properties.value
props = kwargs.pop('context_properties')
if isinstance(props, ContextProperties):
headers['X-Context-Properties'] = props.value
if kwargs.pop('super_properties_to_track', False):
headers['X-Track'] = headers.pop('X-Super-Properties')
@ -468,9 +468,6 @@ class HTTPClient:
return self.request(Route('POST', '/users/@me/channels'), json=payload, context_properties=props)
def leave_group(self, channel_id: Snowflake) -> Response[None]:
return self.request(Route('DELETE', '/channels/{channel_id}', channel_id=channel_id))
def add_group_recipient(self, channel_id: Snowflake, user_id: Snowflake): # TODO: return typings
r = Route('PUT', '/channels/{channel_id}/recipients/{user_id}', channel_id=channel_id, user_id=user_id)
return self.request(r)
@ -1497,13 +1494,13 @@ class HTTPClient:
message_id: Snowflake = MISSING,
): # TODO: response type
if message_id is not MISSING:
context_properties = ContextProperties._from_invite_embed(guild_id=guild_id, channel_id=channel_id, channel_type=channel_type, message_id=message_id) # Invite Button Embed
props = ContextProperties._from_invite_embed(guild_id=guild_id, channel_id=channel_id, channel_type=channel_type, message_id=message_id) # Invite Button Embed
else:
context_properties = choice(( # Join Guild, Accept Invite Page
props = choice(( # Join Guild, Accept Invite Page
ContextProperties._from_accept_invite_page(guild_id=guild_id, channel_id=channel_id, channel_type=channel_type),
ContextProperties._from_join_guild_popup(guild_id=guild_id, channel_id=channel_id, channel_type=channel_type)
))
return self.request(Route('POST', '/invites/{invite_id}', invite_id=invite_id), context_properties=context_properties, json={})
return self.request(Route('POST', '/invites/{invite_id}', invite_id=invite_id), context_properties=props, json={})
def create_invite(
self,
@ -1710,50 +1707,50 @@ class HTTPClient:
def remove_relationship(self, user_id: Snowflake, *, action: RelationshipAction) -> Response[None]:
r = Route('DELETE', '/users/@me/relationships/{user_id}', user_id=user_id)
if action is RelationshipAction.deny_request: # User Profile, Friends, DM Channel
context_properties = choice((
props = choice((
ContextProperties._from_friends_page(), ContextProperties._from_user_profile(),
ContextProperties._from_dm_channel()
))
elif action is RelationshipAction.unfriend: # Friends, ContextMenu, User Profile, DM Channel
context_properties = choice((
props = choice((
ContextProperties._from_context_menu(), ContextProperties._from_user_profile(),
ContextProperties._from_friends_page(), ContextProperties._from_dm_channel()
))
elif action == RelationshipAction.unblock: # Friends, ContextMenu, User Profile, DM Channel, NONE
context_properties = choice((
props = choice((
ContextProperties._from_context_menu(), ContextProperties._from_user_profile(),
ContextProperties._from_friends_page(), ContextProperties._from_dm_channel(), None
))
elif action == RelationshipAction.remove_pending_request: # Friends
context_properties = ContextProperties._from_friends_page()
props = ContextProperties._from_friends_page()
return self.request(r, context_properties=context_properties)
return self.request(r, context_properties=props)
def add_relationship(
self, user_id: Snowflake, type: int = MISSING, *, action: RelationshipAction
): # TODO: return type
r = Route('PUT', '/users/@me/relationships/{user_id}', user_id=user_id)
if action is RelationshipAction.accept_request: # User Profile, Friends, DM Channel
context_properties = choice((
props = choice((
ContextProperties._from_friends_page(),
ContextProperties._from_user_profile(),
ContextProperties._from_dm_channel()
))
elif action is RelationshipAction.block: # Friends, ContextMenu, User Profile, DM Channel.
context_properties = choice((
props = choice((
ContextProperties._from_context_menu(),
ContextProperties._from_user_profile(),
ContextProperties._from_friends_page(),
ContextProperties._from_dm_channel()
))
elif action is RelationshipAction.send_friend_request: # ContextMenu, User Profile, DM Channel
context_properties = choice((
props = choice((
ContextProperties._from_context_menu(),
ContextProperties._from_user_profile(),
ContextProperties._from_dm_channel()
))
kwargs = {
'context_properties': context_properties
'context_properties': props
}
if type:
kwargs['json'] = {'type': type}
@ -1762,7 +1759,7 @@ class HTTPClient:
def send_friend_request(self, username, discriminator): # TODO: return type
r = Route('POST', '/users/@me/relationships')
context_properties = choice(( # Friends, Group DM
props = choice(( # Friends, Group DM
ContextProperties._from_add_friend_page,
ContextProperties._from_group_dm
))
@ -1771,7 +1768,7 @@ class HTTPClient:
'discriminator': int(discriminator)
}
return self.request(r, json=payload, context_properties=context_properties)
return self.request(r, json=payload, context_properties=props)
def change_friend_nickname(self, user_id, nickname):
payload = {

14
discord/state.py

@ -1006,13 +1006,14 @@ class ConnectionState:
self.dispatch('thread_delete', thread)
def parse_thread_list_sync(self, data) -> None:
self_id = self.self_id
guild_id = int(data['guild_id'])
guild: Optional[Guild] = self._get_guild(guild_id)
if guild is None:
_log.debug('THREAD_LIST_SYNC referencing an unknown guild ID: %s. Discarding.', guild_id)
return
try:
try: # Never had this actually happen so IDK
channel_ids = set(data['channel_ids'])
except KeyError:
channel_ids = None
@ -1032,8 +1033,7 @@ class ConnectionState:
old_threads = [t for t in threads.values() if t not in new_threads]
for member in data.get('members', []):
try:
# Note: member['id'] is the thread_id
try: # Note: member['id'] is the thread_id
thread = threads[member['id']]
except KeyError:
continue
@ -1042,11 +1042,13 @@ class ConnectionState:
for k in new_threads.values():
guild._add_thread(k)
self.dispatch('thread_join', k)
self.dispatch('thread_create', k)
if self_id in k.member_ids:
self.dispatch('thread_join', k)
for k in old_threads:
del guild._threads[k.id]
self.dispatch('thread_remove', k)
self.dispatch('thread_delete', k) # Did the thread get deleted or did we get yeeted?
for message in data.get('most_recent_messages', []):
guild_id = utils._get_as_snowflake(message, 'guild_id')
@ -1348,8 +1350,6 @@ class ConnectionState:
self.dispatch('guild_join', guild)
def parse_guild_create(self, data):
guild_id = int(data['id'])
guild = self._get_create_guild(data)
if guild is None:

Loading…
Cancel
Save