Adding mal integration
This commit is contained in:
parent
8d08b87d09
commit
6732506dae
4 changed files with 197 additions and 5 deletions
|
@ -14,14 +14,10 @@
|
||||||
<Version>1.0.2</Version>
|
<Version>1.0.2</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.29.1.991" />
|
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.29.1.991" />
|
||||||
<PackageReference Include="Hangfire" Version="1.6.17" />
|
|
||||||
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.7.0" />
|
|
||||||
<PackageReference Include="HtmlAgilityPack.NetCore">
|
|
||||||
<Version>1.5.0.1</Version>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
|
||||||
|
<PackageReference Include="MyAnimeListSharp" Version="1.3.4" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
||||||
|
|
78
Geekbot.net/Lib/MalClient.cs
Normal file
78
Geekbot.net/Lib/MalClient.cs
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MyAnimeListSharp.Auth;
|
||||||
|
using MyAnimeListSharp.Core;
|
||||||
|
using MyAnimeListSharp.Facade.Async;
|
||||||
|
using Serilog;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Lib
|
||||||
|
{
|
||||||
|
public class MalClient : IMalClient
|
||||||
|
{
|
||||||
|
private readonly IDatabase _redis;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private ICredentialContext _credentials;
|
||||||
|
private AnimeSearchMethodsAsync _animeSearch;
|
||||||
|
private MangaSearchMethodsAsync _mangaSearch;
|
||||||
|
|
||||||
|
public MalClient(IDatabase redis, ILogger logger)
|
||||||
|
{
|
||||||
|
_redis = redis;
|
||||||
|
_logger = logger;
|
||||||
|
reloadClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool reloadClient()
|
||||||
|
{
|
||||||
|
var malCredentials = _redis.HashGetAll("malCredentials");
|
||||||
|
if (malCredentials.Length != 0)
|
||||||
|
{
|
||||||
|
_credentials = new CredentialContext();
|
||||||
|
foreach (var c in malCredentials)
|
||||||
|
{
|
||||||
|
switch (c.Name)
|
||||||
|
{
|
||||||
|
case "Username":
|
||||||
|
_credentials.UserName = c.Value;
|
||||||
|
break;
|
||||||
|
case "Password":
|
||||||
|
_credentials.Password = c.Value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_animeSearch = new AnimeSearchMethodsAsync(_credentials);
|
||||||
|
_mangaSearch = new MangaSearchMethodsAsync(_credentials);
|
||||||
|
_logger.Verbose($"[Geekbot] [MalClient] Logged in to Mal");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
_logger.Warning("[Geekbot] [MalClient] No 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IMalClient
|
||||||
|
{
|
||||||
|
bool reloadClient();
|
||||||
|
bool isLoggedIn();
|
||||||
|
Task<AnimeEntry> getAnime(string query);
|
||||||
|
Task<MangaEntry> getManga(string query);
|
||||||
|
}
|
||||||
|
}
|
116
Geekbot.net/Modules/mal.cs
Normal file
116
Geekbot.net/Modules/mal.cs
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Modules
|
||||||
|
{
|
||||||
|
public class mal : ModuleBase
|
||||||
|
{
|
||||||
|
private readonly IMalClient _malClient;
|
||||||
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
|
||||||
|
public mal(IMalClient malClient, IErrorHandler errorHandler)
|
||||||
|
{
|
||||||
|
_malClient = malClient;
|
||||||
|
_errorHandler = errorHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("anime", RunMode = RunMode.Async)]
|
||||||
|
[Summary("Show Info about an Anime.")]
|
||||||
|
public async Task searchAnime([Remainder, Summary("AnimeName")] string animeName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_malClient.isLoggedIn())
|
||||||
|
{
|
||||||
|
var anime = await _malClient.getAnime(animeName);
|
||||||
|
if (anime != null)
|
||||||
|
{
|
||||||
|
var eb = new EmbedBuilder();
|
||||||
|
|
||||||
|
var description = System.Web.HttpUtility.HtmlDecode(anime.Synopsis)
|
||||||
|
.Replace("<br />", "")
|
||||||
|
.Replace("[i]", "*")
|
||||||
|
.Replace("[/i]", "*");
|
||||||
|
|
||||||
|
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 (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context, "Something went wrong...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("manga", RunMode = RunMode.Async)]
|
||||||
|
[Summary("Show Info about a Manga.")]
|
||||||
|
public async Task searchManga([Remainder, Summary("MangaName")] string mangaName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_malClient.isLoggedIn())
|
||||||
|
{
|
||||||
|
var manga = await _malClient.getManga(mangaName);
|
||||||
|
if (manga != null)
|
||||||
|
{
|
||||||
|
var eb = new EmbedBuilder();
|
||||||
|
|
||||||
|
var description = System.Web.HttpUtility.HtmlDecode(manga.Synopsis)
|
||||||
|
.Replace("<br />", "")
|
||||||
|
.Replace("[i]", "*")
|
||||||
|
.Replace("[/i]", "*");
|
||||||
|
|
||||||
|
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 (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context, "Something went wrong...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -109,6 +109,7 @@ namespace Geekbot.net
|
||||||
var fortunes = new FortunesProvider(RandomClient, logger);
|
var fortunes = new FortunesProvider(RandomClient, logger);
|
||||||
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
||||||
var pandaImages = new PandaProvider(RandomClient, logger);
|
var pandaImages = new PandaProvider(RandomClient, logger);
|
||||||
|
var malClient = new MalClient(redis, logger);
|
||||||
|
|
||||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton(redis);
|
services.AddSingleton(redis);
|
||||||
|
@ -118,6 +119,7 @@ namespace Geekbot.net
|
||||||
services.AddSingleton<IFortunesProvider>(fortunes);
|
services.AddSingleton<IFortunesProvider>(fortunes);
|
||||||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||||
services.AddSingleton<IPandaProvider>(pandaImages);
|
services.AddSingleton<IPandaProvider>(pandaImages);
|
||||||
|
services.AddSingleton<IMalClient>(malClient);
|
||||||
|
|
||||||
logger.Information("[Geekbot] Connecting to Discord");
|
logger.Information("[Geekbot] Connecting to Discord");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue