This repository has been archived on 2024-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
elliebot/src/EllieBot/Modules/Marmalades/Marmalade.cs

243 lines
No EOL
7.7 KiB
C#

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();
}
}