Skip to content

Commit

Permalink
Add fetch voice state endpoints (#692)
Browse files Browse the repository at this point in the history
* Add fetch voice state endpoints

* Add tests
abitofevrything authored Sep 7, 2024
1 parent 7022767 commit b91d782
Showing 2 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/src/http/managers/voice_manager.dart
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import 'package:nyxx/src/http/route.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/voice/voice_region.dart';
import 'package:nyxx/src/models/voice/voice_state.dart';
import 'package:nyxx/src/utils/cache_helpers.dart';
import 'package:nyxx/src/utils/parsing_helpers.dart';

/// A manager for [VoiceState]s.
@@ -64,4 +65,30 @@ class VoiceManager {
final response = await client.httpHandler.executeSafe(request);
return parseMany(response.jsonBody as List, parseVoiceRegion);
}

Future<VoiceState> fetchCurrentUserVoiceState(Snowflake guildId) async {
final route = HttpRoute()
..guilds(id: guildId.toString())
..voiceStates(id: '@me');
final request = BasicRequest(route);

final response = await client.httpHandler.executeSafe(request);
final state = parseVoiceState(response.jsonBody as Map<String, Object?>);

client.updateCacheWith(state);
return state;
}

Future<VoiceState> fetchVoiceState(Snowflake guildId, Snowflake userId) async {
final route = HttpRoute()
..guilds(id: guildId.toString())
..voiceStates(id: userId.toString());
final request = BasicRequest(route);

final response = await client.httpHandler.executeSafe(request);
final state = parseVoiceState(response.jsonBody as Map<String, Object?>);

client.updateCacheWith(state);
return state;
}
}
14 changes: 14 additions & 0 deletions test/unit/http/managers/voice_manager_test.dart
Original file line number Diff line number Diff line change
@@ -93,5 +93,19 @@ void main() {
(client) => client.voice.listRegions(),
response: [sampleVoiceRegion],
);

testEndpoint(
name: 'fetchCurrentUserVoiceState',
'/guilds/0/voice-states/@me',
(client) => client.voice.fetchCurrentUserVoiceState(Snowflake.zero),
response: sampleVoiceState,
);

testEndpoint(
name: 'fetchVoiceState',
'/guilds/0/voice-states/1',
(client) => client.voice.fetchVoiceState(Snowflake.zero, Snowflake(1)),
response: sampleVoiceState,
);
});
}

0 comments on commit b91d782

Please sign in to comment.