From 9f1c511b474536c749869a2a80b74750cdafbdf3 Mon Sep 17 00:00:00 2001 From: z03h <7235242+z03h@users.noreply.github.com> Date: Tue, 26 Jul 2022 06:38:36 -0700 Subject: [PATCH] Fix Guild.afk_channel and VoiceState.channel being None at startup --- discord/guild.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index ac5e46945..cef709fb5 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -520,22 +520,12 @@ class Guild(Hashable): self._scheduled_events[scheduled_event.id] = scheduled_event self.owner_id: Optional[int] = utils._get_as_snowflake(guild, 'owner_id') - self.afk_channel: Optional[VocalGuildChannel] = self.get_channel(utils._get_as_snowflake(guild, 'afk_channel_id')) # type: ignore - - for obj in guild.get('voice_states', []): - self._update_voice_state(obj, int(obj['channel_id'])) - - cache_joined = self._state.member_cache_flags.joined - cache_voice = self._state.member_cache_flags.voice - self_id = self._state.self_id - for mdata in guild.get('members', []): - member = Member(data=mdata, guild=self, state=state) # type: ignore # Members will have the 'user' key in this scenario - if cache_joined or member.id == self_id or (cache_voice and member.id in self._voice_states): - self._add_member(member) self._sync(guild) self._large: Optional[bool] = None if self._member_count is None else self._member_count >= 250 + self.afk_channel: Optional[VocalGuildChannel] = self.get_channel(utils._get_as_snowflake(guild, 'afk_channel_id')) # type: ignore + # TODO: refactor/remove? def _sync(self, data: GuildPayload) -> None: try: @@ -543,13 +533,6 @@ class Guild(Hashable): except KeyError: pass - empty_tuple = tuple() - for presence in data.get('presences', []): - user_id = int(presence['user']['id']) - member = self.get_member(user_id) - if member is not None: - member._presence_update(presence, empty_tuple) # type: ignore - if 'channels' in data: channels = data['channels'] for c in channels: @@ -557,6 +540,24 @@ class Guild(Hashable): if factory: self._add_channel(factory(guild=self, data=c, state=self._state)) # type: ignore + for obj in data.get('voice_states', []): + self._update_voice_state(obj, int(obj['channel_id'])) + + cache_joined = self._state.member_cache_flags.joined + cache_voice = self._state.member_cache_flags.voice + self_id = self._state.self_id + for mdata in data.get('members', []): + member = Member(data=mdata, guild=self, state=self._state) # type: ignore # Members will have the 'user' key in this scenario + if cache_joined or member.id == self_id or (cache_voice and member.id in self._voice_states): + self._add_member(member) + + empty_tuple = tuple() + for presence in data.get('presences', []): + user_id = int(presence['user']['id']) + member = self.get_member(user_id) + if member is not None: + member._presence_update(presence, empty_tuple) # type: ignore + if 'threads' in data: threads = data['threads'] for thread in threads: