Addin Pokedex Command
This commit is contained in:
parent
c48a3b5d84
commit
ef6fd15116
3 changed files with 86 additions and 2 deletions
83
Geekbot.net/Commands/Pokedex.cs
Normal file
83
Geekbot.net/Commands/Pokedex.cs
Normal file
|
@ -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<Pokemon>(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<EmbedBuilder> pokemonEmbedBuilder(Pokemon pokemon)
|
||||
{
|
||||
var eb = new EmbedBuilder();
|
||||
var species = await DataFetcher.GetApiObject<PokemonSpecies>(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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -21,6 +21,7 @@
|
|||
<PackageReference Include="Nancy" Version="2.0.0-clinteastwood" />
|
||||
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0-clinteastwood" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="PokeApi.NET" Version="1.1.0" />
|
||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
||||
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
|
||||
|
|
|
@ -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:");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue