Skip to content

Move the Approve button to the notification channel set in appsettings.json + minor change regarding the Waiting For Approval message #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ProgramowanieBot/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
{
Expand Down
37 changes: 31 additions & 6 deletions ProgramowanieBot/Helpers/PostsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,66 @@

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,
});
else
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),
]),
],
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections;
using System.Globalization;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -9,6 +10,7 @@
using NetCord.Services.ApplicationCommands;

using ProgramowanieBot.Data;
using ProgramowanieBot.Helpers;

namespace ProgramowanieBot.InteractionHandlerModules.Commands.SlashCommands;

Expand All @@ -25,30 +27,20 @@ public async Task<InteractionCallback> 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<DataContext>())
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
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to send the message about resolve in the interaction response instead of responding with an emoji and sending a normal message?
You also added an unnecessary blank line at the end.

Copy link
Contributor Author

@jedrek0429 jedrek0429 Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the main points of this PR, besides moving the Approve button, was to change this exact behaviour that was adopted earlier. I wanted to get rid of the ugly text that said "Original message was deleted" (as seen on the screenshot).
image
Naturally, when it comes to using a slash command this does not take place, however, I wanted to maintain some sort of consistency and make all the Post Resolve messages regular messages instead of replies or interaction responses.

Also, thank you for your additional feedback regarding my code formatting. I will include that in my next commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As promised, formatting issue fixed with 2eee47c.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think slash commands look good. "Original message was deleted" happens only when using a component of an ephemeral message.

Copy link
Contributor Author

@jedrek0429 jedrek0429 Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware of that, but, again, all I wanted was a bit of consistency. Moreover, changing that would require either changing the helper method or not using it, which would require more code while the change wouldn't be neither that noticeable nor that important for the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole change won't be noticeable by users at all but I think sending an interaction response with an emoji is not a good idea, it's better to just respond to the interaction with the resolve message. We have more inconsistency with the current approach either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what are you suggesting? Abandoning the helper in this particular case, or adding an exception to the method where if it's a slash command it will use an interaction response rather than a regular message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create another overload (or method because maybe the current name would be confusing) that is for slash commands. The 2 methods can also share some private helpers to make the code less repetitive.

}

public class NameTranslationsProvider : ITranslationsProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Options;

using NetCord;
using NetCord.Gateway;
using NetCord.Rest;
using NetCord.Services;
using NetCord.Services.Interactions;
Expand All @@ -16,12 +17,10 @@ public class ApproveInteraction(IServiceProvider serviceProvider, IOptions<Confi
{
[RequireUserPermissions<ButtonInteractionContext>(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<DataContext>())
{
await using var transaction = await context.Database.BeginTransactionAsync();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration> options) : InteractionModule<ButtonInteractionContext>
{
[Interaction("resolve")]
public async Task<InteractionCallback> ResolveAsync(ulong helper)
public async Task<InteractionCallback> 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<DataContext>())
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NetCord.Services.Interactions;

using ProgramowanieBot.Data;
using ProgramowanieBot.Helpers;

namespace ProgramowanieBot.InteractionHandlerModules.Interactions.UserMenuInteractions;

Expand All @@ -16,9 +17,8 @@ public class ResolveInteraction(IServiceProvider serviceProvider, IOptions<Confi
public async Task<InteractionCallback> ResolveAsync()
{
var configuration = options.Value;
var channelId = Context.Channel.Id;

var channel = Context.Channel;
var channelId = channel.Id;
await using (var context = serviceProvider.GetRequiredService<DataContext>())
if (await context.Posts.AnyAsync(p => p.PostId == channelId && p.IsResolved))
throw new(configuration.Interaction.PostAlreadyResolvedResponse);
Expand All @@ -40,20 +40,8 @@ public async Task<InteractionCallback> 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;
}
}
5 changes: 3 additions & 2 deletions ProgramowanieBot/appsettings - sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down Expand Up @@ -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": ""
Expand Down