forked from TeamInklings/Octobot
Use custom TimeSpanParser (#223)
Closes #154 --------- Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
894e819865
commit
e01fde83c6
10 changed files with 183 additions and 16 deletions
|
@ -17,6 +17,7 @@ using Remora.Discord.Extensions.Embeds;
|
|||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
using Octobot.Parsers;
|
||||
|
||||
namespace Octobot.Commands;
|
||||
|
||||
|
@ -110,7 +111,7 @@ public class RemindCommandGroup : CommandGroup
|
|||
/// <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="timeSpanString">The period of time which must pass before the reminder will be sent.</param>
|
||||
/// <param name="text">The text of the reminder.</param>
|
||||
/// <returns>A feedback sending result which may or may not have succeeded.</returns>
|
||||
[Command("remind")]
|
||||
|
@ -120,7 +121,8 @@ public class RemindCommandGroup : CommandGroup
|
|||
[UsedImplicitly]
|
||||
public async Task<Result> ExecuteReminderAsync(
|
||||
[Description("After what period of time mention the reminder")]
|
||||
TimeSpan @in,
|
||||
[Option("in")]
|
||||
string timeSpanString,
|
||||
[Description("Reminder text")] [MaxLength(512)]
|
||||
string text)
|
||||
{
|
||||
|
@ -129,6 +131,12 @@ public class RemindCommandGroup : CommandGroup
|
|||
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))
|
||||
{
|
||||
return Result.FromError(botResult);
|
||||
}
|
||||
|
||||
var executorResult = await _userApi.GetUserAsync(executorId, CancellationToken);
|
||||
if (!executorResult.IsDefined(out var executor))
|
||||
{
|
||||
|
@ -138,14 +146,25 @@ public class RemindCommandGroup : CommandGroup
|
|||
var data = await _guildData.GetData(guildId, CancellationToken);
|
||||
Messages.Culture = GuildSettings.Language.Get(data.Settings);
|
||||
|
||||
return await AddReminderAsync(@in, text, data, channelId, executor, CancellationToken);
|
||||
var parseResult = TimeSpanParser.TryParse(timeSpanString);
|
||||
if (!parseResult.IsDefined(out var timeSpan))
|
||||
{
|
||||
var failedEmbed = new EmbedBuilder()
|
||||
.WithSmallTitle(Messages.InvalidTimeSpan, bot)
|
||||
.WithColour(ColorsList.Red)
|
||||
.Build();
|
||||
|
||||
return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: CancellationToken);
|
||||
}
|
||||
|
||||
return await AddReminderAsync(timeSpan, text, data, channelId, executor, CancellationToken);
|
||||
}
|
||||
|
||||
private async Task<Result> AddReminderAsync(TimeSpan @in, string text, GuildData data,
|
||||
private async Task<Result> AddReminderAsync(TimeSpan timeSpan, string text, GuildData data,
|
||||
Snowflake channelId, IUser executor, CancellationToken ct = default)
|
||||
{
|
||||
var memberData = data.GetOrCreateMemberData(executor.ID);
|
||||
var remindAt = DateTimeOffset.UtcNow.Add(@in);
|
||||
var remindAt = DateTimeOffset.UtcNow.Add(timeSpan);
|
||||
var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct);
|
||||
if (!responseResult.IsDefined(out var response))
|
||||
{
|
||||
|
|
Reference in a new issue