Skip to content

Commit 99b72be

Browse files
committed
Fix playlist issue
1 parent a7a4397 commit 99b72be

9 files changed

Lines changed: 82 additions & 33 deletions

File tree

src/Application/DTOs/PlayRequest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public abstract class PlayRequest
88
public Func<string, Task> Callbacks { get; set; } = null!;
99

1010
public abstract object ContextAsObject { get; }
11+
public string? VideoTitle { get; set; }
12+
public string? VideoUrl { get; set; }
1113
}
1214

1315
public class PlayRequest<TContext> : PlayRequest

src/Domain/Common/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public static class Constants
77
public static class CustomIds
88
{
99
public const string Play = nameof(EventType.Play);
10+
public const string PlayListPlay = nameof(EventType.PlayListPlay);
1011
public const string Skip = nameof(EventType.Skip);
1112
public const string Stop = nameof(EventType.Stop);
1213
}
@@ -15,6 +16,7 @@ public static class CustomIds
1516
public class EventType
1617
{
1718
public record Play : IEvent;
19+
public record PlayListPlay : IEvent;
1820
public record Stop : IEvent;
1921
public record Skip : IEvent;
2022
}

src/Infrastructure/Commands/CommandUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public static class CommandUtils
1818
};
1919
}
2020

21-
internal static IEnumerable<IMessageComponentProperties> CreateComponent<T>(T source)
21+
internal static IEnumerable<IMessageComponentProperties> CreateComponent<T>(T source, string id = Constants.CustomIds.Play)
2222
where T : IEnumerable<ComponentModel>
2323
{
2424
return
2525
[
26-
new StringMenuProperties(Constants.CustomIds.Play)
26+
new StringMenuProperties(id)
2727
{
2828
Options = source.Select(s => new StringMenuSelectOptionProperties(s.Title, s.Url)
2929
{

src/Infrastructure/Commands/MusicActionCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ await executor.ExecuteAsync(async serviceProvider =>
5858
var songs = queue.GetAllRequests().Select(async r =>
5959
{
6060
var title = await youtubeService.GetVideoTitleAsync(
61-
(r.ContextAsObject as StringMenuInteractionContext)?.SelectedValues[0],
61+
r.VideoUrl ?? (r.ContextAsObject as StringMenuInteractionContext)?.SelectedValues[0]!,
6262
CancellationToken.None);
6363
return title;
6464
}
65-
).ToList();
65+
).Take(20).ToList();
6666

6767
var titles = await Task.WhenAll(songs);
6868

src/Infrastructure/Commands/MusicPlayCommands.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Application.Interfaces.Services;
2+
using Domain.Common;
23
using Microsoft.Extensions.DependencyInjection;
34
using NetCord.Rest;
45
using NetCord.Services.ApplicationCommands;
@@ -86,11 +87,12 @@ await executor.ExecuteAsync(async serviceProvider =>
8687
try
8788
{
8889
var playlist = await youtubeClient.Playlists.GetAsync(playlistUrl);
89-
var videos = await youtubeClient.Playlists.GetVideosAsync(playlist.Id);
90-
90+
9191
message.Components =
92-
CommandUtils.CreateComponent(videos.Select(s =>
93-
new CommandUtils.ComponentModel(s.Title, s.Url, s.Author.ChannelTitle)));
92+
CommandUtils.CreateComponent(new List<CommandUtils.ComponentModel>
93+
{
94+
new(playlist.Title, playlist.Id, "Playlist")
95+
}, Constants.CustomIds.PlayListPlay);
9496

9597
await RespondAsync(InteractionCallback.Message(message));
9698
}

src/Infrastructure/Interaction/NetCordInteraction.cs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,25 @@
77
using Microsoft.Extensions.Logging;
88
using NetCord.Rest;
99
using NetCord.Services.ComponentInteractions;
10+
using YoutubeExplode;
11+
using YoutubeExplode.Common;
1012

1113
namespace Infrastructure.Interaction;
1214

1315
public class NetCordInteraction(
1416
ILogger<NetCordInteraction> logger,
1517
IEventDispatcher eventDispatcher,
1618
IMusicQueueService queueService,
19+
YoutubeClient youtubeClient,
1720
[FromKeyedServices(nameof(YoutubeService))] IStreamService youtubeService) : ComponentInteractionModule<StringMenuInteractionContext>
1821
{
1922
[ComponentInteraction(Constants.CustomIds.Play)]
2023
public async Task<string> Play()
2124
{
22-
if (!CheckMessageExpiration())
23-
{
24-
return "This interaction has expired. Please use the play command again.";
25-
}
26-
27-
if (!NotInVoiceChannel())
28-
{
29-
return "You must be in a voice channel to use this command.";
30-
}
31-
32-
if (!NotDeafened())
25+
var error = EvaluateChecking();
26+
if (error != null)
3327
{
34-
return "You must be deafened to use this command.";
28+
return error;
3529
}
3630

3731
logger.LogInformation("Play command invoked by user {UserId} in guild {GuildId}", Context.User.Id,
@@ -58,7 +52,57 @@ public async Task<string> Play()
5852
}
5953

6054
return message;
55+
}
56+
57+
[ComponentInteraction(Constants.CustomIds.PlayListPlay)]
58+
public async Task<string> PlayPlaylist()
59+
{
60+
var error = EvaluateChecking();
61+
if (error != null)
62+
{
63+
return error;
64+
}
65+
66+
logger.LogInformation("Play command invoked by user {UserId} in guild {GuildId}", Context.User.Id,
67+
Context.Guild?.Id);
6168

69+
var videos = await youtubeClient.Playlists.GetVideosAsync(Context.SelectedValues[0]);
70+
71+
foreach (var video in videos)
72+
{
73+
var playRequest = new PlayRequest<StringMenuInteractionContext>
74+
{
75+
Context = Context,
76+
Callbacks = async message => await RespondAsyncCallback(message),
77+
VideoTitle = video.Title,
78+
VideoUrl = video.Url
79+
};
80+
queueService.Enqueue(playRequest);
81+
}
82+
83+
eventDispatcher.Dispatch(new EventType.Play());
84+
85+
return "Playlist Added to the queue!";
86+
}
87+
88+
private string? EvaluateChecking()
89+
{
90+
if (!CheckMessageExpiration())
91+
{
92+
return "This interaction has expired. Please use the play command again.";
93+
}
94+
95+
if (!NotInVoiceChannel())
96+
{
97+
return "You must be in a voice channel to use this command.";
98+
}
99+
100+
if (!NotDeafened())
101+
{
102+
return "You must be deafened to use this command.";
103+
}
104+
105+
return null;
62106
}
63107

64108
private bool NotInVoiceChannel()

src/Infrastructure/Services/NetCordAudioPlayerService.cs renamed to src/Infrastructure/Services/AudioPlayerService.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
namespace Infrastructure.Services;
1414

15-
public class NetCordAudioPlayerService(
15+
public class AudioPlayerService(
1616
INativePlaceMusicProcessorService ffmpegProcessService,
1717
IServiceProvider serviceProvider,
18-
ILogger<NetCordAudioPlayerService> logger,
18+
ILogger<AudioPlayerService> logger,
1919
PlayerState<VoiceClient> playerState,
2020
IMusicQueueService queue) : INetCordAudioPlayerService
2121
{
@@ -85,8 +85,8 @@ async Task HandleVoiceStream()
8585
var radioSourceService = scope.ServiceProvider
8686
.GetRequiredService<IRadioSourceService>();
8787

88-
var selectedValue = _currentTrack.Context.SelectedValues[0];
89-
var sourceUrl = await GetSourceUrl(selectedValue, radioSourceService, youTubeService, _currentTrack);
88+
var selectedValue = _currentTrack.VideoUrl ?? _currentTrack.Context.SelectedValues[0];
89+
var sourceUrl = await GetSourceUrl(selectedValue, radioSourceService, youTubeService, _currentTrack.Context.User.Id, _currentTrack.Context.User.Username, _currentTrack.Context.User.GlobalName);
9090

9191
await StartFfmpegStream(sourceUrl, stream);
9292
}
@@ -160,7 +160,7 @@ await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream,
160160
}
161161

162162
private async Task<string> GetSourceUrl(string selectedValue, IRadioSourceService radioSourceService,
163-
IStreamService youTubeService, PlayRequest<StringMenuInteractionContext> currentTrack)
163+
IStreamService youTubeService, ulong userId, string userName, string? globalName)
164164
{
165165
string sourceUrl;
166166

@@ -186,22 +186,21 @@ private async Task<string> GetSourceUrl(string selectedValue, IRadioSourceServic
186186
Url = selectedValue,
187187
Title = await youTubeService.GetVideoTitleAsync(selectedValue,
188188
playerState.SkipCts.Token),
189-
UserId = currentTrack.Context.User.Id
189+
UserId = userId
190190
};
191-
ffmpegProcessService.OnProcessStart += () => HandleOnProcessStartAsync(song, currentTrack.Context);
191+
ffmpegProcessService.OnProcessStart += () => HandleOnProcessStartAsync(song, userId, userName, globalName);
192192
}
193193

194194
return sourceUrl;
195195
}
196196

197-
private async Task HandleOnProcessStartAsync(SongDtoBase song, StringMenuInteractionContext currentContext)
197+
private async Task HandleOnProcessStartAsync(SongDtoBase song, ulong userId, string userName,string? globalName)
198198
{
199199
using var scope = serviceProvider.CreateScope();
200200
var statisticsService = scope.ServiceProvider
201201
.GetRequiredService<IStatisticsService>();
202202
await statisticsService
203-
.LogSongPlayAsync(currentContext.User.Id, currentContext.User.Username,
204-
currentContext.User.GlobalName ?? string.Empty, song)
203+
.LogSongPlayAsync(userId, userName, globalName ?? string.Empty, song)
205204
.ConfigureAwait(false);
206205

207206
playerState.CurrentAction = PlayerAction.Play;

src/Infrastructure/Services/NetCordPlayerHandler.cs renamed to src/Infrastructure/Services/PlayerHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace Infrastructure.Services;
99

10-
public class NetCordPlayerHandler(
10+
public class PlayerHandler(
1111
IMusicQueueService queue,
12-
ILogger<NetCordPlayerHandler> logger,
12+
ILogger<PlayerHandler> logger,
1313
INetCordAudioPlayerService playerService,
1414
PlayerState<VoiceClient> playerState)
1515
: IEventHandler<EventType.Play>, IEventHandler<EventType.Stop>, IEventHandler<EventType.Skip>

src/Worker/DependencyInjection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void AddDiscordServices(this IServiceCollection services, IConfigu
4949
services.AddSingleton<PlayerState<VoiceClient>>();
5050
services.AddSingleton<IHttpRequestService, HttpRequestService>();
5151
services.AddSingleton<INativePlaceMusicProcessorService, FfmpegProcessService>();
52-
services.AddSingleton<INetCordAudioPlayerService, NetCordAudioPlayerService>();
52+
services.AddSingleton<INetCordAudioPlayerService, AudioPlayerService>();
5353
services.AddSingleton<IMusicQueueService, MusicQueueService>();
5454
services.AddSingleton<IScopeExecutor, ScopeExecutor>();
5555

0 commit comments

Comments
 (0)