storage: add voice state fetch

This commit is contained in:
Luna 2019-03-01 18:25:22 -03:00
parent ec738cd41e
commit 75a52a5ac8
3 changed files with 27 additions and 5 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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.