From ef6fd15116f38333f8d35b5b6b7a919ac71ce2e3 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 3 Oct 2017 21:47:09 +0200 Subject: [PATCH] Addin Pokedex Command --- Geekbot.net/Commands/Pokedex.cs | 83 +++++++++++++++++++++++++++++++++ Geekbot.net/Geekbot.net.csproj | 1 + Geekbot.net/Lib/ErrorHandler.cs | 4 +- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 Geekbot.net/Commands/Pokedex.cs diff --git a/Geekbot.net/Commands/Pokedex.cs b/Geekbot.net/Commands/Pokedex.cs new file mode 100644 index 0000000..19d5776 --- /dev/null +++ b/Geekbot.net/Commands/Pokedex.cs @@ -0,0 +1,83 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using PokeAPI; +using Serilog; + +namespace Geekbot.net.Commands +{ + public class Pokedex : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly ILogger _logger; + + public Pokedex(IErrorHandler errorHandler, ILogger logger) + { + _errorHandler = errorHandler; + _logger = logger; + } + + [Command("pokedex", RunMode = RunMode.Async)] + [Summary("A Pokedex Tool")] + public async Task GetPokemon([Summary("pokemonName")] string pokemonName) + { + try + { + DataFetcher.ShouldCacheData = true; + Pokemon pokemon; + try + { + pokemon = await DataFetcher.GetNamedApiObject(pokemonName.ToLower()); + } + catch + { + await ReplyAsync("I couldn't find that pokemon :confused:"); + return; + } + var embed = await pokemonEmbedBuilder(pokemon); + await ReplyAsync("", false, embed.Build()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private async Task pokemonEmbedBuilder(Pokemon pokemon) + { + var eb = new EmbedBuilder(); + var species = await DataFetcher.GetApiObject(pokemon.ID); + eb.Title = $"#{pokemon.ID} {toUpper(pokemon.Name)}"; + eb.Description = species.FlavorTexts[1].FlavorText; + eb.ThumbnailUrl = pokemon.Sprites.FrontMale ?? pokemon.Sprites.FrontFemale; + eb.AddInlineField(getSingularOrPlural(pokemon.Types.Length, "Type"), string.Join(", ", pokemon.Types.Select(t => toUpper(t.Type.Name)))); + eb.AddInlineField(getSingularOrPlural(pokemon.Abilities.Length, "Ability"), string.Join(", ", pokemon.Abilities.Select(t => toUpper(t.Ability.Name)))); + eb.AddInlineField("Height", pokemon.Height); + eb.AddInlineField("Weight", pokemon.Mass); + return eb; + } + + private string getSingularOrPlural(int lenght, string word) + { + if (lenght == 1) + { + return word; + } + return word.EndsWith("y") ? $"{word.Remove(word.Length-1)}ies" : $"{word}s"; + } + + private string toUpper(string s) + { + if (string.IsNullOrEmpty(s)) + { + return string.Empty; + } + return char.ToUpper(s[0]) + s.Substring(1); + } + + + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 056eb93..f139230 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -21,6 +21,7 @@ + diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 5ded3a2..dfba5fa 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -15,7 +15,7 @@ namespace Geekbot.net.Lib // this.botOwnerDmChannel = botOwnerDmChannel; } - public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "") + public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:") { var errorMsg = $"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\", complete message was \"{Context.Message}\""; @@ -31,6 +31,6 @@ namespace Geekbot.net.Lib public interface IErrorHandler { - void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = ""); + void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:"); } } \ No newline at end of file