Added Marmalades module

This commit is contained in:
Toastie 2024-09-20 23:46:42 +12:00
parent 5505052af4
commit 03ab232251
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
4 changed files with 324 additions and 0 deletions

View file

@ -0,0 +1,6 @@
namespace EllieBot.Modules;
public interface IMarmaladesRepositoryService
{
Task<List<ModuleItem>> GetModuleItemsAsync();
}

View file

@ -0,0 +1,243 @@
using Ellie.Common.Marmalade;
namespace EllieBot.Modules;
[OwnerOnly]
[NoPublicBot]
public partial class Marmalade : EllieModule<IMarmaladeLoaderService>
{
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();
}
}

View file

@ -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; }
}

View file

@ -0,0 +1,67 @@
namespace EllieBot.Modules;
public class MarmaladesRepositoryService : IMarmaladesRepositoryService, IEService
{
public async Task<List<ModuleItem>> 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"
}
];
}
}