From 41e0a9f8d72c24766019a4e7844337cab62ce281 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 26 Mar 2021 00:11:15 +0100 Subject: [PATCH] Add !evergiven to see if the ship is still stuck in the suez canal --- src/Bot/Bot.csproj | 9 +++ src/Bot/Commands/Utils/Evergiven.cs | 50 +++++++++++++ src/Bot/Localization/Evergiven.Designer.cs | 81 ++++++++++++++++++++++ src/Bot/Localization/Evergiven.de-ch.resx | 20 ++++++ src/Bot/Localization/Evergiven.resx | 27 ++++++++ src/Bot/Utils/DateLocalization.cs | 6 +- 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/Bot/Commands/Utils/Evergiven.cs create mode 100644 src/Bot/Localization/Evergiven.Designer.cs create mode 100644 src/Bot/Localization/Evergiven.de-ch.resx create mode 100644 src/Bot/Localization/Evergiven.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 9312ac1..2b1cdae 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -88,6 +88,10 @@ ResXFileCodeGenerator Corona.Designer.cs + + ResXFileCodeGenerator + Evergiven.Designer.cs + @@ -153,5 +157,10 @@ True Corona.resx + + True + True + Evergiven.resx + diff --git a/src/Bot/Commands/Utils/Evergiven.cs b/src/Bot/Commands/Utils/Evergiven.cs new file mode 100644 index 0000000..663fb0a --- /dev/null +++ b/src/Bot/Commands/Utils/Evergiven.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.Bot.Utils; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GuildSettingsManager; +using HtmlAgilityPack; + +namespace Geekbot.Bot.Commands.Utils +{ + public class Evergiven : GeekbotCommandBase + { + public Evergiven(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) + { + } + + [Command("evergiven", RunMode = RunMode.Async)] + [Summary("Check if the evergiven ship is still stuck in the suez canal")] + public async Task GetStatus() + { + try + { + var httpClient = HttpAbstractions.CreateDefaultClient(); + var response = await httpClient.GetAsync("https://istheshipstillstuck.com/"); + response.EnsureSuccessStatusCode(); + var stringResponse = await response.Content.ReadAsStringAsync(); + + var doc = new HtmlDocument(); + doc.LoadHtml(stringResponse); + var yesOrNoNode = doc.DocumentNode.SelectNodes("//a").FirstOrDefault(); + + if (yesOrNoNode?.InnerHtml == "Yes.") + { + var stuckSince = DateTime.Now - new DateTime(2021, 03, 23, 10, 39, 0); + var formatted = DateLocalization.FormatDateTimeAsRemaining(stuckSince); + await ReplyAsync(string.Format(Localization.Evergiven.StillStuck, formatted));// $"Ever Given is **still stuck** in the suez canal! It has been stuck for {formatted}"); + return; + } + + await ReplyAsync(Localization.Evergiven.NotStuckAnymore); + } + catch (Exception e) + { + await ErrorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/src/Bot/Localization/Evergiven.Designer.cs b/src/Bot/Localization/Evergiven.Designer.cs new file mode 100644 index 0000000..7f66fed --- /dev/null +++ b/src/Bot/Localization/Evergiven.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Evergiven { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Evergiven() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Evergiven", typeof(Evergiven).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Seems like Ever Given has moved on, check https://istheshipstillstuck.com/. + /// + internal static string NotStuckAnymore { + get { + return ResourceManager.GetString("NotStuckAnymore", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Ever Given is **still stuck** in the suez canal! It has been stuck for {0}. + /// + internal static string StillStuck { + get { + return ResourceManager.GetString("StillStuck", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Evergiven.de-ch.resx b/src/Bot/Localization/Evergiven.de-ch.resx new file mode 100644 index 0000000..b1563fc --- /dev/null +++ b/src/Bot/Localization/Evergiven.de-ch.resx @@ -0,0 +1,20 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Ever Given **steckt immer no fescht** im Suez Kanal! Es isch derte siit {0} + + + Es gseht danach us das Ever Given wiiter gange isch, gsehne https://istheshipstillstuck.com/ + + \ No newline at end of file diff --git a/src/Bot/Localization/Evergiven.resx b/src/Bot/Localization/Evergiven.resx new file mode 100644 index 0000000..36fb012 --- /dev/null +++ b/src/Bot/Localization/Evergiven.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Seems like Ever Given has moved on, check https://istheshipstillstuck.com/ + + + Ever Given is **still stuck** in the suez canal! It has been stuck for {0} + + \ No newline at end of file diff --git a/src/Bot/Utils/DateLocalization.cs b/src/Bot/Utils/DateLocalization.cs index eea40fb..ee52d73 100644 --- a/src/Bot/Utils/DateLocalization.cs +++ b/src/Bot/Utils/DateLocalization.cs @@ -7,7 +7,11 @@ namespace Geekbot.Bot.Utils { public static string FormatDateTimeAsRemaining(DateTimeOffset dateTime) { - var remaining = dateTime - DateTimeOffset.Now; + return FormatDateTimeAsRemaining(dateTime - DateTimeOffset.Now); + } + + public static string FormatDateTimeAsRemaining(TimeSpan remaining) + { const string formattable = "{0} {1}"; var sb = new StringBuilder();