Skip to content

Commit

Permalink
Don't setup lavalink on ready as this is done automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxFoxxo committed Dec 11, 2023
1 parent 0d81036 commit 61723aa
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 43 deletions.
18 changes: 5 additions & 13 deletions backend/Music/Commands/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,14 +18,14 @@ public class PlayCommand : MusicCommand<PlayCommand>
[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)
{
Expand All @@ -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();

Expand Down
27 changes: 13 additions & 14 deletions backend/Music/Commands/PlayPlaylist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,16 +19,19 @@ public class PlaylistCommand : MusicCommand<PlaylistCommand>
[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;
Expand All @@ -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");
Expand All @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions backend/Music/Commands/PlayStream.cs
Original file line number Diff line number Diff line change
@@ -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<PlayStreamCommand>
{
[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}");
}
}
2 changes: 1 addition & 1 deletion backend/Music/Enums/MusicSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum MusicSource
{
None,
Default,
YouTube,
YouTubeMusic,
SoundCloud,
Expand Down
2 changes: 1 addition & 1 deletion backend/Music/Extensions/SourceToTrackType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions backend/Music/MusicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ public override void AddServices(IServiceCollection services, CachedServices cac

.AddSingleton<LyricsOptions>()
.AddSingleton<LyricsService>()

.AddSingleton<ArtworkService>()

.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<IdleInactivityTrackerOptions>(config => config.Timeout = TimeSpan.FromSeconds(10))
.AddInactivityTracking()
.AddSingleton<InactivityTrackingService>()

.AddSingleton<IDiscordClientWrapper, DiscordClientWrapper>(x =>
new DiscordClientWrapper(x.GetRequiredService<DiscordSocketClient>()))

.ConfigureLavalink(config =>
{
config.BaseAddress = new Uri($"http://{Host}:{Port}");
Expand Down
7 changes: 0 additions & 7 deletions backend/Music/Services/MusicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class MusicService(DiscordSocketClient client, IAudioService lavalink, In

public void RegisterEvents()
{
_client.Ready += SetupLavalink;
_client.UserVoiceStateUpdated += CheckLeft;

_lavalink.TrackStarted += OnTrackStarted;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 61723aa

Please sign in to comment.