2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
2023-08-12 16:54:51 +03:00
|
|
|
using System.Text;
|
2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
|
|
|
using Octobot.Services;
|
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;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace 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]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class RemindCommandGroup : CommandGroup
|
|
|
|
{
|
|
|
|
private readonly ICommandContext _context;
|
|
|
|
private readonly FeedbackService _feedback;
|
|
|
|
private readonly GuildDataService _guildData;
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
|
|
|
|
|
|
|
public RemindCommandGroup(
|
2023-08-02 23:51:16 +03:00
|
|
|
ICommandContext context, GuildDataService guildData, FeedbackService feedback,
|
|
|
|
IDiscordRestUserAPI userApi)
|
|
|
|
{
|
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-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()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:45:56 +03:00
|
|
|
var currentUserResult = await _userApi.GetCurrentUserAsync(CancellationToken);
|
|
|
|
if (!currentUserResult.IsDefined(out var currentUser))
|
|
|
|
{
|
|
|
|
return Result.FromError(currentUserResult);
|
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
var data = await _guildData.GetData(guildId, CancellationToken);
|
|
|
|
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
|
|
|
|
2023-08-14 10:45:56 +03:00
|
|
|
return await ListRemindersAsync(data.GetOrCreateMemberData(userId), user, currentUser, CancellationToken);
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:45:56 +03:00
|
|
|
private async Task<Result> ListRemindersAsync(MemberData data, IUser user, IUser currentUser, CancellationToken ct)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
2023-08-14 10:45:56 +03:00
|
|
|
if (data.Reminders.Count == 0)
|
|
|
|
{
|
|
|
|
var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.NoRemindersFound, currentUser)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct);
|
|
|
|
}
|
|
|
|
|
2023-08-12 16:54:51 +03:00
|
|
|
var builder = new StringBuilder();
|
2023-09-12 16:28:46 +03:00
|
|
|
for (var i = data.Reminders.Count - 1; i >= 0; i--)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
|
|
|
var reminder = data.Reminders[i];
|
2023-09-29 18:36:16 +03:00
|
|
|
builder.Append("- ").AppendLine(string.Format(Messages.ReminderIndex, Markdown.InlineCode(i.ToString())))
|
|
|
|
.Append(" - ").AppendLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
|
|
|
|
.Append(" - ").AppendLine(string.Format(Messages.ReminderWillBeSentOn, Markdown.Timestamp(reminder.At)));
|
2023-08-12 16:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-09 18:15:39 +03:00
|
|
|
/// <summary>
|
|
|
|
/// A slash command that schedules a reminder with the specified text.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="in">The period of time which must pass before the reminder will be sent.</param>
|
|
|
|
/// <param name="message">The text of the reminder.</param>
|
|
|
|
/// <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(
|
2023-07-09 18:15:39 +03:00
|
|
|
[Description("After what period of time mention the reminder")]
|
|
|
|
TimeSpan @in,
|
2023-08-02 23:51:16 +03:00
|
|
|
[Description("Reminder message")] string message)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!_context.TryGetContextIDs(out var guildId, out var channelId, out var userId))
|
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-07-24 14:57:41 +03:00
|
|
|
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (!userResult.IsDefined(out var user))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Result.FromError(userResult);
|
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-07-24 14:57:41 +03:00
|
|
|
return await AddReminderAsync(@in, message, data, channelId, user, CancellationToken);
|
2023-07-20 23:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<Result> AddReminderAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
TimeSpan @in, string message, GuildData data,
|
|
|
|
Snowflake channelId, IUser user, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 18:15:39 +03:00
|
|
|
var remindAt = DateTimeOffset.UtcNow.Add(@in);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-04 16:52:54 +03:00
|
|
|
data.GetOrCreateMemberData(user.ID).Reminders.Add(
|
2023-08-02 23:51:16 +03:00
|
|
|
new Reminder
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
At = remindAt,
|
2023-07-20 23:41:02 +03:00
|
|
|
Channel = channelId.Value,
|
2023-07-09 18:15:39 +03:00
|
|
|
Text = message
|
2023-07-09 16:32:14 +03:00
|
|
|
});
|
|
|
|
|
2023-09-29 18:36:16 +03:00
|
|
|
var builder = new StringBuilder().Append("- ").AppendLine(string.Format(
|
|
|
|
Messages.ReminderText, Markdown.InlineCode(message)))
|
|
|
|
.Append("- ").Append(string.Format(Messages.ReminderWillBeSentOn, Markdown.Timestamp(remindAt)));
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(
|
|
|
|
string.Format(Messages.ReminderCreated, user.GetTag()), user)
|
|
|
|
.WithDescription(builder.ToString())
|
2023-07-09 16:32:14 +03:00
|
|
|
.WithColour(ColorsList.Green)
|
|
|
|
.Build();
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
2023-08-12 16:54:51 +03:00
|
|
|
|
|
|
|
/// <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(
|
2023-09-12 16:28:46 +03:00
|
|
|
[Description("Index of reminder to delete")] [MinValue(0)]
|
2023-08-12 23:19:07 +03:00
|
|
|
int index)
|
2023-08-12 16:54:51 +03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|