Skip to content

Commit

Permalink
Fix remind me
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickklaeren committed Nov 15, 2023
1 parent 83fdc34 commit e6b78b0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 162 deletions.
105 changes: 14 additions & 91 deletions src/Accord.Bot/CommandGroups/ReminderCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,106 +35,29 @@ public partial class ReminderCommandGroup : AccordCommandGroup
private readonly IDiscordRestGuildAPI _guildApi;
private readonly DiscordAvatarHelper _discordAvatarHelper;
private readonly FeedbackService _feedbackService;
private readonly IDiscordRestInteractionAPI _discordRestInteractionApi;

[Command("me"), Description("Add a reminder for yourself")]
[SuppressInteractionResponse(true)]
public async Task<IResult> AddReminder()
public async Task<IResult> AddReminder(TimeSpan timeSpan, string message)
{
if (!_commandContext.TryGetUserID(out var discordUserId))
{
return await _feedbackService.SendContextualAsync("Failed to get the Discord user ID");
}
var sanitizedMessage = message.DiscordSanitize();

if (_commandContext is not IInteractionContext interactionContext)
{
return (Result)await _feedbackService.SendContextualWarningAsync
(
"This command can only be used with slash commands.",
discordUserId,
new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral)
);
}
_commandContext.TryGetUserID(out var userId);
_commandContext.TryGetChannelID(out var channelId);

var response = new InteractionResponse
(
InteractionCallbackType.Modal,
new
(
new InteractionModalCallbackData
(
CustomIDHelpers.CreateModalID("add-reminder-modal"),
"Add a reminder",
new[]
{
new ActionRowComponent
(
new[]
{
new TextInputComponent
(
"description",
TextInputStyle.Paragraph,
"What should I remind you about?",
1,
500,
true,
default,
"Remind me to put a semi colon on the end of line 69420"
)
}
),
new ActionRowComponent
(
new[]
{
new TextInputComponent
(
"number",
TextInputStyle.Short,
"When should I remind you?",
1,
3,
true,
default,
"1"
)
}
),
new ActionRowComponent
(
new[]
{
new StringSelectComponent(
"period-unit",
new ISelectOption[]
{
new SelectOption("Seconds", "Seconds"),
new SelectOption("Minutes", "Minutes"),
new SelectOption("Hours", "Hours"),
new SelectOption("Days", "Days"),
new SelectOption("Weeks", "Weeks"),
new SelectOption("Years", "Years"),
},
"Hours",
1,
1)
}
)
}
)
)
);
var response = await _mediator.Send(new AddReminderRequest(
userId.Value,
channelId.Value,
timeSpan,
sanitizedMessage
));

var result = await _discordRestInteractionApi.CreateInteractionResponseAsync
(
interactionContext.Interaction.ID,
interactionContext.Interaction.Token,
response,
ct: CancellationToken
await response.GetAction(
async () => await _feedbackService.SendContextualAsync($"You will be reminded about it in {timeSpan.Humanize()}"),
async () => await _feedbackService.SendContextualAsync(response.ErrorMessage)
);

return result;
return Result.FromSuccess();
}

[Command("list"), Description("List pending reminders"), Ephemeral]
Expand Down
66 changes: 0 additions & 66 deletions src/Accord.Bot/CommandGroups/ReminderCommandGroupInteractions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public static IServiceCollection AddDiscordBot(this IServiceCollection services,
o.Intents |= GatewayIntents.GuildMessages;
})
.AddDiscordCommands(true)
.AddPostExecutionEvent<AfterCommandPostExecutionEvent>();

services
.AddInteractivity()
.AddInteractionGroup<ReminderCommandGroupInteractions>();
.AddPostExecutionEvent<AfterCommandPostExecutionEvent>()
.AddParser<TimeSpanParser>();

services
.AddCommandTree()
Expand Down
23 changes: 23 additions & 0 deletions src/Accord.Bot/Infrastructure/TimeSpanParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Remora.Commands.Parsers;
using Remora.Commands.Results;
using Remora.Results;
using System;
using System.Threading;
using System.Threading.Tasks;
using TimeSpanParserUtil;

namespace Accord.Bot.Infrastructure;

public class TimeSpanParser : AbstractTypeParser<TimeSpan>
{
public override ValueTask<Result<TimeSpan>> TryParseAsync(string value, CancellationToken ct = default)
{
return new ValueTask<Result<TimeSpan>>(
TimeSpanParserUtil.TimeSpanParser.TryParse(
value.ToLowerInvariant(),
new TimeSpanParserOptions { DecimalSecondsCountsAsMilliseconds = true }, out var timeSpan)
? Result<TimeSpan>.FromSuccess(timeSpan)
: new ParsingError<TimeSpan>($"Could not parse input \"{value}\" into a valid {nameof(TimeSpanParserUtil.TimeSpanParser)}")
);
}
}

0 comments on commit e6b78b0

Please sign in to comment.