From 243d754d10435ffb78ffcb03570888e8d918ab92 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 18 Jun 2024 23:51:49 +1200 Subject: [PATCH] Added Music module --- .../Marmalade/IMarmaladesRepositoryService.cs | 6 + src/EllieBot/Modules/Marmalade/Marmalade.cs | 243 ++++++++++++++++++ .../Modules/Marmalade/MarmaladeItem.cs | 8 + .../Marmalade/MarmaladesRepositoryService.cs | 67 +++++ 4 files changed, 324 insertions(+) create mode 100644 src/EllieBot/Modules/Marmalade/IMarmaladesRepositoryService.cs create mode 100644 src/EllieBot/Modules/Marmalade/Marmalade.cs create mode 100644 src/EllieBot/Modules/Marmalade/MarmaladeItem.cs create mode 100644 src/EllieBot/Modules/Marmalade/MarmaladesRepositoryService.cs diff --git a/src/EllieBot/Modules/Marmalade/IMarmaladesRepositoryService.cs b/src/EllieBot/Modules/Marmalade/IMarmaladesRepositoryService.cs new file mode 100644 index 0000000..155f56b --- /dev/null +++ b/src/EllieBot/Modules/Marmalade/IMarmaladesRepositoryService.cs @@ -0,0 +1,6 @@ +namespace EllieBot.Modules; + +public interface IMarmaladesRepositoryService +{ + Task> GetModuleItemsAsync(); +} \ No newline at end of file diff --git a/src/EllieBot/Modules/Marmalade/Marmalade.cs b/src/EllieBot/Modules/Marmalade/Marmalade.cs new file mode 100644 index 0000000..530b23f --- /dev/null +++ b/src/EllieBot/Modules/Marmalade/Marmalade.cs @@ -0,0 +1,243 @@ +using Ellie.Common.Marmalade; + +namespace EllieBot.Modules; + +[OwnerOnly] +[NoPublicBot] +public partial class Marmalade : EllieModule +{ + private readonly IMarmaladesRepositoryService _repo; + + public Marmalade(IMarmaladesRepositoryService repo) + { + _repo = repo; + } + + [Cmd] + [OwnerOnly] + public async Task MarmaladeLoad(string? name = null) + { + if (string.IsNullOrWhiteSpace(name)) + { + var loaded = _service.GetLoadedMarmalades() + .Select(x => x.Name) + .ToHashSet(); + + var unloaded = _service.GetAllMarmalades() + .Where(x => !loaded.Contains(x)) + .Select(x => Format.Code(x.ToString())) + .ToArray(); + + if (unloaded.Length == 0) + { + await Response().Pending(strs.no_marmalade_available).SendAsync(); + return; + } + + await Response() + .Paginated() + .Items(unloaded) + .PageSize(10) + .Page((items, _) => + { + return _sender.CreateEmbed() + .WithOkColor() + .WithTitle(GetText(strs.list_of_unloaded)) + .WithDescription(items.Join('\n')); + }) + .SendAsync(); + return; + } + + var res = await _service.LoadMarmaladeAsync(name); + if (res == MarmaladeLoadResult.Success) + await Response().Confirm(strs.marmalade_loaded(Format.Code(name))).SendAsync(); + else + { + var locStr = res switch + { + MarmaladeLoadResult.Empty => strs.marmalade_empty, + MarmaladeLoadResult.AlreadyLoaded => strs.marmalade_already_loaded(Format.Code(name)), + MarmaladeLoadResult.NotFound => strs.marmalade_invalid_not_found, + MarmaladeLoadResult.UnknownError => strs.error_occured, + _ => strs.error_occured + }; + + await Response().Error(locStr).SendAsync(); + } + } + + [Cmd] + [OwnerOnly] + public async Task MarmaladeUnload(string? name = null) + { + if (string.IsNullOrWhiteSpace(name)) + { + var loaded = _service.GetLoadedMarmalades(); + if (loaded.Count == 0) + { + await Response().Pending(strs.no_marmalade_loaded).SendAsync(); + return; + } + + await Response() + .Embed(_sender.CreateEmbed() + .WithOkColor() + .WithTitle(GetText(strs.loaded_marmalades)) + .WithDescription(loaded.Select(x => x.Name) + .Join("\n"))) + .SendAsync(); + + return; + } + + var res = await _service.UnloadMarmaladeAsync(name); + if (res == MarmaladeUnloadResult.Success) + await Response().Confirm(strs.marmalade_unloaded(Format.Code(name))).SendAsync(); + else + { + var locStr = res switch + { + MarmaladeUnloadResult.NotLoaded => strs.marmalade_not_loaded, + MarmaladeUnloadResult.PossiblyUnable => strs.marmalade_possibly_cant_unload, + _ => strs.error_occured + }; + + await Response().Error(locStr).SendAsync(); + } + } + + [Cmd] + [OwnerOnly] + public async Task MarmaladeList() + { + var all = _service.GetAllMarmalades(); + + if (all.Count == 0) + { + await Response().Pending(strs.no_marmalade_available).SendAsync(); + return; + } + + var loaded = _service.GetLoadedMarmalades() + .Select(x => x.Name) + .ToHashSet(); + + var output = all + .Select(m => + { + var emoji = loaded.Contains(m) ? "`✅`" : "`🔴`"; + return $"{emoji} `{m}`"; + }) + .ToArray(); + + + await Response() + .Paginated() + .Items(output) + .PageSize(10) + .Page((items, _) => _sender.CreateEmbed() + .WithOkColor() + .WithTitle(GetText(strs.list_of_marmalades)) + .WithDescription(items.Join('\n'))) + .SendAsync(); + } + + [Cmd] + [OwnerOnly] + public async Task MarmaladeInfo(string? name = null) + { + var marmalades = _service.GetLoadedMarmalades(); + + if (name is not null) + { + var found = marmalades.FirstOrDefault(x => string.Equals(x.Name, + name, + StringComparison.InvariantCultureIgnoreCase)); + + if (found is null) + { + await Response().Error(strs.marmalade_name_not_found).SendAsync(); + return; + } + + var cmdCount = found.Canaries.Sum(x => x.Commands.Count); + var cmdNames = found.Canaries + .SelectMany(x => Format.Code(string.IsNullOrWhiteSpace(x.Prefix) + ? x.Name + : $"{x.Prefix} {x.Name}")) + .Join("\n"); + + var eb = _sender.CreateEmbed() + .WithOkColor() + .WithAuthor(GetText(strs.marmalade_info)) + .WithTitle(found.Name) + .WithDescription(found.Description) + .AddField(GetText(strs.canaries_count(found.Canaries.Count)), + found.Canaries.Count == 0 + ? "-" + : found.Canaries.Select(x => x.Name).Join('\n'), + true) + .AddField(GetText(strs.commands_count(cmdCount)), + string.IsNullOrWhiteSpace(cmdNames) + ? "-" + : cmdNames, + true); + + await Response().Embed(eb).SendAsync(); + return; + } + + if (marmalades.Count == 0) + { + await Response().Pending(strs.no_marmalade_loaded).SendAsync(); + return; + } + + await Response() + .Paginated() + .Items(marmalades) + .PageSize(9) + .CurrentPage(0) + .Page((items, _) => + { + var eb = _sender.CreateEmbed() + .WithOkColor(); + + foreach (var marmalade in items) + { + eb.AddField(marmalade.Name, + $""" + `Canaries:` {marmalade.Canaries.Count} + `Commands:` {marmalade.Canaries.Sum(x => x.Commands.Count)} + -- + {marmalade.Description} + """); + } + + return eb; + }) + .SendAsync(); + } + + [Cmd] + [OwnerOnly] + public async Task MarmaladeSearch() + { + var eb = _sender.CreateEmbed() + .WithTitle(GetText(strs.list_of_marmalades)) + .WithOkColor(); + + foreach (var item in await _repo.GetModuleItemsAsync()) + { + eb.AddField(item.Name, + $""" + {item.Description} + `{item.Command}` + """, + true); + } + + await Response().Embed(eb).SendAsync(); + } +} \ No newline at end of file diff --git a/src/EllieBot/Modules/Marmalade/MarmaladeItem.cs b/src/EllieBot/Modules/Marmalade/MarmaladeItem.cs new file mode 100644 index 0000000..54c2889 --- /dev/null +++ b/src/EllieBot/Modules/Marmalade/MarmaladeItem.cs @@ -0,0 +1,8 @@ +namespace EllieBot.Modules; + +public sealed class ModuleItem +{ + public required string Name { get; init; } + public required string Description { get; init; } + public required string Command { get; init; } +} \ No newline at end of file diff --git a/src/EllieBot/Modules/Marmalade/MarmaladesRepositoryService.cs b/src/EllieBot/Modules/Marmalade/MarmaladesRepositoryService.cs new file mode 100644 index 0000000..6cc12b5 --- /dev/null +++ b/src/EllieBot/Modules/Marmalade/MarmaladesRepositoryService.cs @@ -0,0 +1,67 @@ +namespace EllieBot.Modules; + +public class MarmaladesRepositoryService : IMarmaladesRepositoryService, IEService +{ + public async Task> GetModuleItemsAsync() + { + // Simulate retrieving data from a database or API + await Task.Delay(100); + return + [ + new() + { + Name = "RSS Reader", + Description = "Keep up to date with your favorite websites", + Command = ".mainstall rss" + }, + new() + { + Name = "Password Manager", + Description = "Safely store and manage all your passwords", + Command = ".mainstall passwordmanager" + }, + new() + { + Name = "Browser Extension", + Description = "Enhance your browsing experience with useful tools", + Command = ".mainstall browserextension" + }, + new() + { + Name = "Video Downloader", + Description = "Download videos from popular websites", + Command = ".mainstall videodownloader" + }, + new() + { + Name = "Virtual Private Network", + Description = "Securely browse the web and protect your privacy", + Command = ".mainstall vpn" + }, + new() + { + Name = "Ad Blocker", + Description = "Block annoying ads and improve page load times", + Command = ".mainstall adblocker" + }, + new() + { + Name = "Cloud Storage", + Description = "Store and share your files online", + Command = ".mainstall cloudstorage" + }, + new() + { + Name = "Social Media Manager", + Description = "Manage all your social media accounts in one place", + Command = ".mainstall socialmediamanager" + }, + new() + { + Name = "Code Editor", + Description = "Write and edit code online", + Command = ".mainstall codeeditor" + } + ]; + } +} \ No newline at end of file