2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
2023-10-28 21:10:16 +03:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-08-12 16:54:51 +03:00
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Commands.Attributes;
|
|
|
|
using Remora.Commands.Groups;
|
2023-07-20 23:41:02 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.Commands.Attributes;
|
2023-07-18 17:18:35 +03:00
|
|
|
using Remora.Discord.Commands.Conditions;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
2023-07-20 23:41:02 +03:00
|
|
|
using Remora.Rest.Core;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Results;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Data;
|
|
|
|
using TeamOctolings.Octobot.Extensions;
|
|
|
|
using TeamOctolings.Octobot.Parsers;
|
|
|
|
using TeamOctolings.Octobot.Services;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot.Commands;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <summary>
|
2023-09-29 18:36:16 +03:00
|
|
|
/// Handles commands to manage reminders: /remind, /listremind, /delremind
|
2023-07-09 16:32:14 +03:00
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2024-05-23 15:47:51 +03:00
|
|
|
public sealed class RemindCommandGroup : CommandGroup
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-12-11 23:38:26 +03:00
|
|
|
private readonly IInteractionCommandContext _context;
|
2023-11-04 21:28:22 +03:00
|
|
|
private readonly IFeedbackService _feedback;
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly GuildDataService _guildData;
|
2023-12-11 23:38:26 +03:00
|
|
|
private readonly IDiscordRestInteractionAPI _interactionApi;
|
2024-05-23 15:47:51 +03:00
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
public RemindCommandGroup(
|
2023-12-11 23:38:26 +03:00
|
|
|
IInteractionCommandContext context, GuildDataService guildData, IFeedbackService feedback,
|
|
|
|
IDiscordRestUserAPI userApi, IDiscordRestInteractionAPI interactionApi)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_context = context;
|
2023-08-02 23:51:16 +03:00
|
|
|
_guildData = guildData;
|
|
|
|
_feedback = feedback;
|
2023-07-09 16:32:14 +03:00
|
|
|
_userApi = userApi;
|
2023-12-11 23:38:26 +03:00
|
|
|
_interactionApi = interactionApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
/// <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()
|
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId))
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
|
|
|
if (!executorResult.IsDefined(out var executor))
|
2023-08-14 10:45:56 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(executorResult);
|
2023-08-14 10:45:56 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
2023-12-11 23:38:26 +03:00
|
|
|
return await ListRemindersAsync(data.GetOrCreateMemberData(executorId), guildId, executor, bot, CancellationToken);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-12-11 23:38:26 +03:00
|
|
|
private Task<Result> ListRemindersAsync(MemberData data, Snowflake guildId, IUser executor, IUser bot, CancellationToken ct)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
2023-08-14 10:45:56 +03:00
|
|
|
if (data.Reminders.Count == 0)
|
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.NoRemindersFound, bot)
|
2023-08-14 10:45:56 +03:00
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct);
|
2023-08-14 10:45:56 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
var builder = new StringBuilder();
|
2023-10-06 16:12:36 +03:00
|
|
|
for (var i = 0; i < data.Reminders.Count; i++)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
|
|
|
var reminder = data.Reminders[i];
|
2023-12-04 17:09:47 +03:00
|
|
|
builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
|
|
|
|
.AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
|
2023-12-11 23:38:26 +03:00
|
|
|
.AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)))
|
|
|
|
.AppendSubBulletPointLine(string.Format(Messages.DescriptionActionJumpToMessage, $"https://discord.com/channels/{guildId.Value}/{reminder.ChannelId}/{reminder.MessageId}"));
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
2023-10-04 18:21:10 +03:00
|
|
|
string.Format(Messages.ReminderList, executor.GetTag()), executor)
|
2023-08-12 16:54:51 +03:00
|
|
|
.WithDescription(builder.ToString())
|
|
|
|
.WithColour(ColorsList.Cyan)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-07-09 18:15:39 +03:00
|
|
|
/// <summary>
|
|
|
|
/// A slash command that schedules a reminder with the specified text.
|
|
|
|
/// </summary>
|
2023-12-31 15:27:00 +03:00
|
|
|
/// <param name="timeSpanString">The period of time which must pass before the reminder will be sent.</param>
|
2023-10-06 17:20:18 +03:00
|
|
|
/// <param name="text">The text of the reminder.</param>
|
2023-07-09 18:15:39 +03:00
|
|
|
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
2023-07-09 16:32:14 +03:00
|
|
|
[Command("remind")]
|
|
|
|
[Description("Create a reminder")]
|
2023-07-18 17:18:35 +03:00
|
|
|
[DiscordDefaultDMPermission(false)]
|
|
|
|
[RequireContext(ChannelContext.Guild)]
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-07-20 23:41:02 +03:00
|
|
|
public async Task<Result> ExecuteReminderAsync(
|
2024-03-17 16:46:53 +03:00
|
|
|
[Description("After what period of time mention the reminder (e.g. 1h30m)")]
|
2023-12-31 15:27:00 +03:00
|
|
|
[Option("in")]
|
|
|
|
string timeSpanString,
|
2023-10-28 21:10:16 +03:00
|
|
|
[Description("Reminder text")] [MaxLength(512)]
|
|
|
|
string text)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var executorId))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-12-31 15:27:00 +03:00
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2023-12-31 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
|
|
|
if (!executorResult.IsDefined(out var executor))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(executorResult);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
2023-12-31 15:27:00 +03:00
|
|
|
var parseResult = TimeSpanParser.TryParse(timeSpanString);
|
|
|
|
if (!parseResult.IsDefined(out var timeSpan))
|
|
|
|
{
|
|
|
|
var failedEmbed = new EmbedBuilder()
|
|
|
|
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
|
2024-03-17 16:46:53 +03:00
|
|
|
.WithDescription(Messages.TimeSpanExample)
|
2023-12-31 15:27:00 +03:00
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await AddReminderAsync(timeSpan, text, data, channelId, executor, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-31 15:27:00 +03:00
|
|
|
private async Task<Result> AddReminderAsync(TimeSpan timeSpan, string text, GuildData data,
|
2023-10-04 18:21:10 +03:00
|
|
|
Snowflake channelId, IUser executor, CancellationToken ct = default)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-10-06 17:17:48 +03:00
|
|
|
var memberData = data.GetOrCreateMemberData(executor.ID);
|
2023-12-31 15:27:00 +03:00
|
|
|
var remindAt = DateTimeOffset.UtcNow.Add(timeSpan);
|
2023-12-11 23:38:26 +03:00
|
|
|
var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct);
|
|
|
|
if (!responseResult.IsDefined(out var response))
|
|
|
|
{
|
|
|
|
return (Result)responseResult;
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-10-06 17:17:48 +03:00
|
|
|
memberData.Reminders.Add(
|
2023-08-02 23:51:16 +03:00
|
|
|
new Reminder
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
At = remindAt,
|
2023-12-11 23:38:26 +03:00
|
|
|
ChannelId = channelId.Value,
|
|
|
|
Text = text,
|
|
|
|
MessageId = response.ID.Value
|
2023-07-09 16:32:14 +03:00
|
|
|
});
|
|
|
|
|
2023-12-11 23:38:26 +03:00
|
|
|
var builder = new StringBuilder()
|
|
|
|
.AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(text)))
|
2023-12-04 17:09:47 +03:00
|
|
|
.AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt)));
|
2023-09-29 18:36:16 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
2023-10-04 18:21:10 +03:00
|
|
|
string.Format(Messages.ReminderCreated, executor.GetTag()), executor)
|
2023-09-29 18:36:16 +03:00
|
|
|
.WithDescription(builder.ToString())
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Green)
|
2023-10-06 17:17:48 +03:00
|
|
|
.WithFooter(string.Format(Messages.ReminderPosition, memberData.Reminders.Count))
|
2023-07-09 16:32:14 +03:00
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
2023-08-12 16:54:51 +03:00
|
|
|
|
2024-01-27 15:50:27 +03:00
|
|
|
public enum Parameters
|
|
|
|
{
|
|
|
|
[UsedImplicitly] Time,
|
|
|
|
[UsedImplicitly] Text
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A slash command that edits a scheduled reminder using the specified text or time.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="position">The list position of the reminder to edit.</param>
|
|
|
|
/// <param name="parameter">The reminder's parameter to edit.</param>
|
|
|
|
/// <param name="value">The new value for the reminder as a text or time.</param>
|
|
|
|
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
|
|
|
[Command("editremind")]
|
|
|
|
[Description("Edit a reminder")]
|
|
|
|
[DiscordDefaultDMPermission(false)]
|
|
|
|
[RequireContext(ChannelContext.Guild)]
|
|
|
|
[UsedImplicitly]
|
|
|
|
public async Task<Result> 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))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2024-01-27 15:50:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
|
|
|
if (!executorResult.IsDefined(out var executor))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(executorResult);
|
2024-01-27 15:50:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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<Result> 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)
|
2024-03-17 16:46:53 +03:00
|
|
|
.WithDescription(Messages.TimeSpanExample)
|
2024-01-27 15:50:27 +03:00
|
|
|
.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<Result> 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);
|
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
/// <summary>
|
2023-10-06 17:17:48 +03:00
|
|
|
/// A slash command that deletes a reminder using its list position.
|
2023-08-12 16:54:51 +03:00
|
|
|
/// </summary>
|
2023-10-06 17:17:48 +03:00
|
|
|
/// <param name="position">The list position of the reminder to delete.</param>
|
2023-08-12 16:54:51 +03:00
|
|
|
/// <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(
|
2023-10-06 17:17:48 +03:00
|
|
|
[Description("Position in list")] [MinValue(1)]
|
|
|
|
int position)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
2023-10-04 18:21:10 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out _, out var executorId))
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
|
|
|
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
|
|
|
|
}
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
2023-10-06 17:17:48 +03:00
|
|
|
return await DeleteReminderAsync(data.GetOrCreateMemberData(executorId), position - 1, bot, CancellationToken);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-12-05 22:24:55 +03:00
|
|
|
private Task<Result> DeleteReminderAsync(MemberData data, int index, IUser bot,
|
2023-08-12 16:54:51 +03:00
|
|
|
CancellationToken ct)
|
|
|
|
{
|
|
|
|
if (index >= data.Reminders.Count)
|
|
|
|
{
|
2023-10-06 17:17:48 +03:00
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.InvalidReminderPosition, bot)
|
2023-08-12 16:54:51 +03:00
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:41:47 +03:00
|
|
|
var reminder = data.Reminders[index];
|
|
|
|
|
|
|
|
var description = new StringBuilder()
|
2023-12-04 17:09:47 +03:00
|
|
|
.AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
|
|
|
|
.AppendBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
|
2023-10-26 17:41:47 +03:00
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
data.Reminders.RemoveAt(index);
|
|
|
|
|
2023-10-04 18:21:10 +03:00
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.ReminderDeleted, bot)
|
2023-10-26 17:41:47 +03:00
|
|
|
.WithDescription(description.ToString())
|
2023-08-12 16:54:51 +03:00
|
|
|
.WithColour(ColorsList.Green)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 18:44:18 +03:00
|
|
|
return _feedback.SendContextualEmbedResultAsync(embed, ct: ct);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|