diff --git a/ProgramowanieBot/Configuration.cs b/ProgramowanieBot/Configuration.cs index f922b20..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; } @@ -87,8 +88,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/Helpers/PostsHelper.cs b/ProgramowanieBot/Helpers/PostsHelper.cs index 12f938e..88582f7 100644 --- a/ProgramowanieBot/Helpers/PostsHelper.cs +++ b/ProgramowanieBot/Helpers/PostsHelper.cs @@ -2,21 +2,24 @@ using Microsoft.EntityFrameworkCore; +using NetCord; +using NetCord.Rest; + using ProgramowanieBot.Data; 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,19 +27,41 @@ 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, }); else post.PostResolveReminderCounter++; } + + 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() + { + 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 501954b..050bdb1 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; @@ -9,6 +10,7 @@ using NetCord.Services.ApplicationCommands; using ProgramowanieBot.Data; +using ProgramowanieBot.Helpers; namespace ProgramowanieBot.InteractionHandlerModules.Commands.SlashCommands; @@ -25,29 +27,18 @@ 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); - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); - var isHelper2 = helper2 != null && helper != helper2; - 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))}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{helper.Id}:{helper != user}:{(isHelper2 ? helper2!.Id : null)}:{(isHelper2 ? helper2 != user : null)}", configuration.Interaction.ApproveButtonLabel, ButtonStyle.Success), - ]), - ], - AllowedMentions = AllowedMentionsProperties.None, + Content = configuration.Emojis.Success, + Flags = MessageFlags.Ephemeral }); } diff --git a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ApproveInteraction.cs index 765a3bb..f40e2ef 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,22 @@ public async Task ApproveAsync(ulong helper, bool giveReputation, ulong? helper2 await transaction.CommitAsync(); } + var 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 b3ac021..aa2368f 100644 --- a/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs +++ b/ProgramowanieBot/Modules/Interactions/ButtonInteractions/ResolveInteraction.cs @@ -7,35 +7,24 @@ using NetCord.Services.Interactions; using ProgramowanieBot.Data; +using ProgramowanieBot.Helpers; namespace ProgramowanieBot.InteractionHandlerModules.Interactions.ButtonInteractions; 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; + 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); - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helperId, null, Context.Client.Rest, configuration); - return InteractionCallback.Message(new() - { - Content = $"**{configuration.Emojis.Success} {string.Format(configuration.Interaction.WaitingForApprovalResponse, $"<@{helper}>")}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{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 3f58e9e..75e94ef 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,20 +40,8 @@ public async Task ResolveAsync() else helper2 = null; - await Context.Client.Rest.SendMessageAsync(configuration.Interaction.PostResolvedNotificationChannelId, $"**{string.Format(configuration.Interaction.PostResolvedNotificationMessage, channel)}**"); + await PostsHelper.SendPostResolveMessagesAsync(channelId, Context.User.Id, helper.Id, helper2?.Id, Context.Client.Rest, configuration); - 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))}**", - Components = - [ - new ActionRowProperties( - [ - new ActionButtonProperties($"approve:{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 3887b36..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!", @@ -97,8 +98,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": ""