From 75a52a5ac8b2ae70a706903db8f3004cbe065c6f Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 1 Mar 2019 18:25:22 -0300 Subject: [PATCH] storage: add voice state fetch --- litecord/storage.py | 25 ++++++++++++++++++++++--- litecord/voice/manager.py | 4 ++-- litecord/voice/state.py | 3 +++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/litecord/storage.py b/litecord/storage.py index 7638853..3911312 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -535,6 +535,27 @@ class Storage: return list(map(dict, roledata)) + async def guild_voice_states(self, guild_id: int, + user_id=None) -> List[Dict[str, Any]]: + """Get a list of voice states for the given guild.""" + channel_ids = await self.get_channel_ids(guild_id) + + res = [] + + for channel_id in channel_ids: + states = await self.app.voice.fetch_states(channel_id, user_id) + + jsonified = [s.as_json_for(user_id) for s in states] + + # discord does NOT insert guild_id to voice states on the + # guild voice state list. + for state in jsonified: + state.pop('guild_id') + + res.extend(jsonified) + + return res + async def get_guild_extra(self, guild_id: int, user_id=None, large=None) -> Dict: """Get extra information about a guild.""" @@ -575,9 +596,7 @@ class Storage: ), 'emojis': await self.get_guild_emojis(guild_id), - - # TODO: voice state management - 'voice_states': [], + 'voice_states': await self.guild_voice_states(guild_id), }} async def get_guild_full(self, guild_id: int, diff --git a/litecord/voice/manager.py b/litecord/voice/manager.py index 7a5c66f..3b0c77c 100644 --- a/litecord/voice/manager.py +++ b/litecord/voice/manager.py @@ -33,8 +33,8 @@ log = Logger(__name__) def _construct_state(state_dict: dict) -> VoiceState: """Create a VoiceState instance out of a dictionary with the VoiceState fields as keys.""" - fields = fields(VoiceState) - args = [state_dict[field.name] for field in fields] + state_fields = fields(VoiceState) + args = [state_dict[field.name] for field in state_fields] return VoiceState(*args) diff --git a/litecord/voice/state.py b/litecord/voice/state.py index 72708af..adb3c20 100644 --- a/litecord/voice/state.py +++ b/litecord/voice/state.py @@ -41,6 +41,9 @@ class VoiceState: """Generate JSON-serializable version, given a user ID.""" self_dict = asdict(self) + if user_id is None: + return self_dict + # state.suppress is defined by the user # that is currently viewing the state.