Skip to content

Commit

Permalink
switched to native lavalink yt search
Browse files Browse the repository at this point in the history
  • Loading branch information
Pannoniae committed Dec 31, 2024
1 parent ac8d98b commit c8ee902
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 57 deletions.
139 changes: 83 additions & 56 deletions CommandHandlers/MusicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public async Task PlayAsync(CommandContext ctx,

if (trackLoad.LoadType == LavalinkLoadResultType.Playlist) {
var playlist = (LavalinkPlaylist)result;
tracks = playlist.Tracks;
if (playlist.Info.SelectedTrack > 0) {
var index = playlist.Info.SelectedTrack;
tracks = tracks.Skip(index).Concat(tracks.Take(index)).ToList();
Expand Down Expand Up @@ -312,6 +313,7 @@ await common.respond(ctx,
elInd = -1;
}
else {
await common.modify(ctx, msg, "Invalid choice was made.");
return;
}
}
Expand Down Expand Up @@ -488,22 +490,82 @@ public async Task PlayAsync(CommandContext ctx,
string term) {
var interactivity = ctx.Client.GetInteractivity();

var results = (await YouTube.SearchAsync(term)).ToList();
if (!results.Any()) {
var r = await Music.SearchTracksAsync(term);
if (r.LoadType != LavalinkLoadResultType.Search) {
await common.respond(ctx, "No tracks were found at specified link.");
return;
}
var results = (List<LavalinkTrack>)r.Result;
if (results.Count == 0) {
await common.respond(ctx, "Nothing was found.");
return;
}

var msgC = string.Join("\n",
results.Select((x, i) =>
$"{MusicCommon.NumberMappings[i + 1]} {WebUtility.HtmlDecode(x.Title).Sanitize().Bold().URLDecode()} by {WebUtility.HtmlDecode(x.Author).Sanitize().Bold().URLDecode()}"));
msgC =
$"{msgC}\n\nType a number 1-{results.Count} to queue a track. To cancel, type cancel or {MusicCommon.NumberMappingsReverse.Last()}.";
if (results.Count == 0) {
await common.respond(ctx, "Nothing was found.");
return;
}

LavalinkTrack? track;
object? track_;
if (results.Count == 1) {
// only one result
var el_ = results.First();
track_ = el_;
if (track_ == null) {
await common.respond(ctx, "No tracks were found at specified link.");
return;
}

track = (LavalinkTrack)track_;


GuildMusic.queue.Enqueue(track);


await startPlayer(ctx);
await GuildMusic.queue.PlayAsync();

/*if (trackCount_ > 1) {
await common.respond(ctx, $"Added {trackCount_:#,##0} tracks to playback queue.");
}
else {
var track = tracks_.First();*/
await common.respond(ctx,
$"Added {track.ToLimitedTrackString()} to the playback queue.");
return;
}

var pageCount = results.Count / 10 + 1;
if (results.Count % 10 == 0) {
pageCount--;
}

var content = results.Select((x, i) => (x, i))
.GroupBy(e => e.i / 10)
.Select(xg => new Page(
$"{string.Join("\n", xg.Select(xa => $"`{xa.i + 1}` {xa.x.ToLimitedTrackString()}"))}\n\nPage {xg.Key + 1}/{pageCount}"));

Task task = null;
if (pageCount == 1) {
await ctx.Channel.SendMessageAsync(content.First().Content);
}
else {
task = interactivity.SendPaginatedMessageAsync(ctx.Channel, ctx.User, content, TimeSpan.FromMinutes(2),
PaginationBehaviour.Ignore,
ButtonPaginationBehavior.Ignore);
}
task = task;

var msgC =
$"Type a number 1-{results.Count} to queue a track. To cancel, type cancel or {MusicCommon.NumberMappingsReverse.Last()}.";

var msg = await ctx.RespondAsync(msgC);

var res = await interactivity.WaitForMessageAsync(x => x.Author == ctx.User, TimeSpan.FromSeconds(30));
var res = await interactivity.WaitForMessageAsync(x => x.Author == ctx.User && x.Channel == ctx.Channel,
TimeSpan.FromMinutes(2));
if (res.TimedOut || res.Result == null) {
await common.modify(ctx, msg, "No choice was made.");
await msg.ModifyAsync($"{Program.cube} No choice was made.");
return;
}

Expand All @@ -513,15 +575,12 @@ public async Task PlayAsync(CommandContext ctx,
elInd = -1;
}
else {
await common.modify(ctx, msg, "Invalid choice was made.");
return;
}
}
else if (elInd < 1) {
await common.modify(ctx, msg, "Invalid choice was made.");
return;
}

if (!MusicCommon.NumberMappings.ContainsKey(elInd)) {
else if (elInd < 0 || elInd > results.Count) {
await common.modify(ctx, msg, "Invalid choice was made.");
return;
}
Expand All @@ -532,55 +591,23 @@ public async Task PlayAsync(CommandContext ctx,
}

var el = results.ElementAt(elInd - 1);
var url = new Uri($"https://youtu.be/{el.Id}");
track_ = el;

var trackLoad = await Music.GetTracksAsync(url);
var result = trackLoad.Result;
List<LavalinkTrack> tracks = [];
switch (trackLoad.LoadType) {
case LavalinkLoadResultType.Error:
await common.respond(ctx, "No tracks were found at specified link.");
return;
case LavalinkLoadResultType.Playlist: {
var playlist = (LavalinkPlaylist)result;
if (playlist.Info.SelectedTrack > 0) {
var index = playlist.Info.SelectedTrack;
tracks = tracks.Skip(index).Concat(tracks.Take(index)).ToList();
}
break;
}
case LavalinkLoadResultType.Search: {
var search = (List<LavalinkTrack>)result;
tracks = search;
break;
}
case LavalinkLoadResultType.Track: {
var search = (LavalinkTrack)result;
tracks = [search];
break;
}
case LavalinkLoadResultType.Empty:
break;
default:
throw new ArgumentOutOfRangeException();
}

var trackCount = tracks.Count;
foreach (var track in tracks) {
GuildMusic.queue.Enqueue(track);
if (track_ == null) {
await common.modify(ctx, msg, "No tracks were found at specified link.");
return;
}

track = el;
GuildMusic.queue.Enqueue(track);

await startPlayer(ctx);
await GuildMusic.queue.PlayAsync();

if (trackCount > 1) {
await common.modify(ctx, msg, $"Added {trackCount:#,##0} tracks to playback queue.");
}
else {
var track = tracks.First();
await common.modify(ctx, msg,
$"Added {track.ToLimitedTrackString()} to the playback queue.");
}
var track2 = results.First();
await common.modify(ctx, msg,
$"Added {track2.ToLimitedTrackString()} to the playback queue.");
}

[Command("artist"), Description("Plays tracks from an matchedArtist."), Aliases("a")]
Expand Down
6 changes: 6 additions & 0 deletions EconomyBot.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIPeerMessageHandler_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F249ecc2b7056f1ea4e038a5b2117c862125ae77dad7931b8a5321819f57bd5c_003FIPeerMessageHandler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkFilterBand_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fb443a5a236a64937b0c7d2a01391ebda22200_003Fac_003Fd308f0b7_003FLavalinkFilterBand_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkGuildPlayer_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F4bc6a892ecf926bc53c372f61c9f68c707f37dc954a9dcf548a7390c61f7668_003FLavalinkGuildPlayer_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkLoadResultType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F14fa7ede26feb6321a1ce2d33fb7f6fc207c7865ba58c6bc6f401db1b3463dc_003FLavalinkLoadResultType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkPlaylistInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F102c3742dd6e24dd949cc3f8bf99dc36ac8a8bc9cbf38bb19d1ad43a234ca3f_003FLavalinkPlaylistInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkRestClient_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe63f1cd322c5d2329539376aeb11447bc02747caa2ced559dab4869cb53d995_003FLavalinkRestClient_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkSessionConfiguration_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F768fbb1e4ffe3afadb79ac69a88bb584d3258c9d0995d45d90aa285baf2f6f_003FLavalinkSessionConfiguration_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkSession_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F7034ee14b7715ecdefd17e4cd1a5b9fabab8628daadff9f128b7917559ebf_003FLavalinkSession_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALavalinkTrackLoadingResult_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F65ea7cd168bdb3b51346915f738b6c7f38882595b5662a74f740e95877693d_003FLavalinkTrackLoadingResult_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMessageReaderExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fa0728b2d46c1fd279cf4b15743111c47606e5db726da47ebd71718c9e0aeae_003FMessageReaderExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANonterminalReal_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F704435e1714fc3b2c85816a24834ebde113600_003F61_003F3c03c0ea_003FNonterminalReal_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANonterminal_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F704435e1714fc3b2c85816a24834ebde113600_003F13_003F41325beb_003FNonterminal_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
Expand Down
4 changes: 4 additions & 0 deletions Music/MusicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DisCatSharp.Entities;
using DisCatSharp.Lavalink;
using DisCatSharp.Lavalink.Entities;
using DisCatSharp.Lavalink.Enums;
using DisCatSharp.Lavalink.EventArgs;
using Soulseek;
using File = Soulseek.File;
Expand Down Expand Up @@ -110,6 +111,9 @@ public Task<LavalinkTrackLoadingResult> GetTracksAsync(Uri uri)

public Task<LavalinkTrackLoadingResult> GetTracksAsync(string search)
=> node.LoadTracksAsync(search);

public Task<LavalinkTrackLoadingResult> SearchTracksAsync(string search)
=> node.LoadTracksAsync(LavalinkSearchType.Youtube, search);
}

public record SLSKResult(SearchResponse response, File file) {
Expand Down
2 changes: 1 addition & 1 deletion application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lavalink:
gc-warnings: true
plugins:
# Replace VERSION with the current version as shown by the Releases tab or a long commit hash for snapshots.
- dependency: "dev.lavalink.youtube:youtube-plugin:1.11.2"
- dependency: "dev.lavalink.youtube:youtube-plugin:1.11.3"
snapshot: false # Set to true if you want to use a snapshot version.

plugins:
Expand Down

0 comments on commit c8ee902

Please sign in to comment.