Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
38 changes: 32 additions & 6 deletions ProgramowanieBot/Helpers/PostsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,67 @@

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
Expand Up @@ -9,6 +9,7 @@
using NetCord.Services.ApplicationCommands;

using ProgramowanieBot.Data;
using ProgramowanieBot.Helpers;

namespace ProgramowanieBot.InteractionHandlerModules.Commands.SlashCommands;

Expand All @@ -25,30 +26,15 @@ 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,
});
return InteractionCallback.DeferredModifyMessage;
}

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