geekbot/Geekbot.net/Commands/Games/Pokedex.cs

76 lines
2.7 KiB
C#
Raw Normal View History

2017-10-03 21:47:09 +02:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
2017-10-03 21:47:09 +02:00
using PokeAPI;
namespace Geekbot.net.Commands.Games
2017-10-03 21:47:09 +02:00
{
public class Pokedex : ModuleBase
{
private readonly IErrorHandler _errorHandler;
2017-12-29 01:53:50 +01:00
public Pokedex(IErrorHandler errorHandler)
2017-10-03 21:47:09 +02:00
{
_errorHandler = errorHandler;
}
[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<Pokemon>(pokemonName.ToLower());
}
catch
{
await ReplyAsync("I couldn't find that pokemon :confused:");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var embed = await PokemonEmbedBuilder(pokemon);
2017-10-03 21:47:09 +02:00
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
2018-04-30 23:44:19 +02:00
private async Task<EmbedBuilder> PokemonEmbedBuilder(Pokemon pokemon)
2017-10-03 21:47:09 +02:00
{
var eb = new EmbedBuilder();
var species = await DataFetcher.GetApiObject<PokemonSpecies>(pokemon.ID);
2018-04-30 23:44:19 +02:00
eb.Title = $"#{pokemon.ID} {ToUpper(pokemon.Name)}";
2017-10-03 21:47:09 +02:00
eb.Description = species.FlavorTexts[1].FlavorText;
eb.ThumbnailUrl = pokemon.Sprites.FrontMale ?? pokemon.Sprites.FrontFemale;
2018-04-30 23:44:19 +02:00
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))));
2017-10-03 21:47:09 +02:00
eb.AddInlineField("Height", pokemon.Height);
eb.AddInlineField("Weight", pokemon.Mass);
return eb;
}
2018-04-30 23:44:19 +02:00
private string GetSingularOrPlural(int lenght, string word)
2017-10-03 21:47:09 +02:00
{
2017-12-29 01:53:50 +01:00
if (lenght == 1) return word;
return word.EndsWith("y") ? $"{word.Remove(word.Length - 1)}ies" : $"{word}s";
2017-10-03 21:47:09 +02:00
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
private string ToUpper(string s)
2017-10-03 21:47:09 +02:00
{
2017-12-29 01:53:50 +01:00
if (string.IsNullOrEmpty(s)) return string.Empty;
2017-10-03 21:47:09 +02:00
return char.ToUpper(s[0]) + s.Substring(1);
}
}
}