From 488a1eec0ce5925802995af7644f2f8b7cead269 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 12 Aug 2023 18:54:51 +0500 Subject: [PATCH] Add /listremind and /delremind (#82) This PR closes #61. This PR adds the `/listremind` command and the `/delremind` command. The `/delremind` command requires an index to determine which reminder to delete. cc @mctaylors review embed design and feature experience --------- Signed-off-by: Octol1ttle --- locale/Messages.resx | 9 +++ locale/Messages.ru.resx | 9 +++ locale/Messages.tt-ru.resx | 9 +++ src/Commands/RemindCommandGroup.cs | 101 +++++++++++++++++++++++++++++ src/Messages.Designer.cs | 18 +++++ 5 files changed, 146 insertions(+) diff --git a/locale/Messages.resx b/locale/Messages.resx index f463a3b..ddb0f06 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -582,4 +582,13 @@ Previous + + {0}'s reminders + + + There's no reminder with that index! + + + Reminder deleted + diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index d045bc3..0cb87fb 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -582,4 +582,13 @@ Назад + + Напоминания {0} + + + У тебя нет напоминания с указанным индексом! + + + Напоминание удалено + diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index 1909d27..5b0b798 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -582,4 +582,13 @@ предыдущее + + напоминалки {0} + + + у тебя нет напоминалки с этим индексом! + + + напоминалка уничтожена + diff --git a/src/Commands/RemindCommandGroup.cs b/src/Commands/RemindCommandGroup.cs index cabbbeb..d5110e0 100644 --- a/src/Commands/RemindCommandGroup.cs +++ b/src/Commands/RemindCommandGroup.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Text; using Boyfriend.Data; using Boyfriend.Services; using JetBrains.Annotations; @@ -38,6 +39,54 @@ public class RemindCommandGroup : CommandGroup _userApi = userApi; } + /// + /// A slash command that lists reminders of the user that called it. + /// + /// A feedback sending result which may or may not have succeeded. + [Command("listremind")] + [Description("List your reminders")] + [DiscordDefaultDMPermission(false)] + [RequireContext(ChannelContext.Guild)] + [UsedImplicitly] + public async Task ExecuteListReminderAsync() + { + if (!_context.TryGetContextIDs(out var guildId, out _, out var userId)) + { + return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + } + + var userResult = await _userApi.GetUserAsync(userId, CancellationToken); + if (!userResult.IsDefined(out var user)) + { + return Result.FromError(userResult); + } + + var data = await _guildData.GetData(guildId, CancellationToken); + Messages.Culture = GuildSettings.Language.Get(data.Settings); + + return await ListRemindersAsync(data.GetOrCreateMemberData(userId), user, CancellationToken); + } + + private async Task ListRemindersAsync(MemberData data, IUser user, CancellationToken ct) + { + var builder = new StringBuilder(); + for (var i = 0; i < data.Reminders.Count; i++) + { + var reminder = data.Reminders[i]; + builder.AppendLine( + $"- {Markdown.InlineCode(i.ToString())} - {Markdown.InlineCode(reminder.Text)} - {Markdown.Timestamp(reminder.At)}"); + } + + var embed = new EmbedBuilder().WithSmallTitle( + string.Format(Messages.ReminderList, user.GetTag()), user) + .WithDescription(builder.ToString()) + .WithColour(ColorsList.Cyan) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync( + embed, ct); + } + /// /// A slash command that schedules a reminder with the specified text. /// @@ -92,4 +141,56 @@ public class RemindCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, ct); } + + /// + /// A slash command that deletes a reminder using its index. + /// + /// The index of the reminder to delete. + /// A feedback sending result which may or may not have succeeded. + [Command("delremind")] + [Description("Delete one of your reminders")] + [DiscordDefaultDMPermission(false)] + [RequireContext(ChannelContext.Guild)] + [UsedImplicitly] + public async Task ExecuteDeleteReminderAsync( + [MinValue(0)] int index) + { + if (!_context.TryGetContextIDs(out var guildId, out _, out var userId)) + { + return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context"); + } + + var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken); + if (!currentUserResult.IsDefined(out var currentUser)) + { + return Result.FromError(currentUserResult); + } + + var data = await _guildData.GetData(guildId, CancellationToken); + Messages.Culture = GuildSettings.Language.Get(data.Settings); + + return await DeleteReminderAsync(data.GetOrCreateMemberData(userId), index, currentUser, CancellationToken); + } + + private async Task DeleteReminderAsync(MemberData data, int index, IUser currentUser, + CancellationToken ct) + { + if (index >= data.Reminders.Count) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.InvalidReminderIndex, currentUser) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct); + } + + data.Reminders.RemoveAt(index); + + var embed = new EmbedBuilder().WithSmallTitle(Messages.ReminderDeleted, currentUser) + .WithColour(ColorsList.Green) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync( + embed, ct); + } } diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index 1f6fbc7..b4509e8 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -977,5 +977,23 @@ namespace Boyfriend { return ResourceManager.GetString("Previous", resourceCulture); } } + + internal static string ReminderList { + get { + return ResourceManager.GetString("ReminderList", resourceCulture); + } + } + + internal static string InvalidReminderIndex { + get { + return ResourceManager.GetString("InvalidReminderIndex", resourceCulture); + } + } + + internal static string ReminderDeleted { + get { + return ResourceManager.GetString("ReminderDeleted", resourceCulture); + } + } } }