add: Added .quteedit command

add: Added an edit button to .quoteshow and .exprshow commands that opens a modal which lets you edit the quote or expr in question
This commit is contained in:
Kwoth
2024-05-21 00:33:40 +00:00
parent b51ce34190
commit 06321380ee
17 changed files with 313 additions and 78 deletions

View File

@@ -1,5 +1,7 @@
#nullable disable
using NadekoBot.Db.Models;
namespace NadekoBot.Modules.NadekoExpressions;
[Name("Expressions")]
@@ -34,12 +36,12 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
await Response()
.Embed(_sender.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))
.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();
}
@@ -93,8 +95,7 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
return;
}
if ((channel is null && !_creds.IsOwner(ctx.User))
|| (channel is not null && !((IGuildUser)ctx.User).GuildPermissions.Administrator))
if (!IsValidExprEditor())
{
await Response().Error(strs.expr_insuff_perms).SendAsync();
return;
@@ -105,12 +106,12 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
{
await Response()
.Embed(_sender.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))
.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
@@ -119,6 +120,10 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
}
}
private bool IsValidExprEditor()
=> (ctx.Guild is not null && ((IGuildUser)ctx.User).GuildPermissions.Administrator)
|| (ctx.Guild is null && _creds.IsOwner(ctx.User));
[Cmd]
[Priority(1)]
public async Task ExprList(int page = 1)
@@ -132,7 +137,7 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
.OrderBy(x => x.Trigger)
.ToArray();
if (allExpressions is null || !allExpressions.Any())
if (!allExpressions.Any())
{
await Response().Error(strs.expr_no_found).SendAsync();
return;
@@ -171,16 +176,48 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
return;
}
var inter = CreateEditInteraction(id, found);
await Response()
.Interaction(IsValidExprEditor() ? inter : null)
.Embed(_sender.CreateEmbed()
.WithOkColor()
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), found.Trigger.TrimTo(1024))
.AddField(GetText(strs.response),
found.Response.TrimTo(1000).Replace("](", "]\\(")))
.WithOkColor()
.WithDescription($"#{id}")
.AddField(GetText(strs.trigger), found.Trigger.TrimTo(1024))
.AddField(GetText(strs.response),
found.Response.TrimTo(1000).Replace("](", "]\\(")))
.SendAsync();
}
private NadekoInteractionBase CreateEditInteraction(kwum id, NadekoExpression 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));
var inter = _inter.Create(ctx.User.Id,
new ButtonBuilder()
.WithEmote(Emoji.Parse("📝"))
.WithLabel("Edit")
.WithStyle(ButtonStyle.Primary)
.WithCustomId("test"),
modal,
async (sm) =>
{
var msg = sm.Data.Components.FirstOrDefault()?.Value;
await ExprEdit(id, msg);
}
);
return inter;
}
public async Task ExprDeleteInternalAsync(kwum id)
{
var ex = await _service.DeleteAsync(ctx.Guild?.Id, id);
@@ -189,11 +226,11 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
{
await Response()
.Embed(_sender.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)))
.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
@@ -340,8 +377,8 @@ public partial class NadekoExpressions : NadekoModule<NadekoExpressionsService>
public async Task ExprClear()
{
if (await PromptUserConfirmAsync(_sender.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();

View File

@@ -154,7 +154,7 @@ public partial class Gambling : GamblingModule<GamblingService>
await smc.RespondConfirmAsync(_sender, GetText(strs.remind_timely(tt)), ephemeral: true);
}
private NadekoInteraction CreateRemindMeInteraction(int period)
private NadekoInteractionBase CreateRemindMeInteraction(int period)
=> _inter
.Create(ctx.User.Id,
new ButtonBuilder(
@@ -415,7 +415,7 @@ public partial class Gambling : GamblingModule<GamblingService>
.Pipe(text => smc.RespondConfirmAsync(_sender, text, ephemeral: true));
}
private NadekoInteraction CreateCashInteraction()
private NadekoInteractionBase CreateCashInteraction()
=> _inter.Create(ctx.User.Id,
new ButtonBuilder(
customId: "cash:bank_show_balance",

View File

@@ -1,4 +1,6 @@
#nullable disable warnings
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Common.Yml;
using NadekoBot.Db;
using NadekoBot.Db.Models;
@@ -133,23 +135,54 @@ public partial class Utility
await ShowQuoteData(quote);
}
private async Task ShowQuoteData(Quote data)
private NadekoInteractionBase CreateEditInteraction(kwum id, Quote found)
{
var modal = new ModalBuilder()
.WithCustomId("quote:edit_modal")
.WithTitle($"Edit expression {id}")
.AddTextInput(new TextInputBuilder()
.WithLabel(GetText(strs.response))
.WithValue(found.Text)
.WithMinLength(1)
.WithCustomId("quote:edit_modal:response")
.WithStyle(TextInputStyle.Paragraph));
var inter = _inter.Create(ctx.User.Id,
new ButtonBuilder()
.WithEmote(Emoji.Parse("📝"))
.WithLabel("Edit")
.WithStyle(ButtonStyle.Primary)
.WithCustomId("test"),
modal,
async (sm) =>
{
var msg = sm.Data.Components.FirstOrDefault()?.Value;
if(!string.IsNullOrWhiteSpace(msg))
await QuoteEdit(id, msg);
}
);
return inter;
}
private async Task ShowQuoteData(Quote quote)
{
var inter = CreateEditInteraction(quote.Id, quote);
var eb = _sender.CreateEmbed()
.WithOkColor()
.WithTitle($"{GetText(strs.quote_id($"`{new kwum(data.Id)}"))}`")
.WithDescription(Format.Sanitize(data.Text).Replace("](", "]\\(").TrimTo(4096))
.AddField(GetText(strs.trigger), data.Keyword)
.WithTitle($"{GetText(strs.quote_id($"`{new kwum(quote.Id)}"))}`")
.WithDescription(Format.Sanitize(quote.Text).Replace("](", "]\\(").TrimTo(4096))
.AddField(GetText(strs.trigger), quote.Keyword)
.WithFooter(
GetText(strs.created_by($"{data.AuthorName} ({data.AuthorId})")));
GetText(strs.created_by($"{quote.AuthorName} ({quote.AuthorId})")));
if (!(data.Text.Length > 4096))
if (!(quote.Text.Length > 4096))
{
await Response().Embed(eb).SendAsync();
await Response().Embed(eb).Interaction(quote.AuthorId == ctx.User.Id ? inter : null).SendAsync();
return;
}
await using var textStream = await data.Text.ToStream();
await using var textStream = await quote.Text.ToStream();
await Response()
.Embed(eb)
@@ -255,6 +288,47 @@ public partial class Utility
await Response().Confirm(strs.quote_added_new(Format.Code(new kwum(q.Id).ToString()))).SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteEdit(kwum quoteId, [Leftover] string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return;
}
Quote q;
await using (var uow = _db.GetDbContext())
{
var intId = (int)quoteId;
var result = await uow.GetTable<Quote>()
.Where(x => x.Id == intId && x.AuthorId == ctx.User.Id)
.Set(x => x.Text, text)
.UpdateWithOutputAsync((del, ins) => ins);
q = result.FirstOrDefault();
await uow.SaveChangesAsync();
}
if (q is not null)
{
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.WithTitle(GetText(strs.quote_edited))
.WithDescription($"#{quoteId}")
.AddField(GetText(strs.trigger), q.Keyword)
.AddField(GetText(strs.response),
text.Length > 1024 ? GetText(strs.redacted_too_long) : text))
.SendAsync();
}
else
{
await Response().Error(strs.expr_no_found_id).SendAsync();
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteDelete(kwum quoteId)

View File

@@ -506,7 +506,7 @@ public partial class Xp : NadekoModule<XpService>
{
var result = await _service.BuyShopItemAsync(ctx.User.Id, (XpShopItemType)type, key);
NadekoInteraction GetUseInteraction()
NadekoInteractionBase GetUseInteraction()
{
return _inter.Create(ctx.User.Id,
new(label: "Use", customId: "xpshop:use_item", emote: Emoji.Parse("👐")),