1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

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 <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-08-12 18:54:51 +05:00 committed by GitHub
parent cac3ee9bf7
commit 488a1eec0c
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 146 additions and 0 deletions

View file

@ -582,4 +582,13 @@
<data name="Previous" xml:space="preserve"> <data name="Previous" xml:space="preserve">
<value>Previous</value> <value>Previous</value>
</data> </data>
<data name="ReminderList" xml:space="preserve">
<value>{0}'s reminders</value>
</data>
<data name="InvalidReminderIndex" xml:space="preserve">
<value>There's no reminder with that index!</value>
</data>
<data name="ReminderDeleted" xml:space="preserve">
<value>Reminder deleted</value>
</data>
</root> </root>

View file

@ -582,4 +582,13 @@
<data name="Previous" xml:space="preserve"> <data name="Previous" xml:space="preserve">
<value>Назад</value> <value>Назад</value>
</data> </data>
<data name="ReminderList" xml:space="preserve">
<value>Напоминания {0}</value>
</data>
<data name="InvalidReminderIndex" xml:space="preserve">
<value>У тебя нет напоминания с указанным индексом!</value>
</data>
<data name="ReminderDeleted" xml:space="preserve">
<value>Напоминание удалено</value>
</data>
</root> </root>

View file

@ -582,4 +582,13 @@
<data name="Previous" xml:space="preserve"> <data name="Previous" xml:space="preserve">
<value>предыдущее</value> <value>предыдущее</value>
</data> </data>
<data name="ReminderList" xml:space="preserve">
<value>напоминалки {0}</value>
</data>
<data name="InvalidReminderIndex" xml:space="preserve">
<value>у тебя нет напоминалки с этим индексом!</value>
</data>
<data name="ReminderDeleted" xml:space="preserve">
<value>напоминалка уничтожена</value>
</data>
</root> </root>

View file

@ -1,4 +1,5 @@
using System.ComponentModel; using System.ComponentModel;
using System.Text;
using Boyfriend.Data; using Boyfriend.Data;
using Boyfriend.Services; using Boyfriend.Services;
using JetBrains.Annotations; using JetBrains.Annotations;
@ -38,6 +39,54 @@ public class RemindCommandGroup : CommandGroup
_userApi = userApi; _userApi = userApi;
} }
/// <summary>
/// A slash command that lists reminders of the user that called it.
/// </summary>
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
[Command("listremind")]
[Description("List your reminders")]
[DiscordDefaultDMPermission(false)]
[RequireContext(ChannelContext.Guild)]
[UsedImplicitly]
public async Task<Result> 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<Result> 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);
}
/// <summary> /// <summary>
/// A slash command that schedules a reminder with the specified text. /// A slash command that schedules a reminder with the specified text.
/// </summary> /// </summary>
@ -92,4 +141,56 @@ public class RemindCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct); return await _feedback.SendContextualEmbedResultAsync(embed, ct);
} }
/// <summary>
/// A slash command that deletes a reminder using its index.
/// </summary>
/// <param name="index">The index of the reminder to delete.</param>
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
[Command("delremind")]
[Description("Delete one of your reminders")]
[DiscordDefaultDMPermission(false)]
[RequireContext(ChannelContext.Guild)]
[UsedImplicitly]
public async Task<Result> 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<Result> 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);
}
} }

View file

@ -977,5 +977,23 @@ namespace Boyfriend {
return ResourceManager.GetString("Previous", resourceCulture); 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);
}
}
} }
} }