-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DSharpPlus; | ||
using DSharpPlus.Commands; | ||
using DSharpPlus.Commands.ArgumentModifiers; | ||
using DSharpPlus.Commands.Converters; | ||
using DSharpPlus.Commands.Processors.TextCommands; | ||
using Humanizer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace OoLunar.Tomoe.Commands.Common | ||
{ | ||
/// <summary> | ||
/// "Time until in 4 hours: 4 hours, 43 minutes, 33 seconds" | ||
/// </summary> | ||
/// <remarks> | ||
/// I'm only getting 4 and a half hours of sleep tonight. | ||
/// </remarks> | ||
public static class TimeUntilCommand | ||
{ | ||
private static readonly DateTimeOffsetConverter _dateTimeArgumentConverter = new(); | ||
|
||
/// <summary> | ||
/// Calculates how long you have to wait until a specific time. | ||
/// </summary> | ||
/// <param name="when">When you want to know how long you have to wait.</param> | ||
[Command("time_until")] | ||
public static async ValueTask ExecuteAsync(CommandContext context, [RemainingText] string? when = null) | ||
{ | ||
if (string.IsNullOrWhiteSpace(when)) | ||
{ | ||
await context.RespondAsync("Invalid timestamp: `null`"); | ||
return; | ||
} | ||
|
||
when = when.Trim(); | ||
TextConverterContext converterContext = new() | ||
{ | ||
Channel = context.Channel, | ||
Command = context.Command, | ||
Extension = context.Extension, | ||
RawArguments = when, | ||
Message = null!, | ||
ServiceScope = context.ServiceProvider.CreateAsyncScope(), | ||
Splicer = context.Extension.GetProcessor<TextCommandProcessor>().Configuration.TextArgumentSplicer, | ||
User = context.User | ||
}; | ||
|
||
converterContext.NextArgument(); | ||
if (!(await _dateTimeArgumentConverter.ConvertAsync(converterContext)).IsDefined(out DateTimeOffset dateTimeOffset) || dateTimeOffset == default) | ||
{ | ||
await context.RespondAsync($"Invalid timestamp: `{when}`"); | ||
return; | ||
} | ||
|
||
TimeSpan offset = (await context.GetTimeZoneAsync()).BaseUtcOffset; | ||
DateTimeOffset untilDate = dateTimeOffset.ToOffset(offset).Subtract(offset); | ||
TimeSpan untilTime = untilDate - DateTimeOffset.UtcNow; | ||
await context.RespondAsync($"Time until {Formatter.Timestamp(untilDate)}: {untilTime.Humanize(3)}"); | ||
} | ||
} | ||
} |