From 59c2c5eada6da66f5472bef7859c3fe1975d6f91 Mon Sep 17 00:00:00 2001 From: neroduckale <100025711+neroduckale@users.noreply.github.com> Date: Tue, 12 Dec 2023 01:38:26 +0500 Subject: [PATCH] Add link to original message for activated reminders and /listremind (#203) --- src/Commands/RemindCommandGroup.cs | 37 +++++++++++++--------- src/Data/Reminder.cs | 3 +- src/Services/Update/MemberUpdateService.cs | 15 ++++++--- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/Commands/RemindCommandGroup.cs b/src/Commands/RemindCommandGroup.cs index 79a583b..1966b9b 100644 --- a/src/Commands/RemindCommandGroup.cs +++ b/src/Commands/RemindCommandGroup.cs @@ -26,19 +26,21 @@ namespace Octobot.Commands; [UsedImplicitly] public class RemindCommandGroup : CommandGroup { - private readonly ICommandContext _context; + private readonly IInteractionCommandContext _context; private readonly IFeedbackService _feedback; private readonly GuildDataService _guildData; private readonly IDiscordRestUserAPI _userApi; + private readonly IDiscordRestInteractionAPI _interactionApi; public RemindCommandGroup( - ICommandContext context, GuildDataService guildData, IFeedbackService feedback, - IDiscordRestUserAPI userApi) + IInteractionCommandContext context, GuildDataService guildData, IFeedbackService feedback, + IDiscordRestUserAPI userApi, IDiscordRestInteractionAPI interactionApi) { _context = context; _guildData = guildData; _feedback = feedback; _userApi = userApi; + _interactionApi = interactionApi; } /// @@ -72,10 +74,10 @@ public class RemindCommandGroup : CommandGroup var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); - return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), executor, bot, CancellationToken); + return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), guildId, executor, bot, CancellationToken); } - private Task ListRemindersAsync(MemberData data, IUser executor, IUser bot, CancellationToken ct) + private Task ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct) { if (data.Reminders.Count == 0) { @@ -92,7 +94,8 @@ public class RemindCommandGroup : CommandGroup var reminder = data.Reminders[i]; builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString()))) .AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text))) - .AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At))); + .AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At))) + .AppendSubBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}")); } var embed = new EmbedBuilder().WithSmallTitle( @@ -139,25 +142,29 @@ public class RemindCommandGroup : CommandGroup return await AddReminderAsync(@in, text, data, channelId, executor, CancellationToken); } - private Task AddReminderAsync( - TimeSpan @in, string text, GuildData data, + private async Task AddReminderAsync(TimeSpan @in, string text, GuildData data, Snowflake channelId, IUser executor, CancellationToken ct = default) { - var remindAt = DateTimeOffset.UtcNow.Add(@in); var memberData = data.GetOrCreateMemberData(executor.ID); + var remindAt = DateTimeOffset.UtcNow.Add(@in); + var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct); + if (!responseResult.IsDefined(out var response)) + { + return (Result)responseResult; + } memberData.Reminders.Add( new Reminder { At = remindAt, - Channel = channelId.Value, - Text = text + ChannelId = channelId.Value, + Text = text, + MessageId = response.ID.Value }); - var builder = new StringBuilder().AppendBulletPointLine(string.Format( - Messages.ReminderText, Markdown.InlineCode(text))) + var builder = new StringBuilder() + .AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(text))) .AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt))); - var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.ReminderCreated, executor.GetTag()), executor) .WithDescription(builder.ToString()) @@ -165,7 +172,7 @@ public class RemindCommandGroup : CommandGroup .WithFooter(string.Format(Messages.ReminderPosition, memberData.Reminders.Count)) .Build(); - return _feedback.SendContextualEmbedResultAsync(embed, ct); + return await _feedback.SendContextualEmbedResultAsync(embed, ct); } /// diff --git a/src/Data/Reminder.cs b/src/Data/Reminder.cs index 42144f9..f21b222 100644 --- a/src/Data/Reminder.cs +++ b/src/Data/Reminder.cs @@ -4,5 +4,6 @@ public struct Reminder { public DateTimeOffset At { get; init; } public string Text { get; init; } - public ulong Channel { get; init; } + public ulong ChannelId { get; init; } + public ulong MessageId { get; init; } } diff --git a/src/Services/Update/MemberUpdateService.cs b/src/Services/Update/MemberUpdateService.cs index b4289ce..c3139c3 100644 --- a/src/Services/Update/MemberUpdateService.cs +++ b/src/Services/Update/MemberUpdateService.cs @@ -1,3 +1,4 @@ +using System.Text; using System.Text.RegularExpressions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -114,7 +115,7 @@ public sealed partial class MemberUpdateService : BackgroundService for (var i = data.Reminders.Count - 1; i >= 0; i--) { - var reminderTickResult = await TickReminderAsync(data.Reminders[i], user, data, ct); + var reminderTickResult = await TickReminderAsync(data.Reminders[i], user, data, guildId, ct); failedResults.AddIfFailed(reminderTickResult); } @@ -217,17 +218,21 @@ public sealed partial class MemberUpdateService : BackgroundService [GeneratedRegex("[^0-9A-Za-zА-Яа-яЁё]")] private static partial Regex IllegalChars(); - private async Task TickReminderAsync(Reminder reminder, IUser user, MemberData data, CancellationToken ct) + private async Task TickReminderAsync(Reminder reminder, IUser user, MemberData data, Snowflake guildId, + CancellationToken ct) { if (DateTimeOffset.UtcNow < reminder.At) { return Result.FromSuccess(); } + var builder = new StringBuilder() + .AppendBulletPointLine(string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text))) + .AppendBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}")); + var embed = new EmbedBuilder().WithSmallTitle( string.Format(Messages.Reminder, user.GetTag()), user) - .WithDescription( - string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text))) + .WithDescription(builder.ToString()) .WithColour(ColorsList.Magenta) .Build(); @@ -237,7 +242,7 @@ public sealed partial class MemberUpdateService : BackgroundService } var messageResult = await _channelApi.CreateMessageAsync( - reminder.Channel.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct); + reminder.ChannelId.ToSnowflake(), Mention.User(user), embeds: new[] { built }, ct: ct); if (!messageResult.IsSuccess) { return Result.FromError(messageResult);