Format dates in the !anime and !manga commands with the correct culture info

This commit is contained in:
runebaas 2020-09-24 12:49:55 +02:00
parent 7ef0b6a319
commit ed7748833a
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -1,24 +1,25 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Discord;
using Discord.Commands;
using Geekbot.Core;
using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Extensions;
using Geekbot.Core.GuildSettingsManager;
using JikanDotNet;
namespace Geekbot.Bot.Commands.Integrations
{
public class Mal : ModuleBase
public class Mal : GeekbotCommandBase
{
private readonly IErrorHandler _errorHandler;
private readonly IJikan _client;
public Mal(IErrorHandler errorHandler)
public Mal(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager)
{
_client = new Jikan();
_errorHandler = errorHandler;
}
[Command("anime", RunMode = RunMode.Async)]
@ -41,8 +42,8 @@ namespace Geekbot.Bot.Commands.Integrations
eb.Title = anime.Title;
eb.Description = description;
eb.ImageUrl = anime.ImageURL;
eb.AddInlineField("Premiered", $"{anime.StartDate.Value.ToShortDateString()}");
eb.AddInlineField("Ended", anime.Airing ? "Present" : anime.EndDate.Value.ToShortDateString());
eb.AddInlineField("Premiered", FormatDate(anime.StartDate));
eb.AddInlineField("Ended", anime.Airing ? "Present" : FormatDate(anime.EndDate));
eb.AddInlineField("Episodes", anime.Episodes);
eb.AddInlineField("MAL Score", anime.Score);
eb.AddInlineField("Type", anime.Type);
@ -57,7 +58,7 @@ namespace Geekbot.Bot.Commands.Integrations
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
await ErrorHandler.HandleCommandException(e, Context);
}
}
@ -81,8 +82,8 @@ namespace Geekbot.Bot.Commands.Integrations
eb.Title = manga.Title;
eb.Description = description;
eb.ImageUrl = manga.ImageURL;
eb.AddInlineField("Premiered", $"{manga.StartDate.Value.ToShortDateString()}");
eb.AddInlineField("Ended", manga.Publishing ? "Present" : manga.EndDate.Value.ToShortDateString());
eb.AddInlineField("Premiered", FormatDate(manga.StartDate));
eb.AddInlineField("Ended", manga.Publishing ? "Present" : FormatDate(manga.EndDate));
eb.AddInlineField("Volumes", manga.Volumes);
eb.AddInlineField("Chapters", manga.Chapters);
eb.AddInlineField("MAL Score", manga.Score);
@ -97,8 +98,18 @@ namespace Geekbot.Bot.Commands.Integrations
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
await ErrorHandler.HandleCommandException(e, Context);
}
}
private string FormatDate(DateTime? dateTime)
{
if (!dateTime.HasValue)
{
return DateTime.MinValue.ToString("d", Thread.CurrentThread.CurrentUICulture);
}
return dateTime.Value.ToString("d", Thread.CurrentThread.CurrentUICulture);
}
}
}