Add link to original message for activated reminders and /listremind (#203)

This commit is contained in:
neroduckale 2023-12-12 01:38:26 +05:00 committed by GitHub
parent e21e193218
commit 59c2c5eada
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 21 deletions

View file

@ -26,19 +26,21 @@ namespace Octobot.Commands;
[UsedImplicitly] [UsedImplicitly]
public class RemindCommandGroup : CommandGroup public class RemindCommandGroup : CommandGroup
{ {
private readonly ICommandContext _context; private readonly IInteractionCommandContext _context;
private readonly IFeedbackService _feedback; private readonly IFeedbackService _feedback;
private readonly GuildDataService _guildData; private readonly GuildDataService _guildData;
private readonly IDiscordRestUserAPI _userApi; private readonly IDiscordRestUserAPI _userApi;
private readonly IDiscordRestInteractionAPI _interactionApi;
public RemindCommandGroup( public RemindCommandGroup(
ICommandContext context, GuildDataService guildData, IFeedbackService feedback, IInteractionCommandContext context, GuildDataService guildData, IFeedbackService feedback,
IDiscordRestUserAPI userApi) IDiscordRestUserAPI userApi, IDiscordRestInteractionAPI interactionApi)
{ {
_context = context; _context = context;
_guildData = guildData; _guildData = guildData;
_feedback = feedback; _feedback = feedback;
_userApi = userApi; _userApi = userApi;
_interactionApi = interactionApi;
} }
/// <summary> /// <summary>
@ -72,10 +74,10 @@ public class RemindCommandGroup : CommandGroup
var data = await _guildData.GetData(guildId, CancellationToken); var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings); 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<Result> ListRemindersAsync(MemberData data, IUser executor, IUser bot, CancellationToken ct) private Task<Result> ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct)
{ {
if (data.Reminders.Count == 0) if (data.Reminders.Count == 0)
{ {
@ -92,7 +94,8 @@ public class RemindCommandGroup : CommandGroup
var reminder = data.Reminders[i]; var reminder = data.Reminders[i];
builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString()))) builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
.AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text))) .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( var embed = new EmbedBuilder().WithSmallTitle(
@ -139,25 +142,29 @@ public class RemindCommandGroup : CommandGroup
return await AddReminderAsync(@in, text, data, channelId, executor, CancellationToken); return await AddReminderAsync(@in, text, data, channelId, executor, CancellationToken);
} }
private Task<Result> AddReminderAsync( private async Task<Result> AddReminderAsync(TimeSpan @in, string text, GuildData data,
TimeSpan @in, string text, GuildData data,
Snowflake channelId, IUser executor, CancellationToken ct = default) Snowflake channelId, IUser executor, CancellationToken ct = default)
{ {
var remindAt = DateTimeOffset.UtcNow.Add(@in);
var memberData = data.GetOrCreateMemberData(executor.ID); 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( memberData.Reminders.Add(
new Reminder new Reminder
{ {
At = remindAt, At = remindAt,
Channel = channelId.Value, ChannelId = channelId.Value,
Text = text Text = text,
MessageId = response.ID.Value
}); });
var builder = new StringBuilder().AppendBulletPointLine(string.Format( var builder = new StringBuilder()
Messages.ReminderText, Markdown.InlineCode(text))) .AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(text)))
.AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt))); .AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt)));
var embed = new EmbedBuilder().WithSmallTitle( var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.ReminderCreated, executor.GetTag()), executor) string.Format(Messages.ReminderCreated, executor.GetTag()), executor)
.WithDescription(builder.ToString()) .WithDescription(builder.ToString())
@ -165,7 +172,7 @@ public class RemindCommandGroup : CommandGroup
.WithFooter(string.Format(Messages.ReminderPosition, memberData.Reminders.Count)) .WithFooter(string.Format(Messages.ReminderPosition, memberData.Reminders.Count))
.Build(); .Build();
return _feedback.SendContextualEmbedResultAsync(embed, ct); return await _feedback.SendContextualEmbedResultAsync(embed, ct);
} }
/// <summary> /// <summary>

View file

@ -4,5 +4,6 @@ public struct Reminder
{ {
public DateTimeOffset At { get; init; } public DateTimeOffset At { get; init; }
public string Text { get; init; } public string Text { get; init; }
public ulong Channel { get; init; } public ulong ChannelId { get; init; }
public ulong MessageId { get; init; }
} }

View file

@ -1,3 +1,4 @@
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -114,7 +115,7 @@ public sealed partial class MemberUpdateService : BackgroundService
for (var i = data.Reminders.Count - 1; i >= 0; i--) 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); failedResults.AddIfFailed(reminderTickResult);
} }
@ -217,17 +218,21 @@ public sealed partial class MemberUpdateService : BackgroundService
[GeneratedRegex("[^0-9A-Za-zА-Яа-яЁё]")] [GeneratedRegex("[^0-9A-Za-zА-Яа-яЁё]")]
private static partial Regex IllegalChars(); private static partial Regex IllegalChars();
private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, CancellationToken ct) private async Task<Result> TickReminderAsync(Reminder reminder, IUser user, MemberData data, Snowflake guildId,
CancellationToken ct)
{ {
if (DateTimeOffset.UtcNow < reminder.At) if (DateTimeOffset.UtcNow < reminder.At)
{ {
return Result.FromSuccess(); 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( var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.Reminder, user.GetTag()), user) string.Format(Messages.Reminder, user.GetTag()), user)
.WithDescription( .WithDescription(builder.ToString())
string.Format(Messages.DescriptionReminder, Markdown.InlineCode(reminder.Text)))
.WithColour(ColorsList.Magenta) .WithColour(ColorsList.Magenta)
.Build(); .Build();
@ -237,7 +242,7 @@ public sealed partial class MemberUpdateService : BackgroundService
} }
var messageResult = await _channelApi.CreateMessageAsync( 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) if (!messageResult.IsSuccess)
{ {
return Result.FromError(messageResult); return Result.FromError(messageResult);