From 61723aa441ce27c00967c1c464a4ef6ec04b8fe1 Mon Sep 17 00:00:00 2001 From: "feroxfoxxo@gmail.com" Date: Mon, 11 Dec 2023 23:45:42 +1300 Subject: [PATCH] Don't setup lavalink on ready as this is done automatically --- backend/Music/Commands/Play.cs | 18 +++------ backend/Music/Commands/PlayPlaylist.cs | 27 +++++++------ backend/Music/Commands/PlayStream.cs | 38 +++++++++++++++++++ backend/Music/Enums/MusicSource.cs | 2 +- backend/Music/Extensions/SourceToTrackType.cs | 2 +- backend/Music/MusicModule.cs | 12 +++--- backend/Music/Services/MusicService.cs | 7 ---- 7 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 backend/Music/Commands/PlayStream.cs diff --git a/backend/Music/Commands/Play.cs b/backend/Music/Commands/Play.cs index 6b9b313fa..f3062910a 100644 --- a/backend/Music/Commands/Play.cs +++ b/backend/Music/Commands/Play.cs @@ -3,15 +3,11 @@ using Discord; using Discord.Interactions; using Discord.WebSocket; -using Lavalink4NET.Integrations.Lavasearch; -using Lavalink4NET.Integrations.Lavasearch.Extensions; using Lavalink4NET.Players; -using Lavalink4NET.Rest.Entities.Tracks; using Lavalink4NET.Tracks; using Music.Abstractions; using Music.Enums; using Music.Extensions; -using System.Collections.Immutable; using System.Text; namespace Music.Commands; @@ -22,14 +18,14 @@ public class PlayCommand : MusicCommand [BotChannel] public async Task Play( [Summary("query", "Music query")] string query, - [Summary("source", "Music source")] MusicSource source) + [Summary("source", "Music source")] MusicSource source = MusicSource.Default) { StringBuilder tInfoSb = new(); + var searchMode = source.GetSearchMode(); if (Uri.IsWellFormedUriString(query, UriKind.Absolute)) { - var search = await Audio.Tracks.SearchAsync(query); - var track = search.Tracks.FirstOrDefault(); + var track = await Audio.Tracks.LoadTrackAsync(query, searchMode); if (track == null) { @@ -42,13 +38,9 @@ public async Task Play( } else { - if (!string.IsNullOrEmpty(query)) + if (query != null) { - var tracks = await Audio.Tracks.SearchAsync( - query: query, - loadOptions: new TrackLoadOptions(SearchMode: source.GetSearchMode()), - categories: ImmutableArray.Create(SearchCategory.Track) - ); + var tracks = await Audio.Tracks.LoadTracksAsync(query, searchMode); var lavalinkTracks = tracks.Tracks.ToList(); diff --git a/backend/Music/Commands/PlayPlaylist.cs b/backend/Music/Commands/PlayPlaylist.cs index 6ed2982a9..d84dccd9f 100644 --- a/backend/Music/Commands/PlayPlaylist.cs +++ b/backend/Music/Commands/PlayPlaylist.cs @@ -2,15 +2,12 @@ using Discord; using Discord.Interactions; using Fergun.Interactive; -using Lavalink4NET.Integrations.Lavasearch; -using Lavalink4NET.Integrations.Lavasearch.Extensions; using Lavalink4NET.Players; using Lavalink4NET.Rest.Entities.Tracks; using Lavalink4NET.Tracks; using Music.Abstractions; using Music.Enums; using Music.Extensions; -using System.Collections.Immutable; using System.Text; namespace Music.Commands; @@ -22,16 +19,19 @@ public class PlaylistCommand : MusicCommand [SlashCommand("play-playlist", "Add tracks from a playlist to queue")] [BotChannel] public async Task PlayPlaylist( - [Summary("playlist-url", "Playlist URL")] - string playlistUrl, MusicSource source = MusicSource.None) + [Summary("playlist-url", "Playlist URL")] string playlistUrl, + [Summary("source", "Music source")] MusicSource source = MusicSource.Default) { - var searchResult = await Audio.Tracks.SearchAsync( - query: playlistUrl, - loadOptions: source == MusicSource.None ? default : new TrackLoadOptions(SearchMode: source.GetSearchMode()), - categories: ImmutableArray.Create(SearchCategory.Playlist) - ); + if (!Uri.IsWellFormedUriString(playlistUrl, UriKind.Absolute)) + { + await RespondInteraction("You need to provide a valid URL"); + return; + } - if (searchResult.Tracks.Length <= 0) + var tracks = await Audio.Tracks.LoadTracksAsync(playlistUrl, source.GetSearchMode()); + var lavalinkTracks = tracks.Tracks.ToList(); + + if (lavalinkTracks.Count == 0) { await RespondInteraction("Unable to get the tracks"); return; @@ -42,7 +42,7 @@ public async Task PlayPlaylist( StringBuilder text = new(); var idx = 0; - foreach (var track in searchResult.Tracks) + foreach (var track in lavalinkTracks) { var url = Format.Url($"{Format.Bold(Format.Sanitize(track.Title))} by {Format.Bold(track.Author)}", track.Uri?.AbsoluteUri ?? "https://unknown.com"); @@ -59,12 +59,11 @@ public async Task PlayPlaylist( postProcessedTracks.Add(track); } - foreach (var track in postProcessedTracks) await Player.PlayAsync(track); var pages = MusicPages.CreatePagesFromString(text.ToString(), "Queued Playlist", Color.Gold); - + if (!Player.Queue.IsEmpty) { if (Player.State != PlayerState.Playing) diff --git a/backend/Music/Commands/PlayStream.cs b/backend/Music/Commands/PlayStream.cs new file mode 100644 index 000000000..04190f920 --- /dev/null +++ b/backend/Music/Commands/PlayStream.cs @@ -0,0 +1,38 @@ +using Bot.Attributes; +using Discord.Interactions; +using Lavalink4NET.Rest.Entities.Tracks; +using Microsoft.AspNetCore.Components.Routing; +using Music.Abstractions; +using Music.Enums; +using Music.Extensions; +using System.Numerics; + +namespace Music.Commands; + +public class PlayStreamCommand : MusicCommand +{ + [SlashCommand("play-stream", "Play a stream")] + [BotChannel] + public async Task PlayStream( + [Summary("stream-url", "Stream URL")] string streamUrl, + [Summary("source", "Music source")] MusicSource source = MusicSource.Default) + { + if (!Uri.IsWellFormedUriString(streamUrl, UriKind.Absolute)) + { + await RespondInteraction("I need a valid stream URL to function"); + return; + } + + var track = await Audio.Tracks.LoadTrackAsync(streamUrl, source.GetSearchMode()); + + if (track == null) + { + await RespondInteraction($"Unable to get the stream from {streamUrl}"); + return; + } + + await Player.PlayAsync(track); + + await RespondInteraction($"Now streaming from {streamUrl}"); + } +} diff --git a/backend/Music/Enums/MusicSource.cs b/backend/Music/Enums/MusicSource.cs index 359300d57..2a56716b2 100644 --- a/backend/Music/Enums/MusicSource.cs +++ b/backend/Music/Enums/MusicSource.cs @@ -2,7 +2,7 @@ public enum MusicSource { - None, + Default, YouTube, YouTubeMusic, SoundCloud, diff --git a/backend/Music/Extensions/SourceToTrackType.cs b/backend/Music/Extensions/SourceToTrackType.cs index 97bbb11c2..346eef4f5 100644 --- a/backend/Music/Extensions/SourceToTrackType.cs +++ b/backend/Music/Extensions/SourceToTrackType.cs @@ -7,7 +7,7 @@ public static class SourceToTrackType { public static TrackSearchMode GetSearchMode(this MusicSource source) => source switch { - MusicSource.None => TrackSearchMode.None, + MusicSource.Default => TrackSearchMode.None, MusicSource.YouTube => TrackSearchMode.YouTube, MusicSource.YouTubeMusic => TrackSearchMode.YouTubeMusic, MusicSource.SoundCloud => TrackSearchMode.SoundCloud, diff --git a/backend/Music/MusicModule.cs b/backend/Music/MusicModule.cs index 671d73205..8b1465f52 100644 --- a/backend/Music/MusicModule.cs +++ b/backend/Music/MusicModule.cs @@ -32,21 +32,19 @@ public override void AddServices(IServiceCollection services, CachedServices cac .AddSingleton() .AddSingleton() + .AddSingleton() - .AddSingleton(new InactivityTrackingOptions + .ConfigureInactivityTracking(options => { - DefaultPollInterval = TimeSpan.FromMinutes(5), - DefaultTimeout = TimeSpan.FromMinutes(5), - UseDefaultTrackers = true + options.DefaultPollInterval = TimeSpan.FromMinutes(5); + options.DefaultTimeout = TimeSpan.FromMinutes(5); + options.UseDefaultTrackers = true; }) .Configure(config => config.Timeout = TimeSpan.FromSeconds(10)) .AddInactivityTracking() .AddSingleton() - .AddSingleton(x => - new DiscordClientWrapper(x.GetRequiredService())) - .ConfigureLavalink(config => { config.BaseAddress = new Uri($"http://{Host}:{Port}"); diff --git a/backend/Music/Services/MusicService.cs b/backend/Music/Services/MusicService.cs index cded69b1b..c5f999661 100644 --- a/backend/Music/Services/MusicService.cs +++ b/backend/Music/Services/MusicService.cs @@ -22,7 +22,6 @@ public class MusicService(DiscordSocketClient client, IAudioService lavalink, In public void RegisterEvents() { - _client.Ready += SetupLavalink; _client.UserVoiceStateUpdated += CheckLeft; _lavalink.TrackStarted += OnTrackStarted; @@ -105,12 +104,6 @@ private async Task CheckLeft(SocketUser user, SocketVoiceState originalState, So } } - private async Task SetupLavalink() - { - await _lavalink.StartAsync(); - await _inactivityTracker.StartAsync(); - } - public void SetStartTimeAsCurrent(ulong guildId) { lock (StartTimes)