geekbot/Geekbot.net/Commands/Integrations/MagicTheGathering.cs

96 lines
3.9 KiB
C#
Raw Normal View History

2017-12-24 14:54:35 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib.Converters;
using Geekbot.net.Lib.ErrorHandling;
2018-05-17 22:06:58 +02:00
using Geekbot.net.Lib.Extensions;
2017-12-24 14:54:35 +01:00
using MtgApiManager.Lib.Service;
namespace Geekbot.net.Commands.Integrations
2017-12-24 14:54:35 +01:00
{
2018-05-17 22:06:58 +02:00
public class MagicTheGathering : ModuleBase
2017-12-24 14:54:35 +01:00
{
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
2018-02-19 21:35:45 +01:00
private readonly IMtgManaConverter _manaConverter;
2017-12-24 14:54:35 +01:00
2018-05-17 22:06:58 +02:00
public MagicTheGathering(IErrorHandler errorHandler, IMtgManaConverter manaConverter)
2017-12-24 14:54:35 +01:00
{
_errorHandler = errorHandler;
2018-02-19 21:35:45 +01:00
_manaConverter = manaConverter;
2017-12-24 14:54:35 +01:00
}
[Command("mtg", RunMode = RunMode.Async)]
[Summary("Find a Magic The Gathering Card.")]
public async Task GetCard([Remainder] [Summary("card-name")] string cardName)
2017-12-24 14:54:35 +01:00
{
try
{
var message = await Context.Channel.SendMessageAsync($":mag: Looking up\"{cardName}\", please wait...");
2017-12-24 14:54:35 +01:00
var service = new CardService();
2019-03-02 07:21:42 +01:00
var result = service
.Where(x => x.Name, cardName)
// fewer cards less risk of deserialization problems, don't need more than one anyways...
.Where(x => x.PageSize, 1);
2017-12-24 14:54:35 +01:00
var card = result.All().Value.FirstOrDefault();
if (card == null)
{
await message.ModifyAsync(properties => properties.Content = ":red_circle: I couldn't find a card with that name...");
2017-12-24 14:54:35 +01:00
return;
}
2017-12-29 01:53:50 +01:00
var eb = new EmbedBuilder
{
Title = card.Name,
Description = card.Type
};
2017-12-29 01:53:50 +01:00
2017-12-24 14:54:35 +01:00
if (card.Colors != null) eb.WithColor(GetColor(card.Colors));
if (card.ImageUrl != null) eb.ImageUrl = card.ImageUrl.ToString();
2018-02-19 21:35:45 +01:00
if (!string.IsNullOrEmpty(card.Text)) eb.AddField("Text", _manaConverter.ConvertMana(card.Text));
2017-12-29 01:53:50 +01:00
2017-12-24 14:54:35 +01:00
if (!string.IsNullOrEmpty(card.Flavor)) eb.AddField("Flavor", card.Flavor);
if (!string.IsNullOrEmpty(card.SetName)) eb.AddInlineField("Set", card.SetName);
if (!string.IsNullOrEmpty(card.Power)) eb.AddInlineField("Power", card.Power);
if (!string.IsNullOrEmpty(card.Loyalty)) eb.AddInlineField("Loyality", card.Loyalty);
if (!string.IsNullOrEmpty(card.Toughness)) eb.AddInlineField("Thoughness", card.Toughness);
2017-12-29 01:53:50 +01:00
2018-02-19 21:35:45 +01:00
if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", _manaConverter.ConvertMana(card.ManaCost));
2017-12-24 14:54:35 +01:00
if (!string.IsNullOrEmpty(card.Rarity)) eb.AddInlineField("Rarity", card.Rarity);
2017-12-29 01:53:50 +01:00
if (card.Legalities != null && card.Legalities.Count > 0)
2017-12-29 01:53:50 +01:00
eb.AddField("Legality", string.Join(", ", card.Legalities.Select(e => e.Format)));
2017-12-24 14:54:35 +01:00
await message.ModifyAsync(properties =>
{
properties.Content = string.Empty;
properties.Embed = eb.Build();
});
2017-12-24 14:54:35 +01:00
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-12-24 14:54:35 +01:00
}
}
private Color GetColor(IEnumerable<string> colors)
{
var color = colors.FirstOrDefault();
return color switch
2017-12-24 14:54:35 +01:00
{
"Black" => new Color(203, 194, 191),
"White" => new Color(255, 251, 213),
"Blue" => new Color(170, 224, 250),
"Red" => new Color(250, 170, 143),
"Green" => new Color(155, 211, 174),
_ => new Color(204, 194, 212)
};
2017-12-24 14:54:35 +01:00
}
}
}