TomatenMusic-V2/TomatenMusicCore/Prompt/Implementation/SongSelectorPrompt.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2022-03-29 22:12:22 +02:00
using System;
using System.Collections.Generic;
using System.Text;
using TomatenMusic.Prompt.Model;
using DSharpPlus;
using System.Threading.Tasks;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Logging;
using DSharpPlus.Entities;
using TomatenMusic.Util;
using TomatenMusic.Music.Entitites;
using TomatenMusic.Music;
using System.Linq;
using Lavalink4NET.Player;
using TomatenMusicCore.Music;
using TomatenMusicCore.Music.Entities;
using TomatenMusic.Prompt.Option;
namespace TomatenMusic.Prompt.Implementation
{
sealed class SongSelectorPrompt : PaginatedSelectPrompt<LavalinkTrack>
{
public bool IsConfirmed { get; set; }
public Func<TrackList, Task> ConfirmCallback { get; set; } = (tracks) =>
{
return Task.CompletedTask;
};
public IEnumerable<LavalinkTrack> Tracks { get; private set; }
public SongSelectorPrompt(string title, IEnumerable<LavalinkTrack> tracks, DiscordPromptBase lastPrompt = null, List<DiscordEmbed> embeds = null) : base(title, tracks.ToList(), lastPrompt, embeds)
{
Title = title;
Tracks = tracks;
AddOption(new ButtonPromptOption
{
Emoji = new DiscordComponentEmoji("✔️"),
Row = 3,
Style = ButtonStyle.Success,
Run = async (args, client, option) =>
{
if (SelectedItems.Count == 0)
{
await args.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent("Please Select a Song!").AsEphemeral(true));
return;
}
IsConfirmed = true;
_ = ConfirmCallback.Invoke(new TrackList(SelectedItems));
}
});
}
public override Task<PaginatedSelectMenuOption<LavalinkTrack>> ConvertToOption(LavalinkTrack item)
{
return Task.FromResult<PaginatedSelectMenuOption<LavalinkTrack>>(new PaginatedSelectMenuOption<LavalinkTrack>
{
Label = item.Title,
Description = item.Author
});
}
public override Task OnSelect(LavalinkTrack item, ComponentInteractionCreateEventArgs args, DiscordClient sender)
{
_logger.LogDebug($"Added {item.Title}, {SelectedItems}");
return Task.CompletedTask;
}
public override Task OnUnselect(LavalinkTrack item, ComponentInteractionCreateEventArgs args, DiscordClient sender)
{
_logger.LogDebug($"Removed {item.Title}");
return Task.CompletedTask;
}
public async Task<List<LavalinkTrack>> AwaitSelectionAsync()
{
return await Task.Run(() =>
{
while (!IsConfirmed)
{
if (State == PromptState.INVALID)
throw new InvalidOperationException("Prompt has been Invalidated");
}
IsConfirmed = false;
return SelectedItems;
});
}
protected override DiscordMessageBuilder PopulateMessage(DiscordEmbedBuilder builder)
{
builder.WithTitle(Title);
builder.WithDescription(Common.TrackListString(PageManager.GetPage(CurrentPage), 4000));
List<DiscordEmbed> embeds = new List<DiscordEmbed>();
embeds.Add(builder.Build());
if (Embeds != null)
embeds.AddRange(Embeds);
return new DiscordMessageBuilder().AddEmbeds(embeds);
}
}
}