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

128 lines
5 KiB
C#
Raw Normal View History

2017-10-01 23:41:25 +02:00
using System;
using System.Threading.Tasks;
2017-12-29 01:53:50 +01:00
using System.Web;
using System.Xml;
2017-10-01 23:41:25 +02:00
using Discord;
using Discord.Commands;
using Geekbot.net.Lib.Clients;
using Geekbot.net.Lib.ErrorHandling;
2018-05-17 22:06:58 +02:00
using Geekbot.net.Lib.Extensions;
2017-10-01 23:41:25 +02:00
namespace Geekbot.net.Commands.Integrations
2017-10-01 23:41:25 +02:00
{
2018-04-30 23:44:19 +02:00
public class Mal : ModuleBase
2017-10-01 23:41:25 +02:00
{
private readonly IErrorHandler _errorHandler;
2017-12-29 01:53:50 +01:00
private readonly IMalClient _malClient;
2017-10-01 23:41:25 +02:00
2018-04-30 23:44:19 +02:00
public Mal(IMalClient malClient, IErrorHandler errorHandler)
2017-10-01 23:41:25 +02:00
{
_malClient = malClient;
_errorHandler = errorHandler;
}
2017-12-29 01:53:50 +01:00
2017-10-01 23:41:25 +02:00
[Command("anime", RunMode = RunMode.Async)]
[Summary("Show Info about an Anime.")]
2018-04-30 23:44:19 +02:00
public async Task SearchAnime([Remainder] [Summary("AnimeName")] string animeName)
2017-10-01 23:41:25 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
if (_malClient.IsLoggedIn())
2017-10-01 23:41:25 +02:00
{
2018-04-30 23:44:19 +02:00
var anime = await _malClient.GetAnime(animeName);
2017-10-01 23:41:25 +02:00
if (anime != null)
{
var eb = new EmbedBuilder();
2017-12-29 01:53:50 +01:00
var description = HttpUtility.HtmlDecode(anime.Synopsis)
2017-10-01 23:41:25 +02:00
.Replace("<br />", "")
.Replace("[i]", "*")
.Replace("[/i]", "*");
2017-12-29 01:53:50 +01:00
2017-10-01 23:41:25 +02:00
eb.Title = anime.Title;
eb.Description = description;
eb.ImageUrl = anime.Image;
eb.AddInlineField("Premiered", $"{anime.StartDate}");
eb.AddInlineField("Ended", anime.EndDate == "0000-00-00" ? "???" : anime.EndDate);
eb.AddInlineField("Status", anime.Status);
eb.AddInlineField("Episodes", anime.Episodes);
eb.AddInlineField("MAL Score", anime.Score);
eb.AddInlineField("Type", anime.Type);
eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.Id}");
await ReplyAsync("", false, eb.Build());
}
else
{
await ReplyAsync("No anime found with that name...");
}
}
else
{
await ReplyAsync(
"Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me");
}
}
catch (XmlException e)
{
await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer");
}
2017-10-01 23:41:25 +02:00
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2017-10-01 23:41:25 +02:00
}
}
2017-12-29 01:53:50 +01:00
2017-10-01 23:41:25 +02:00
[Command("manga", RunMode = RunMode.Async)]
[Summary("Show Info about a Manga.")]
2018-04-30 23:44:19 +02:00
public async Task SearchManga([Remainder] [Summary("MangaName")] string mangaName)
2017-10-01 23:41:25 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
if (_malClient.IsLoggedIn())
2017-10-01 23:41:25 +02:00
{
2018-04-30 23:44:19 +02:00
var manga = await _malClient.GetManga(mangaName);
2017-10-01 23:41:25 +02:00
if (manga != null)
{
var eb = new EmbedBuilder();
2017-12-29 01:53:50 +01:00
var description = HttpUtility.HtmlDecode(manga.Synopsis)
2017-10-01 23:41:25 +02:00
.Replace("<br />", "")
.Replace("[i]", "*")
.Replace("[/i]", "*");
2017-12-29 01:53:50 +01:00
2017-10-01 23:41:25 +02:00
eb.Title = manga.Title;
eb.Description = description;
eb.ImageUrl = manga.Image;
eb.AddInlineField("Premiered", $"{manga.StartDate}");
eb.AddInlineField("Ended", manga.EndDate == "0000-00-00" ? "???" : manga.EndDate);
eb.AddInlineField("Status", manga.Status);
eb.AddInlineField("Volumes", manga.Volumes);
eb.AddInlineField("Chapters", manga.Chapters);
eb.AddInlineField("MAL Score", manga.Score);
eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.Id}");
await ReplyAsync("", false, eb.Build());
}
else
{
await ReplyAsync("No manga found with that name...");
}
}
else
{
await ReplyAsync(
"Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me");
}
}
catch (XmlException e)
{
await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer");
}
2017-10-01 23:41:25 +02:00
catch (Exception e)
{
2018-07-28 16:31:18 +02:00
await _errorHandler.HandleCommandException(e, Context);
2017-10-01 23:41:25 +02:00
}
}
}
}