Swap the my anime list wrapper for the !anime and !manga commands

This commit is contained in:
runebaas 2020-09-24 12:21:21 +02:00
parent 216188f61f
commit 7ef0b6a319
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
6 changed files with 47 additions and 150 deletions

View file

@ -24,8 +24,8 @@
<PackageReference Include="CommandLineParser" Version="2.8.0" /> <PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.45.0.1929" /> <PackageReference Include="Google.Apis.YouTube.v3" Version="1.45.0.1929" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.24" /> <PackageReference Include="HtmlAgilityPack" Version="1.11.24" />
<PackageReference Include="JikanDotNet" Version="1.5.1" />
<PackageReference Include="MtgApiManager.Lib" Version="1.2.2" /> <PackageReference Include="MtgApiManager.Lib" Version="1.2.2" />
<PackageReference Include="MyAnimeListSharp" Version="1.3.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PokeApi.NET" Version="1.1.2" /> <PackageReference Include="PokeApi.NET" Version="1.1.2" />
<PackageReference Include="SharpRaven" Version="2.4.0" /> <PackageReference Include="SharpRaven" Version="2.4.0" />

View file

@ -1,23 +1,23 @@
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
using System.Xml;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.Core.ErrorHandling; using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Extensions; using Geekbot.Core.Extensions;
using Geekbot.Core.MalClient; using JikanDotNet;
namespace Geekbot.Bot.Commands.Integrations namespace Geekbot.Bot.Commands.Integrations
{ {
public class Mal : ModuleBase public class Mal : ModuleBase
{ {
private readonly IErrorHandler _errorHandler; private readonly IErrorHandler _errorHandler;
private readonly IMalClient _malClient; private readonly IJikan _client;
public Mal(IMalClient malClient, IErrorHandler errorHandler) public Mal(IErrorHandler errorHandler)
{ {
_malClient = malClient; _client = new Jikan();
_errorHandler = errorHandler; _errorHandler = errorHandler;
} }
@ -27,46 +27,34 @@ namespace Geekbot.Bot.Commands.Integrations
{ {
try try
{ {
if (_malClient.IsLoggedIn()) var results = await _client.SearchAnime(animeName);
var anime = results.Results.FirstOrDefault();
if (anime != null)
{ {
var anime = await _malClient.GetAnime(animeName); var eb = new EmbedBuilder();
if (anime != null)
{
var eb = new EmbedBuilder();
var description = HttpUtility.HtmlDecode(anime.Synopsis) var description = HttpUtility.HtmlDecode(anime.Description)
.Replace("<br />", "") .Replace("<br />", "")
.Replace("[i]", "*") .Replace("[i]", "*")
.Replace("[/i]", "*"); .Replace("[/i]", "*");
eb.Title = anime.Title; eb.Title = anime.Title;
eb.Description = description; eb.Description = description;
eb.ImageUrl = anime.Image; eb.ImageUrl = anime.ImageURL;
eb.AddInlineField("Premiered", $"{anime.StartDate}"); eb.AddInlineField("Premiered", $"{anime.StartDate.Value.ToShortDateString()}");
eb.AddInlineField("Ended", anime.EndDate == "0000-00-00" ? "???" : anime.EndDate); eb.AddInlineField("Ended", anime.Airing ? "Present" : anime.EndDate.Value.ToShortDateString());
eb.AddInlineField("Status", anime.Status); eb.AddInlineField("Episodes", anime.Episodes);
eb.AddInlineField("Episodes", anime.Episodes); eb.AddInlineField("MAL Score", anime.Score);
eb.AddInlineField("MAL Score", anime.Score); eb.AddInlineField("Type", anime.Type);
eb.AddInlineField("Type", anime.Type); eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.MalId}");
eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.Id}");
await ReplyAsync("", false, eb.Build()); await ReplyAsync("", false, eb.Build());
}
else
{
await ReplyAsync("No anime found with that name...");
}
} }
else else
{ {
await ReplyAsync( await ReplyAsync("No anime found with that name...");
"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");
}
catch (Exception e) catch (Exception e)
{ {
await _errorHandler.HandleCommandException(e, Context); await _errorHandler.HandleCommandException(e, Context);
@ -79,46 +67,34 @@ namespace Geekbot.Bot.Commands.Integrations
{ {
try try
{ {
if (_malClient.IsLoggedIn()) var results = await _client.SearchManga(mangaName);
var manga = results.Results.FirstOrDefault();
if (manga != null)
{ {
var manga = await _malClient.GetManga(mangaName); var eb = new EmbedBuilder();
if (manga != null)
{ var description = HttpUtility.HtmlDecode(manga.Description)
var eb = new EmbedBuilder(); .Replace("<br />", "")
.Replace("[i]", "*")
var description = HttpUtility.HtmlDecode(manga.Synopsis) .Replace("[/i]", "*");
.Replace("<br />", "")
.Replace("[i]", "*") eb.Title = manga.Title;
.Replace("[/i]", "*"); eb.Description = description;
eb.ImageUrl = manga.ImageURL;
eb.Title = manga.Title; eb.AddInlineField("Premiered", $"{manga.StartDate.Value.ToShortDateString()}");
eb.Description = description; eb.AddInlineField("Ended", manga.Publishing ? "Present" : manga.EndDate.Value.ToShortDateString());
eb.ImageUrl = manga.Image; eb.AddInlineField("Volumes", manga.Volumes);
eb.AddInlineField("Premiered", $"{manga.StartDate}"); eb.AddInlineField("Chapters", manga.Chapters);
eb.AddInlineField("Ended", manga.EndDate == "0000-00-00" ? "???" : manga.EndDate); eb.AddInlineField("MAL Score", manga.Score);
eb.AddInlineField("Status", manga.Status); eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.MalId}");
eb.AddInlineField("Volumes", manga.Volumes);
eb.AddInlineField("Chapters", manga.Chapters); await ReplyAsync("", false, eb.Build());
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 else
{ {
await ReplyAsync( await ReplyAsync("No manga found with that name...");
"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");
}
catch (Exception e) catch (Exception e)
{ {
await _errorHandler.HandleCommandException(e, Context); await _errorHandler.HandleCommandException(e, Context);

View file

@ -18,7 +18,6 @@ using Geekbot.Core.Highscores;
using Geekbot.Core.KvInMemoryStore; using Geekbot.Core.KvInMemoryStore;
using Geekbot.Core.Levels; using Geekbot.Core.Levels;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
using Geekbot.Core.MalClient;
using Geekbot.Core.Media; using Geekbot.Core.Media;
using Geekbot.Core.RandomNumberGenerator; using Geekbot.Core.RandomNumberGenerator;
using Geekbot.Core.ReactionListener; using Geekbot.Core.ReactionListener;
@ -159,7 +158,6 @@ namespace Geekbot.Bot
_reactionListener = new ReactionListener(_databaseInitializer.Initialize()); _reactionListener = new ReactionListener(_databaseInitializer.Initialize());
_guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize()); _guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize());
var fortunes = new FortunesProvider(_logger); var fortunes = new FortunesProvider(_logger);
var malClient = new MalClient(_globalSettings, _logger);
var levelCalc = new LevelCalc(); var levelCalc = new LevelCalc();
var emojiConverter = new EmojiConverter(); var emojiConverter = new EmojiConverter();
var mtgManaConverter = new MtgManaConverter(); var mtgManaConverter = new MtgManaConverter();
@ -176,7 +174,6 @@ namespace Geekbot.Bot
services.AddSingleton<IEmojiConverter>(emojiConverter); services.AddSingleton<IEmojiConverter>(emojiConverter);
services.AddSingleton<IFortunesProvider>(fortunes); services.AddSingleton<IFortunesProvider>(fortunes);
services.AddSingleton<IMediaProvider>(mediaProvider); services.AddSingleton<IMediaProvider>(mediaProvider);
services.AddSingleton<IMalClient>(malClient);
services.AddSingleton<IMtgManaConverter>(mtgManaConverter); services.AddSingleton<IMtgManaConverter>(mtgManaConverter);
services.AddSingleton<IWikipediaClient>(wikipediaClient); services.AddSingleton<IWikipediaClient>(wikipediaClient);
services.AddSingleton<IRandomNumberGenerator>(randomNumberGenerator); services.AddSingleton<IRandomNumberGenerator>(randomNumberGenerator);

View file

@ -22,7 +22,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0-rc.1.*" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0-rc.1.*" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-rc.1.*" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-rc.1.*" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0-rc.1.*" /> <PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0-rc.1.*" />
<PackageReference Include="MyAnimeListSharp" Version="1.3.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.2" /> <PackageReference Include="NLog" Version="4.7.2" />
<PackageReference Include="NLog.Config" Version="4.7.2" /> <PackageReference Include="NLog.Config" Version="4.7.2" />

View file

@ -1,12 +0,0 @@
using System.Threading.Tasks;
using MyAnimeListSharp.Core;
namespace Geekbot.Core.MalClient
{
public interface IMalClient
{
bool IsLoggedIn();
Task<AnimeEntry> GetAnime(string query);
Task<MangaEntry> GetManga(string query);
}
}

View file

@ -1,63 +0,0 @@
using System.Threading.Tasks;
using Geekbot.Core.GlobalSettings;
using Geekbot.Core.Logger;
using MyAnimeListSharp.Auth;
using MyAnimeListSharp.Core;
using MyAnimeListSharp.Facade.Async;
namespace Geekbot.Core.MalClient
{
public class MalClient : IMalClient
{
private readonly IGlobalSettings _globalSettings;
private readonly IGeekbotLogger _logger;
private ICredentialContext _credentials;
private AnimeSearchMethodsAsync _animeSearch;
private MangaSearchMethodsAsync _mangaSearch;
public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger)
{
_globalSettings = globalSettings;
_logger = logger;
ReloadClient();
}
private bool ReloadClient()
{
var malCredentials = _globalSettings.GetKey("MalCredentials");
if (!string.IsNullOrEmpty(malCredentials))
{
var credSplit = malCredentials.Split('|');
_credentials = new CredentialContext()
{
UserName = credSplit[0],
Password = credSplit[1]
};
_animeSearch = new AnimeSearchMethodsAsync(_credentials);
_mangaSearch = new MangaSearchMethodsAsync(_credentials);
_logger.Debug(LogSource.Geekbot, "Logged in to MAL");
return true;
}
_logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!");
return false;
}
public bool IsLoggedIn()
{
return _credentials != null;
}
public async Task<AnimeEntry> GetAnime(string query)
{
var response = await _animeSearch.SearchDeserializedAsync(query);
return response.Entries.Count == 0 ? null : response.Entries[0];
}
public async Task<MangaEntry> GetManga(string query)
{
var response = await _mangaSearch.SearchDeserializedAsync(query);
return response.Entries.Count == 0 ? null : response.Entries[0];
}
}
}