Skip to content

Commit

Permalink
Use discord timestamp to present times in the kachna command. (Resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha12 committed Oct 9, 2023
1 parent 5735daf commit f8a7149
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
35 changes: 24 additions & 11 deletions src/GrillBot.App/Actions/Commands/DuckInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GrillBot.Common.Services.KachnaOnline;
using GrillBot.Common.Services.KachnaOnline.Models;
using GrillBot.Core.Exceptions;
using GrillBot.Core.Extensions;

namespace GrillBot.App.Actions.Commands;

Expand All @@ -15,7 +16,6 @@ public class DuckInfo : CommandAction
private LoggingManager LoggingManager { get; }

private string InfoChannel => Configuration.GetRequiredSection("Services:KachnaOnline:InfoChannel").Get<string>()!;
private CultureInfo Culture => Texts.GetCulture(Locale);

public DuckInfo(IKachnaOnlineClient client, ITextsManager texts, IConfiguration configuration, LoggingManager loggingManager)
{
Expand All @@ -30,6 +30,8 @@ public async Task<Embed> ProcessAsync()
try
{
var currentState = await Client.GetCurrentStateAsync();
ConvertDateTimesToUtc(currentState);

return CreateEmbed(currentState);
}
catch (HttpRequestException ex)
Expand All @@ -39,6 +41,17 @@ public async Task<Embed> ProcessAsync()
}
}

private static void ConvertDateTimesToUtc(DuckState state)
{
state.Start = state.Start.WithKind(DateTimeKind.Local).ToUniversalTime();

if (state.PlannedEnd is not null)
state.PlannedEnd = state.PlannedEnd.Value.WithKind(DateTimeKind.Local).ToUniversalTime();

if (state.FollowingState is not null)
ConvertDateTimesToUtc(state.FollowingState);
}

private Embed CreateEmbed(DuckState currentState)
{
var embed = new EmbedBuilder()
Expand Down Expand Up @@ -81,35 +94,35 @@ private void ProcessPrivateOrClosed(StringBuilder titleBuilder, DuckState state,

private void FormatWithNextOpening(StringBuilder titleBuilder, DuckState state, EmbedBuilder embedBuilder)
{
var left = state.FollowingState!.Start - DateTime.Now;

titleBuilder
.Append(GetText("NextOpenAt").FormatWith(left.Humanize(culture: Culture, precision: int.MaxValue, minUnit: TimeUnit.Minute)));
var tag = TimestampTag.FromDateTime(state.FollowingState!.Start);

titleBuilder.Append(GetText("NextOpenAt").FormatWith(tag.ToString(TimestampTagStyles.Relative)));
AddNoteToEmbed(embedBuilder, state.Note);
}

private void ProcessOpenBar(StringBuilder titleBuilder, DuckState state, EmbedBuilder embedBuilder)
{
titleBuilder.Append(GetText("Opened"));
embedBuilder.AddField(GetText("Open"), state.Start.ToString("HH:mm"), true);

var openingAt = TimestampTag.FromDateTimeOffset(state.Start);
embedBuilder.AddField(GetText("Open"), openingAt.ToString(TimestampTagStyles.ShortTime), true);

if (state.PlannedEnd.HasValue)
{
var left = state.PlannedEnd.Value - DateTime.Now;
var tag = TimestampTag.FromDateTime(state.PlannedEnd.Value);

titleBuilder.Append(GetText("TimeToClose").FormatWith(left.Humanize(culture: Culture, precision: int.MaxValue, minUnit: TimeUnit.Minute)));
embedBuilder.AddField(GetText("Closing"), state.PlannedEnd.Value.ToString("HH:mm"), true);
titleBuilder.Append(GetText("TimeToClose").FormatWith(tag.ToString(TimestampTagStyles.Relative)));
embedBuilder.AddField(GetText("Closing"), tag.ToString(TimestampTagStyles.ShortTime), true);
}

AddNoteToEmbed(embedBuilder, state.Note);
}

private void ProcessChillzone(StringBuilder titleBuilder, DuckState state, EmbedBuilder embedBuilder)
{
titleBuilder
.Append(GetText("ChillzoneTo").FormatWith(state.PlannedEnd!.Value.ToString("HH:mm")));
var closingAt = TimestampTag.FromDateTime(state.PlannedEnd!.Value);

titleBuilder.Append(GetText("ChillzoneTo").FormatWith(closingAt.ToString(TimestampTagStyles.ShortTime)));
AddNoteToEmbed(embedBuilder, state.Note);
}

Expand Down
2 changes: 1 addition & 1 deletion src/GrillBot.App/GrillBot.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="7.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="7.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
Expand Down
3 changes: 2 additions & 1 deletion src/GrillBot.App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void ConfigureServices(IServiceCollection services)
AlwaysResolveStickers = true,
DefaultRetryMode = RetryMode.RetryRatelimit,
SuppressUnknownDispatchWarnings = true,
LogGatewayIntentWarnings = false
LogGatewayIntentWarnings = false,
UseInteractionSnowflakeDate = false
};

var currentAssembly = Assembly.GetExecutingAssembly();
Expand Down
2 changes: 1 addition & 1 deletion src/GrillBot.Common/GrillBot.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ItemGroup>
<PackageReference Include="CircularBuffer" Version="1.3.0" />
<PackageReference Include="Discord.Net" Version="3.12.0" />
<PackageReference Include="GrillBot.Core.Services" Version="1.4.1" />
<PackageReference Include="GrillBot.Core.Services" Version="1.4.2" />
<PackageReference Include="Humanizer.Core.cs" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
Expand Down
4 changes: 2 additions & 2 deletions src/GrillBot.Data/Resources/Localization/messages.cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@
"DuckName": "U kachničky",
"Closed": "Kachna je zavřená.",
"NextOpenNotPlanned": "Další otvíračka není naplánovaná.",
"NextOpenAt": "Další otvíračka bude za {0}.",
"NextOpenAt": "Další otvíračka bude {0}.",
"Opened": "Kachna je otevřená!",
"Open": "Otevřeno",
"TimeToClose": " Do konce zbývá {0}.",
"TimeToClose": " Otvíračka končí {0}.",
"Closing": "Zavíráme",
"ChillzoneTo": "Kachna je otevřená v režimu chillzóna až do {0}!",
"Note": "Poznámka"
Expand Down
4 changes: 2 additions & 2 deletions src/GrillBot.Data/Resources/Localization/messages.en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@
"DuckName": "U kachničky",
"Closed": "Kachna is closed.",
"NextOpenNotPlanned": "The next opener is not scheduled.",
"NextOpenAt": "Opening hours start at {0}.",
"NextOpenAt": "Opening hours start {0}.",
"Opened": "Kachna is open!",
"Open": "OPEN",
"TimeToClose": " {0} left to go.",
"TimeToClose": " Opening hours ends {0}.",
"Closing": "We are closing",
"ChillzoneTo": "The duck is open in chillzone mode until {0}!",
"Note": "Note"
Expand Down

0 comments on commit f8a7149

Please sign in to comment.