2023-07-09 16:32:14 +03:00
|
|
|
using System.ComponentModel;
|
|
|
|
using Boyfriend.Data;
|
|
|
|
using Boyfriend.Services;
|
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;
|
|
|
|
|
|
|
|
namespace Boyfriend.Commands;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the command to manage reminders: /remind
|
|
|
|
/// </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-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-07-20 23:41:02 +03:00
|
|
|
data.GetMemberData(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
|
|
|
});
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(string.Format(Messages.ReminderCreated, user.GetTag()), user)
|
|
|
|
.WithDescription(string.Format(Messages.DescriptionReminderCreated, Markdown.Timestamp(remindAt)))
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|