Skip to content

Commit

Permalink
Add methods to LavalinkPlugin for loading tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Dec 9, 2023
1 parent 1339def commit 2d29533
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/lavalink/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class HttpLavalinkClient {

/// Unmark a failed address in the RoutePlanner extension.
Future<void> unmarkFailedAddress(String address) async {
jsonDecode(await _executeSafe(
await _executeSafe(
'POST',
'/v4/routeplanner/free/address',
body: {'address': address},
));
);
}

/// Unmark all failed addresses in the RoutePlanner extension.
Expand Down
34 changes: 30 additions & 4 deletions packages/nyxx_lavalink/lib/src/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LavalinkPlugin extends NyxxPlugin<NyxxGateway> {

final Uri base;
final String password;
final LavalinkClient? innerClient;
final LavalinkClient? _innerClient;

final StreamController<LavalinkMessage> _messagesController = StreamController.broadcast();
Stream<LavalinkMessage> get onMessage => _messagesController.stream;
Expand All @@ -40,11 +40,37 @@ class LavalinkPlugin extends NyxxPlugin<NyxxGateway> {
LavalinkPlugin({
required this.base,
required this.password,
this.innerClient,
});
LavalinkClient? innerClient,
}) : _innerClient = innerClient;

@override
NyxxPluginState<NyxxGateway, LavalinkPlugin> createState() => _LavalinkPluginState(this);

Future<T> _withClient<T>(Future<T> Function(HttpLavalinkClient client) f) async {
if (_innerClient case final innerClient?) {
return f(innerClient);
} else {
final client = HttpLavalinkClient(base: base, password: password);
final result = await f(client);
await client.close();
return result;
}
}

Future<LoadResult> loadTrack(String identifier) =>
_withClient((client) => client.loadTrack(identifier));

Future<Track> decodeTrack(String encodedTrack) =>
_withClient((client) => client.decodeTrack(encodedTrack));

Future<List<Track>> decodeTracks(List<String> encodedTracks) =>
_withClient((client) => client.decodeTracks(encodedTracks));

Future<LavalinkInfo> getInfo() => _withClient((client) => client.getInfo());

Future<LavalinkStats> getStats() => _withClient((client) => client.getStats());

Future<String> getVersion() => _withClient((client) => client.getVersion());
}

class _LavalinkPluginState extends NyxxPluginState<NyxxGateway, LavalinkPlugin> {
Expand All @@ -59,7 +85,7 @@ class _LavalinkPluginState extends NyxxPluginState<NyxxGateway, LavalinkPlugin>
Future<void> afterConnect(NyxxGateway client) async {
await super.afterConnect(client);

lavalinkClient = plugin.innerClient ??
lavalinkClient = plugin._innerClient ??
await LavalinkClient.connect(
plugin.base,
password: plugin.password,
Expand Down

0 comments on commit 2d29533

Please sign in to comment.