2022-03-19 22:28:54 +01:00

139 lines
4.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DSharpPlus.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TomatenMusic.Util;
using DSharpPlus;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Logging;
namespace TomatenMusic.Prompt.Model
{
abstract class PaginatedButtonPrompt<T> : ButtonPrompt
{
protected PageManager<T> PageManager { get; set; }
protected int CurrentPage { get; set; } = 1;
public string Title { get; set; }
public PaginatedButtonPrompt(string title, List<T> items, DiscordPromptBase lastPrompt = null) : base(lastPrompt)
{
PageManager = new PageManager<T>(items, 9);
Title = title;
for (int i = 0; i < 9; i++)
{
int currentNumber = i + 1;
ButtonPromptOption option = new ButtonPromptOption()
{
Style = DSharpPlus.ButtonStyle.Primary,
Row = i < 5 ? 1 : 2,
UpdateMethod = async (option) =>
{
option.Disabled = PageManager.GetPage(CurrentPage).Count < currentNumber;
return option;
},
Run = async (args, sender, prompt) =>
{
List<T> items = PageManager.GetPage(CurrentPage);
await OnSelect(items[currentNumber-1], args, sender);
}
};
switch (i)
{
case 0:
option.Emoji = new DiscordComponentEmoji("1⃣");
break;
case 1:
option.Emoji = new DiscordComponentEmoji("2⃣");
break;
case 2:
option.Emoji = new DiscordComponentEmoji("3⃣");
break;
case 3:
option.Emoji = new DiscordComponentEmoji("4⃣");
break;
case 4:
option.Emoji = new DiscordComponentEmoji("5⃣");
break;
case 5:
option.Emoji = new DiscordComponentEmoji("6⃣");
break;
case 6:
option.Emoji = new DiscordComponentEmoji("7⃣");
break;
case 7:
option.Emoji = new DiscordComponentEmoji("8⃣");
break;
case 8:
option.Emoji = new DiscordComponentEmoji("9⃣");
break;
}
AddOption(option);
}
AddOption(new ButtonPromptOption
{
Style = ButtonStyle.Secondary,
Emoji= new DiscordComponentEmoji("⬅️"),
Row = 3,
UpdateMethod = async (prompt) =>
{
prompt.Disabled = CurrentPage - 1 == 0;
return prompt;
},
Run = async (args, sender, prompt) =>
{
CurrentPage--;
await UpdateAsync();
}
});
AddOption(new ButtonPromptOption
{
Style = ButtonStyle.Secondary,
Emoji = new DiscordComponentEmoji("➡️"),
Row = 3,
UpdateMethod = async (prompt) =>
{
prompt.Disabled = PageManager.GetTotalPages() == CurrentPage;
return prompt;
},
Run = async (args, sender, prompt) =>
{
CurrentPage++;
await UpdateAsync();
}
});
}
public abstract Task OnSelect(T item, ComponentInteractionCreateEventArgs args, DiscordClient sender);
protected int GetTotalPages()
{
return PageManager.GetTotalPages();
}
protected async override Task<DiscordMessageBuilder> GetMessageAsync()
{
DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
.WithTitle(Title)
.WithFooter($"Page {CurrentPage} of {GetTotalPages()}")
.WithDescription("Select your desired Tracks");
return PopulateMessage(builder);
}
protected abstract DiscordMessageBuilder PopulateMessage(DiscordEmbedBuilder builder);
}
}