light cleanup

This commit is contained in:
Toastie 2025-04-11 08:57:45 +12:00
parent ff46c4eab6
commit 15fafc577d
Signed by: toastie_t0ast
GPG key ID: 74226CF45EEE5AAF

View file

@ -4,13 +4,9 @@ using EllieBot.Db.Models;
namespace EllieBot.Modules.EllieExpressions;
[Name("Expressions")]
public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory clientFactory) : EllieModule<EllieExpressionsService>
public sealed class EllieExpressions(IBotCreds creds, IHttpClientFactory clientFactory)
: EllieModule<EllieExpressionsService>
{
public enum All
{
All
}
private bool AdminInGuildOrOwnerInDm()
=> (ctx.Guild is null && creds.IsOwner(ctx.User))
|| (ctx.Guild is not null && ((IGuildUser)ctx.User).GuildPermissions.Administrator);
@ -25,14 +21,14 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
var ex = await _service.AddAsync(ctx.Guild?.Id, key, message);
await Response()
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_new))
.WithDescription($"#{new kwum(ex.Id)}")
.AddField(GetText(strs.trigger), key)
.AddField(GetText(strs.response),
message.Length > 1024 ? GetText(strs.redacted_too_long) : message))
.SendAsync();
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_new))
.WithDescription($"#{new kwum(ex.Id)}")
.AddField(GetText(strs.trigger), key)
.AddField(GetText(strs.response),
message.Length > 1024 ? GetText(strs.redacted_too_long) : message))
.SendAsync();
}
[Cmd]
@ -95,14 +91,14 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
if (ex is not null)
{
await Response()
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_edited))
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), ex.Trigger)
.AddField(GetText(strs.response),
message.Length > 1024 ? GetText(strs.redacted_too_long) : message))
.SendAsync();
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_edited))
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), ex.Trigger)
.AddField(GetText(strs.response),
message.Length > 1024 ? GetText(strs.redacted_too_long) : message))
.SendAsync();
}
else
{
@ -124,8 +120,8 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
}
var allExpressions = _service.GetExpressionsFor(ctx.Guild?.Id)
.OrderBy(x => x.Trigger)
.ToArray();
.OrderBy(x => x.Trigger)
.ToArray();
if (!allExpressions.Any())
{
@ -134,25 +130,25 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
}
await Response()
.Paginated()
.Items(allExpressions)
.PageSize(20)
.CurrentPage(page)
.Page((exprs, _) =>
{
var desc = exprs
.Select(ex => $"{(ex.ContainsAnywhere ? "🗯" : "")}"
+ $"{(ex.DmResponse ? "" : "")}"
+ $"{(ex.AutoDeleteTrigger ? "" : "")}"
+ $"`{(kwum)ex.Id}` {ex.Trigger}"
+ (string.IsNullOrWhiteSpace(ex.Reactions)
? string.Empty
: " // " + string.Join(" ", ex.GetReactions())))
.Join('\n');
.Paginated()
.Items(allExpressions)
.PageSize(20)
.CurrentPage(page)
.Page((exprs, _) =>
{
var desc = exprs
.Select(ex => $"{(ex.ContainsAnywhere ? "🗯" : "")}"
+ $"{(ex.DmResponse ? "" : "")}"
+ $"{(ex.AutoDeleteTrigger ? "" : "")}"
+ $"`{(kwum)ex.Id}` {ex.Trigger}"
+ (string.IsNullOrWhiteSpace(ex.Reactions)
? string.Empty
: " // " + string.Join(" ", ex.GetReactions())))
.Join('\n');
return CreateEmbed().WithOkColor().WithTitle(GetText(strs.expressions)).WithDescription(desc);
})
.SendAsync();
return CreateEmbed().WithOkColor().WithTitle(GetText(strs.expressions)).WithDescription(desc);
})
.SendAsync();
}
[Cmd]
@ -169,27 +165,27 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
var inter = CreateEditInteraction(id, found);
await Response()
.Interaction(IsValidExprEditor() ? inter : null)
.Embed(CreateEmbed()
.WithOkColor()
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), found.Trigger.TrimTo(1024))
.AddField(GetText(strs.response),
found.Response.TrimTo(1000).Replace("](", "]\\(")))
.SendAsync();
.Interaction(IsValidExprEditor() ? inter : null)
.Embed(CreateEmbed()
.WithOkColor()
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), found.Trigger.TrimTo(1024))
.AddField(GetText(strs.response),
found.Response.TrimTo(1000).Replace("](", "]\\(")))
.SendAsync();
}
private EllieInteractionBase CreateEditInteraction(kwum id, EllieExpression found)
{
var modal = new ModalBuilder()
.WithCustomId("expr:edit_modal")
.WithTitle($"Edit expression {id}")
.AddTextInput(new TextInputBuilder()
.WithLabel(GetText(strs.response))
.WithValue(found.Response)
.WithMinLength(1)
.WithCustomId("expr:edit_modal:response")
.WithStyle(TextInputStyle.Paragraph));
.WithCustomId("expr:edit_modal")
.WithTitle($"Edit expression {id}")
.AddTextInput(new TextInputBuilder()
.WithLabel(GetText(strs.response))
.WithValue(found.Response)
.WithMinLength(1)
.WithCustomId("expr:edit_modal:response")
.WithStyle(TextInputStyle.Paragraph));
var inter = _inter.Create(ctx.User.Id,
new ButtonBuilder()
@ -215,13 +211,13 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
if (ex is not null)
{
await Response()
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_deleted))
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), ex.Trigger.TrimTo(1024))
.AddField(GetText(strs.response), ex.Response.TrimTo(1024)))
.SendAsync();
.Embed(CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.expr_deleted))
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), ex.Trigger.TrimTo(1024))
.AddField(GetText(strs.response), ex.Response.TrimTo(1024)))
.SendAsync();
}
else
{
@ -287,7 +283,9 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
break;
}
}
catch { }
catch
{
}
}
if (succ.Count == 0)
@ -300,9 +298,9 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
await Response()
.Confirm(strs.expr_set(Format.Bold(id.ToString()),
succ.Select(static x => x.ToString()).Join(", ")))
.SendAsync();
.Confirm(strs.expr_set(Format.Bold(id.ToString()),
succ.Select(static x => x.ToString()).Join(", ")))
.SendAsync();
}
[Cmd]
@ -348,16 +346,16 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
if (newVal)
{
await Response()
.Confirm(strs.option_enabled(Format.Code(option.ToString()),
Format.Code(id.ToString())))
.SendAsync();
.Confirm(strs.option_enabled(Format.Code(option.ToString()),
Format.Code(id.ToString())))
.SendAsync();
}
else
{
await Response()
.Confirm(strs.option_disabled(Format.Code(option.ToString()),
Format.Code(id.ToString())))
.SendAsync();
.Confirm(strs.option_disabled(Format.Code(option.ToString()),
Format.Code(id.ToString())))
.SendAsync();
}
}
@ -367,8 +365,8 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
public async Task ExprClear()
{
if (await PromptUserConfirmAsync(CreateEmbed()
.WithTitle("Expression clear")
.WithDescription("This will delete all expressions on this server.")))
.WithTitle("Expression clear")
.WithDescription("This will delete all expressions on this server.")))
{
var count = _service.DeleteAllExpressions(ctx.Guild.Id);
await Response().Confirm(strs.exprs_cleared(count)).SendAsync();
@ -388,7 +386,8 @@ public partial class EllieExpressions(IBotCreds creds, IHttpClientFactory client
var serialized = _service.ExportExpressions(ctx.Guild?.Id);
await using var stream = await serialized.ToStream();
await ctx.User.SendFileAsync(stream, $"exprs-export_{DateTime.UtcNow:yyyy-MM-dd-HH-mm-ss}_{(ctx.Guild?.Id.ToString() ?? "global")}.yml");
await ctx.User.SendFileAsync(stream,
$"exprs-export_{DateTime.UtcNow:yyyy-MM-dd-HH-mm-ss}_{(ctx.Guild?.Id.ToString() ?? "global")}.yml");
}
[Cmd]