From af84f8853ad4bffde0ede8b89256d508d958ecdb Mon Sep 17 00:00:00 2001 From: Macintxsh <95250141+mctaylors@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:50:27 +0300 Subject: [PATCH] Add /editremind (#247) In this PR, I'm adding a command to modify existing reminders. This can be useful if you, for example, got the name a bit wrong or set the wrong reminder time. Just use /editremind and recreating the reminder from scratch will no longer be necessary. --------- Signed-off-by: mctaylors --- locale/Messages.resx | 3 + locale/Messages.ru.resx | 3 + locale/Messages.tt-ru.resx | 3 + src/Commands/RemindCommandGroup.cs | 126 +++++++++++++++++++++++++++++ src/Messages.Designer.cs | 6 ++ 5 files changed, 141 insertions(+) diff --git a/locale/Messages.resx b/locale/Messages.resx index adc9f6d..218c414 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -591,4 +591,7 @@ Kicked + + Reminder edited + diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index de2158d..3eb53f1 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -591,4 +591,7 @@ Выгнан + + Напоминание отредактировано + diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index ca3c19d..f5d789b 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -591,4 +591,7 @@ кикнут + + напоминалка подправлена + diff --git a/src/Commands/RemindCommandGroup.cs b/src/Commands/RemindCommandGroup.cs index 5e8c9c5..c270f30 100644 --- a/src/Commands/RemindCommandGroup.cs +++ b/src/Commands/RemindCommandGroup.cs @@ -193,6 +193,132 @@ public class RemindCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); } + public enum Parameters + { + [UsedImplicitly] Time, + [UsedImplicitly] Text + } + + /// + /// A slash command that edits a scheduled reminder using the specified text or time. + /// + /// The list position of the reminder to edit. + /// The reminder's parameter to edit. + /// The new value for the reminder as a text or time. + /// A feedback sending result which may or may not have succeeded. + [Command("editremind")] + [Description("Edit a reminder")] + [DiscordDefaultDMPermission(false)] + [RequireContext(ChannelContext.Guild)] + [UsedImplicitly] + public async Task ExecuteEditReminderAsync( + [Description("Position in list")] [MinValue(1)] + int position, + [Description("Parameter to edit")] Parameters parameter, + [Description("Parameter's new value")] string value) + { + if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId)) + { + return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + } + + var botResult = await _userApi.GetCurrentUserAsync(CancellationToken); + if (!botResult.IsDefined(out var bot)) + { + return Result.FromError(botResult); + } + + var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken); + if (!executorResult.IsDefined(out var executor)) + { + return Result.FromError(executorResult); + } + + var data = await _guildData.GetData(guildId, CancellationToken); + Messages.Culture = GuildSettings.Language.Get(data.Settings); + + var memberData = data.GetOrCreateMemberData(executor.ID); + + if (parameter is Parameters.Time) + { + return await EditReminderTimeAsync(position - 1, value, memberData, bot, executor, CancellationToken); + } + + return await EditReminderTextAsync(position - 1, value, memberData, bot, executor, CancellationToken); + } + + private async Task EditReminderTimeAsync(int index, string value, MemberData data, + IUser bot, IUser executor, CancellationToken ct = default) + { + if (index >= data.Reminders.Count) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.InvalidReminderPosition, bot) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + } + + var parseResult = TimeSpanParser.TryParse(value); + if (!parseResult.IsDefined(out var timeSpan)) + { + var failedEmbed = new EmbedBuilder() + .WithSmallTitle(Messages.InvalidTimeSpan, bot) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + } + + var oldReminder = data.Reminders[index]; + var remindAt = DateTimeOffset.UtcNow.Add(timeSpan); + + data.Reminders.Add(oldReminder with { At = remindAt }); + data.Reminders.RemoveAt(index); + + var builder = new StringBuilder() + .AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(oldReminder.Text))) + .AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt))); + var embed = new EmbedBuilder().WithSmallTitle( + string.Format(Messages.ReminderEdited, executor.GetTag()), executor) + .WithDescription(builder.ToString()) + .WithColour(ColorsList.Cyan) + .WithFooter(string.Format(Messages.ReminderPosition, data.Reminders.Count)) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + } + + private async Task EditReminderTextAsync(int index, string value, MemberData data, + IUser bot, IUser executor, CancellationToken ct = default) + { + if (index >= data.Reminders.Count) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.InvalidReminderPosition, bot) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + } + + var oldReminder = data.Reminders[index]; + + data.Reminders.Add(oldReminder with { Text = value }); + data.Reminders.RemoveAt(index); + + var builder = new StringBuilder() + .AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(value))) + .AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(oldReminder.At))); + var embed = new EmbedBuilder().WithSmallTitle( + string.Format(Messages.ReminderEdited, executor.GetTag()), executor) + .WithDescription(builder.ToString()) + .WithColour(ColorsList.Cyan) + .WithFooter(string.Format(Messages.ReminderPosition, data.Reminders.Count)) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); + } + /// /// A slash command that deletes a reminder using its list position. /// diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index f5a06c0..a0c915a 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -1052,5 +1052,11 @@ namespace Octobot { return ResourceManager.GetString("UserInfoKicked", resourceCulture); } } + + internal static string ReminderEdited { + get { + return ResourceManager.GetString("ReminderEdited", resourceCulture); + } + } } }