From 19bdc0f9deeea5003ccbbe79d8cf0be1501fedc9 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:57:30 +0100 Subject: [PATCH 01/11] Change `WaitingForApprovalResponse` from a reply to a normal message --- .../ApplicationCommands/SlashCommands/ResolveCommand.cs | 5 ++++- .../Interactions/ButtonInteractions/ResolveInteraction.cs | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index 501954b..f320404 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -37,7 +37,8 @@ public async Task ResolveAsync( var isHelper2 = helper2 != null && helper != helper2; var user = Context.User; - return InteractionCallback.Message(new() + + await Context.Channel.SendMessageAsync(new() { Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersResponse, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalResponse, helper))}**", Components = @@ -49,6 +50,8 @@ public async Task ResolveAsync( ], AllowedMentions = AllowedMentionsProperties.None, }); + + return InteractionCallback.DeferredModifyMessage; } public class NameTranslationsProvider : ITranslationsProvider diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs index b3ac021..44f6172 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -24,8 +24,8 @@ public async Task ResolveAsync(ulong helper) throw new(configuration.Interaction.PostAlreadyResolvedResponse); await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); - - return InteractionCallback.Message(new() + + await Context.Channel.SendMessageAsync(new() { Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalResponse, $"<@{helper}>")}**", Components = @@ -37,5 +37,7 @@ public async Task ResolveAsync(ulong helper) ], AllowedMentions = AllowedMentionsProperties.None, }); + + return InteractionCallback.DeferredModifyMessage; } } From 9ef7efc8d276e377dfb3977f6cc141d882fd2b35 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Mon, 12 Feb 2024 23:38:20 +0100 Subject: [PATCH 02/11] Change `WaitingForApproval*Response` to `WaitingForApproval*Message` (since it is not a response anymore) --- ProgramowanieBot/Configuration.cs | 4 ++-- .../ApplicationCommands/SlashCommands/ResolveCommand.cs | 2 +- .../Interactions/UserMenuInteractions/ResolveInteraction.cs | 2 +- ProgramowanieBot/appsettings - sample.json | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ProgramowanieBot/Configuration.cs b/ProgramowanieBot/Configuration.cs index f922b20..8a252dc 100644 --- a/ProgramowanieBot/Configuration.cs +++ b/ProgramowanieBot/Configuration.cs @@ -87,8 +87,8 @@ public class InteractionHandlerConfiguration public string ShowProfileOnBotResponse { get; set; } public StealEmojiConfiguration StealEmoji { get; set; } public string SyncingPostsResponse { get; set; } - public string WaitingForApprovalResponse { get; set; } - public string WaitingForApprovalWith2HelpersResponse { get; set; } + public string WaitingForApprovalMessage { get; set; } + public string WaitingForApprovalWith2HelpersMessage { get; set; } public class ReactionCommandsConfiguration { diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index f320404..17710bb 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -40,7 +40,7 @@ public async Task ResolveAsync( await Context.Channel.SendMessageAsync(new() { - Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersResponse, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalResponse, helper))}**", + Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", Components = [ new ActionRowProperties( diff --git a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs index 3f58e9e..ad373dd 100644 --- a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs @@ -45,7 +45,7 @@ public async Task ResolveAsync() var user = Context.User; return InteractionCallback.Message(new() { - Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersResponse, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalResponse, helper))}**", + Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", Components = [ new ActionRowProperties( diff --git a/ProgramowanieBot/appsettings - sample.json b/ProgramowanieBot/appsettings - sample.json index 3887b36..4f82bb4 100644 --- a/ProgramowanieBot/appsettings - sample.json +++ b/ProgramowanieBot/appsettings - sample.json @@ -97,8 +97,8 @@ "StealEmojisMenuPlaceholder": "Wybierz emoji do dodania" }, "SyncingPostsResponse": "Synchronizowanie postów! Ta operacja może zająć długi czas.", - "WaitingForApprovalResponse": "{0} został wskazany jako pomocnik! Oczekiwanie na zatwierdzenie przez administrację!", - "WaitingForApprovalWith2HelpersResponse": "{0} oraz {1} zostali wskazani jako pomocnicy! Oczekiwanie na zatwierdzenie przez administrację!" + "WaitingForApprovalMessage": "{0} został wskazany jako pomocnik! Oczekiwanie na zatwierdzenie przez administrację!", + "WaitingForApprovalWith2HelpersMessage": "{0} oraz {1} zostali wskazani jako pomocnicy! Oczekiwanie na zatwierdzenie przez administrację!" }, "Discord": { "Token": "" From 30f89b266c2f6c313879663b63016f1058677180 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Mon, 12 Feb 2024 23:57:10 +0100 Subject: [PATCH 03/11] Move `ApproveInteraction` button to `PostResolvedNotificationChannel` --- ProgramowanieBot/Configuration.cs | 1 + .../SlashCommands/ResolveCommand.cs | 14 +++++++++----- .../ButtonInteractions/ApproveInteraction.cs | 15 +++++++++++---- .../ButtonInteractions/ResolveInteraction.cs | 15 +++++++++------ .../UserMenuInteractions/ResolveInteraction.cs | 17 ++++++++++++----- ProgramowanieBot/appsettings - sample.json | 1 + 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/ProgramowanieBot/Configuration.cs b/ProgramowanieBot/Configuration.cs index 8a252dc..25e46ae 100644 --- a/ProgramowanieBot/Configuration.cs +++ b/ProgramowanieBot/Configuration.cs @@ -68,6 +68,7 @@ public class InteractionHandlerConfiguration { public string AlreadyMentionedResponse { get; set; } public string ApproveButtonLabel { get; set; } + public string ApprovedPostResolvingMessage { get; set; } public string IHelpedMyselfButtonLabel { get; set; } public string NotHelpChannelResponse { get; set; } public string NotOwnMessageResponse { get; set; } diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index 17710bb..5570f13 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -33,22 +33,26 @@ public async Task ResolveAsync( if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); - var isHelper2 = helper2 != null && helper != helper2; var user = Context.User; - await Context.Channel.SendMessageAsync(new() + var closingMessage = await Context.Channel.SendMessageAsync(new() { Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", + AllowedMentions = AllowedMentionsProperties.None, + }); + + + await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() + { + Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", Components = [ new ActionRowProperties( [ - new ActionButtonProperties($"approve:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), + new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), ]), ], - AllowedMentions = AllowedMentionsProperties.None, }); return InteractionCallback.DeferredModifyMessage; diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs index 765a3bb..96ed049 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Options; using NetCord; +using NetCord.Gateway; using NetCord.Rest; using NetCord.Services; using NetCord.Services.Interactions; @@ -16,12 +17,10 @@ public class ApproveInteraction(IServiceProvider serviceProvider, IOptions(Permissions.Administrator)] [Interaction("approve")] - public async Task ApproveAsync(ulong helper, bool giveReputation, ulong? helper2 = null, bool? giveReputation2 = null) + public async Task ApproveAsync(ulong channelId, ulong closingMessageId, ulong helper, bool giveReputation, ulong? helper2 = null, bool? giveReputation2 = null) { var configuration = options.Value; - var channel = (GuildThread)Context.Channel; - var channelId = channel.Id; await using (var context = serviceProvider.GetRequiredService()) { await using var transaction = await context.Database.BeginTransactionAsync(); @@ -38,13 +37,21 @@ public async Task ApproveAsync(ulong helper, bool giveReputation, ulong? helper2 await transaction.CommitAsync(); } + GuildThread channel = (GuildThread)Context.Client.Rest.GetChannelAsync(channelId).Result; + await RespondAsync(InteractionCallback.ModifyMessage(m => { - m.Content = $"**{configuration.Emojis.Success} {configuration.Interaction.PostResolvedResponse}**"; + m.Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.ApprovedPostResolvingMessage, channel)}**"; m.Components = []; })); var user = Context.User; + + await channel.ModifyMessageAsync(closingMessageId, m => + { + m.Content = $"**{configuration.Emojis.Success} {configuration.Interaction.PostResolvedResponse}**"; + m.Components = []; + }); await channel.ModifyAsync(t => { t.Archived = true; diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs index 44f6172..72e7772 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -23,19 +23,22 @@ public async Task ResolveAsync(ulong helper) if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); - - await Context.Channel.SendMessageAsync(new() + var closingMessage = await Context.Channel.SendMessageAsync(new() { - Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalResponse, $"<@{helper}>")}**", + Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalMessage, $"<@{helper}>")}**", + AllowedMentions = AllowedMentionsProperties.None, + }); + + await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() + { + Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", Components = [ new ActionRowProperties( [ - new ActionButtonProperties($"approve:{helper}:{helper != Context.User.Id}::", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), + new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper}:{helper != Context.User.Id}::", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), ]), ], - AllowedMentions = AllowedMentionsProperties.None, }); return InteractionCallback.DeferredModifyMessage; diff --git a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs index ad373dd..c641af9 100644 --- a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs @@ -40,20 +40,27 @@ public async Task ResolveAsync() else helper2 = null; - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); - var user = Context.User; - return InteractionCallback.Message(new() + + var closingMessage = await Context.Channel.SendMessageAsync(new() { Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", + AllowedMentions = AllowedMentionsProperties.None, + }); + + + await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() + { + Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", Components = [ new ActionRowProperties( [ - new ActionButtonProperties($"approve:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), + new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), ]), ], - AllowedMentions = AllowedMentionsProperties.None, }); + + return InteractionCallback.DeferredModifyMessage; } } diff --git a/ProgramowanieBot/appsettings - sample.json b/ProgramowanieBot/appsettings - sample.json index 4f82bb4..5f8664e 100644 --- a/ProgramowanieBot/appsettings - sample.json +++ b/ProgramowanieBot/appsettings - sample.json @@ -63,6 +63,7 @@ "Interaction": { "AlreadyMentionedResponse": "Już spingowano!", "ApproveButtonLabel": "Zatwierdź", + "ApprovedPostResolvingMessage": "Post {0} został rozwiązany. Zatwierdzono przez administrację.", "IHelpedMyselfButtonLabel": "Sam sobie pomogłem", "NotHelpChannelResponse": "Nie możesz używać tej komendy tutaj!", "NotOwnMessageResponse": "Możesz użyć tej komendy tylko na własnych wiadomościach!", From 8aa666c070e239efd9da99331f3c7fe9508e5959 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Tue, 13 Feb 2024 00:13:22 +0100 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com> --- .../Interactions/ButtonInteractions/ApproveInteraction.cs | 3 ++- .../Interactions/UserMenuInteractions/ResolveInteraction.cs | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs index 96ed049..f40e2ef 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs @@ -37,7 +37,7 @@ public async Task ApproveAsync(ulong channelId, ulong closingMessageId, ulong he await transaction.CommitAsync(); } - GuildThread channel = (GuildThread)Context.Client.Rest.GetChannelAsync(channelId).Result; + var channel = (GuildThread)Context.Client.Rest.GetChannelAsync(channelId).Result; await RespondAsync(InteractionCallback.ModifyMessage(m => { @@ -52,6 +52,7 @@ await channel.ModifyMessageAsync(closingMessageId, m => m.Content = $"**{configuration.Emojis.Success} {configuration.Interaction.PostResolvedResponse}**"; m.Components = []; }); + await channel.ModifyAsync(t => { t.Archived = true; diff --git a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs index c641af9..bfefcc0 100644 --- a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs @@ -48,7 +48,6 @@ public async Task ResolveAsync() AllowedMentions = AllowedMentionsProperties.None, }); - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() { Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", From 7ddf3bcba477d1a4cb77c3dd7ccdbbb8b7670fb7 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Tue, 13 Feb 2024 17:03:32 +0100 Subject: [PATCH 05/11] Change `postId` to `channelId`, as by convention this is ineed a channel ID and the word "post" should only be used when reffering to the database object --- ProgramowanieBot/Helpers/PostsHelper.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ProgramowanieBot/Helpers/PostsHelper.cs b/ProgramowanieBot/Helpers/PostsHelper.cs index 12f938e..4b07312 100644 --- a/ProgramowanieBot/Helpers/PostsHelper.cs +++ b/ProgramowanieBot/Helpers/PostsHelper.cs @@ -8,15 +8,15 @@ namespace ProgramowanieBot.Helpers; internal static class PostsHelper { - public static async Task ResolvePostAsync(DataContext context, ulong postId) + public static async Task ResolvePostAsync(DataContext context, ulong channelId) { Debug.Assert(context.Database.CurrentTransaction != null, "Transaction is required."); - var post = await context.Posts.FirstOrDefaultAsync(p => p.PostId == postId); + var post = await context.Posts.FirstOrDefaultAsync(p => p.PostId == channelId); if (post == null) await context.Posts.AddAsync(new() { - PostId = postId, + PostId = channelId, PostResolveReminderCounter = 0, IsResolved = true, }); @@ -24,15 +24,15 @@ await context.Posts.AddAsync(new() post.IsResolved = true; } - public static async Task IncrementPostResolveReminderCounterAsync(DataContext context, ulong postId) + public static async Task IncrementPostResolveReminderCounterAsync(DataContext context, ulong channelId) { Debug.Assert(context.Database.CurrentTransaction != null, "Transaction is required."); - var post = await context.Posts.FirstOrDefaultAsync(p => p.PostId == postId); + var post = await context.Posts.FirstOrDefaultAsync(p => p.PostId == channelId); if (post == null) await context.Posts.AddAsync(new() { - PostId = postId, + PostId = channelId, PostResolveReminderCounter = 1, IsResolved = false, }); From 61507a682a4c0087a9153c195881d2004655d54f Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Tue, 13 Feb 2024 17:54:50 +0100 Subject: [PATCH 06/11] Rename `helper` to `helperId` in `ResolveInteraction` button interaction to avoid confusion --- .../Interactions/ButtonInteractions/ResolveInteraction.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs index 72e7772..b618e0f 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -13,7 +13,7 @@ namespace ProgramowanieBot.InteractionHandlerModules.Interactions.ButtonInteract public class ResolveInteraction(IServiceProvider serviceProvider, IOptions options) : InteractionModule { [Interaction("resolve")] - public async Task ResolveAsync(ulong helper) + public async Task ResolveAsync(ulong helperId) { var configuration = options.Value; @@ -25,7 +25,7 @@ public async Task ResolveAsync(ulong helper) var closingMessage = await Context.Channel.SendMessageAsync(new() { - Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalMessage, $"<@{helper}>")}**", + Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalMessage, $"<@{helperId}>")}**", AllowedMentions = AllowedMentionsProperties.None, }); @@ -36,7 +36,7 @@ public async Task ResolveAsync(ulong helper) [ new ActionRowProperties( [ - new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper}:{helper != Context.User.Id}::", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), + new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helperId}:{helperId != Context.User.Id}::", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), ]), ], }); From beb88882006f2a99037e405cdba8d202b6ff47e5 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Tue, 13 Feb 2024 18:28:23 +0100 Subject: [PATCH 07/11] Add `SendPostResolveMessages` to `PostHelper` and replace unnecessarily long code with the method call wherever possible or needed --- ProgramowanieBot/Helpers/PostsHelper.cs | 26 ++++++++++++++++++ .../SlashCommands/ResolveCommand.cs | 27 +++---------------- .../ButtonInteractions/ResolveInteraction.cs | 22 +++------------ .../ResolveInteraction.cs | 25 +++-------------- 4 files changed, 35 insertions(+), 65 deletions(-) diff --git a/ProgramowanieBot/Helpers/PostsHelper.cs b/ProgramowanieBot/Helpers/PostsHelper.cs index 4b07312..e08d535 100644 --- a/ProgramowanieBot/Helpers/PostsHelper.cs +++ b/ProgramowanieBot/Helpers/PostsHelper.cs @@ -2,6 +2,9 @@ using Microsoft.EntityFrameworkCore; +using NetCord; +using NetCord.Rest; + using ProgramowanieBot.Data; namespace ProgramowanieBot.Helpers; @@ -39,4 +42,27 @@ await context.Posts.AddAsync(new() else post.PostResolveReminderCounter++; } + + public static async Task SendPostResolveMessages(ulong channelId, ulong userId, ulong helperId, ulong? helper2Id, RestClient rest, Configuration configuration) + {; + var isHelper2 = helper2Id != null && helperId != helper2Id; + var closingMessage = await rest.SendMessageAsync(channelId, new() + { + Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, $"<@{helperId}>", $"<@{helper2Id}>") : string.Format(configuration.Interaction.WaitingForApprovalMessage, $"<@{helperId}>"))}**", + AllowedMentions = AllowedMentionsProperties.None, + }); + + + await rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() + { + Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, $"<#{channelId}>")}**", + Components = + [ + new ActionRowProperties( + [ + new ActionButtonProperties($"approve:{channelId}:{closingMessage.Id}:{helperId}:{helperId != userId}:{(isHelper2 ? helper2Id : null)}:{(isHelper2 ? helper2Id != userId : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), + ]), + ], + }); + } } diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index 5570f13..30270bf 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -9,6 +9,7 @@ using NetCord.Services.ApplicationCommands; using ProgramowanieBot.Data; +using ProgramowanieBot.Helpers; namespace ProgramowanieBot.InteractionHandlerModules.Commands.SlashCommands; @@ -25,35 +26,13 @@ public async Task ResolveAsync( User? helper2 = null) { var configuration = options.Value; - - var channel = Context.Channel; - var channelId = channel.Id; + var channelId = Context.Channel.Id; await using (var context = serviceProvider.GetRequiredService()) if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - var isHelper2 = helper2 != null && helper != helper2; - var user = Context.User; - - var closingMessage = await Context.Channel.SendMessageAsync(new() - { - Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", - AllowedMentions = AllowedMentionsProperties.None, - }); - - - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() - { - Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), - ]), - ], - }); + await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs index b618e0f..9916086 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -7,6 +7,7 @@ using NetCord.Services.Interactions; using ProgramowanieBot.Data; +using ProgramowanieBot.Helpers; namespace ProgramowanieBot.InteractionHandlerModules.Interactions.ButtonInteractions; @@ -16,30 +17,13 @@ public class ResolveInteraction(IServiceProvider serviceProvider, IOptions ResolveAsync(ulong helperId) { var configuration = options.Value; + var channelId = Context.Channel.Id; - var channel = Context.Channel; - var channelId = channel.Id; await using (var context = serviceProvider.GetRequiredService()) if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - var closingMessage = await Context.Channel.SendMessageAsync(new() - { - Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalMessage, $"<@{helperId}>")}**", - AllowedMentions = AllowedMentionsProperties.None, - }); - - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() - { - Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helperId}:{helperId != Context.User.Id}::", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), - ]), - ], - }); + await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helperId, null, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } diff --git a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs index c641af9..3c005f4 100644 --- a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs @@ -7,6 +7,7 @@ using NetCord.Services.Interactions; using ProgramowanieBot.Data; +using ProgramowanieBot.Helpers; namespace ProgramowanieBot.InteractionHandlerModules.Interactions.UserMenuInteractions; @@ -16,9 +17,8 @@ public class ResolveInteraction(IServiceProvider serviceProvider, IOptions ResolveAsync() { var configuration = options.Value; + var channelId = Context.Channel.Id; - var channel = Context.Channel; - var channelId = channel.Id; await using (var context = serviceProvider.GetRequiredService()) if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); @@ -40,26 +40,7 @@ public async Task ResolveAsync() else helper2 = null; - var user = Context.User; - - var closingMessage = await Context.Channel.SendMessageAsync(new() - { - Content = $"**{configuration.Emojis.Success} {(isHelper2 ? string.Format(configuration.Interaction.WaitingForApprovalWith2HelpersMessage, helper, helper2) : string.Format(configuration.Interaction.WaitingForApprovalMessage, helper))}**", - AllowedMentions = AllowedMentionsProperties.None, - }); - - - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() - { - Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{Context.Channel.Id}:{closingMessage.Id}:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), - ]), - ], - }); + await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } From fa895c4c7908808bb32dc9a9ab2805bfc9f62cc8 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Tue, 13 Feb 2024 19:06:05 +0100 Subject: [PATCH 08/11] Rename `SendPostResolveMessages` to `SendPostResolveMessagesAsync` --- ProgramowanieBot/Helpers/PostsHelper.cs | 2 +- .../Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs | 2 +- .../Interactions/ButtonInteractions/ResolveInteraction.cs | 2 +- .../Interactions/UserMenuInteractions/ResolveInteraction.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ProgramowanieBot/Helpers/PostsHelper.cs b/ProgramowanieBot/Helpers/PostsHelper.cs index e08d535..9b1585c 100644 --- a/ProgramowanieBot/Helpers/PostsHelper.cs +++ b/ProgramowanieBot/Helpers/PostsHelper.cs @@ -43,7 +43,7 @@ await context.Posts.AddAsync(new() post.PostResolveReminderCounter++; } - public static async Task SendPostResolveMessages(ulong channelId, ulong userId, ulong helperId, ulong? helper2Id, RestClient rest, Configuration configuration) + public static async Task SendPostResolveMessagesAsync(ulong channelId, ulong userId, ulong helperId, ulong? helper2Id, RestClient rest, Configuration configuration) {; var isHelper2 = helper2Id != null && helperId != helper2Id; var closingMessage = await rest.SendMessageAsync(channelId, new() diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index 30270bf..fceaf8c 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -32,7 +32,7 @@ public async Task ResolveAsync( if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs index 9916086..aa2368f 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -23,7 +23,7 @@ public async Task ResolveAsync(ulong helperId) if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved)) throw new(configuration.Interaction.PostAlreadyResolvedResponse); - await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helperId, null, Context.Client.Rest, configuration); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helperId, null, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } diff --git a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs index 3c005f4..75e94ef 100644 --- a/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/UserMenuInteractions/ResolveInteraction.cs @@ -40,7 +40,7 @@ public async Task ResolveAsync() else helper2 = null; - await PostsHelper.SendPostResolveMessages(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); return InteractionCallback.DeferredModifyMessage; } From caab97cd5b13b852c61712d4b6e72c453128807c Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:58:43 +0100 Subject: [PATCH 09/11] Fix bot returning a forbidden callback type for `ResolveCommand`. A fix for a previous mistake --- .../SlashCommands/ResolveCommand.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index fceaf8c..beb37ab 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System.Collections; +using System.Globalization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -34,7 +35,12 @@ public async Task ResolveAsync( await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); - return InteractionCallback.DeferredModifyMessage; + return InteractionCallback.Message(new() + { + Content = configuration.Emojis.Success, + Flags = MessageFlags.Ephemeral + }); + } public class NameTranslationsProvider : ITranslationsProvider From 7665db112a6f6696c26fc2fb0f819aebdaf786ea Mon Sep 17 00:00:00 2001 From: Andrzej Bansleben <64475360+jedrek0429@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:01:17 +0100 Subject: [PATCH 10/11] Apply suggestion (formatting issue) Co-authored-by: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com> --- ProgramowanieBot/Helpers/PostsHelper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ProgramowanieBot/Helpers/PostsHelper.cs b/ProgramowanieBot/Helpers/PostsHelper.cs index 9b1585c..88582f7 100644 --- a/ProgramowanieBot/Helpers/PostsHelper.cs +++ b/ProgramowanieBot/Helpers/PostsHelper.cs @@ -52,7 +52,6 @@ public static async Task SendPostResolveMessagesAsync(ulong channelId, ulong use AllowedMentions = AllowedMentionsProperties.None, }); - await rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, new() { Content = $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, $"<#{channelId}>")}**", From 2eee47cebc9c9a29e48a99b4f9fb8d79b45166b3 Mon Sep 17 00:00:00 2001 From: jedrek0429 <64475360+jedrek0429@users.noreply.github.com> Date: Wed, 14 Feb 2024 22:29:47 +0100 Subject: [PATCH 11/11] Apply suggestion (formatting issue) --- .../Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs index beb37ab..050bdb1 100644 --- a/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs +++ b/ProgramowanieBot/Modules/ApplicationCommands/SlashCommands/ResolveCommand.cs @@ -40,7 +40,6 @@ public async Task ResolveAsync( Content = configuration.Emojis.Success, Flags = MessageFlags.Ephemeral }); - } public class NameTranslationsProvider : ITranslationsProvider