diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 0da2296..5764975 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -14,14 +14,10 @@ 1.0.2 - - - - 1.5.0.1 - + diff --git a/Geekbot.net/Lib/MalClient.cs b/Geekbot.net/Lib/MalClient.cs new file mode 100644 index 0000000..4827b13 --- /dev/null +++ b/Geekbot.net/Lib/MalClient.cs @@ -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 getAnime(string query) + { + var response = await _animeSearch.SearchDeserializedAsync(query); + return response.Entries.Count == 0 ? null : response.Entries[0]; + } + + public async Task 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 getAnime(string query); + Task getManga(string query); + } +} \ No newline at end of file diff --git a/Geekbot.net/Modules/mal.cs b/Geekbot.net/Modules/mal.cs new file mode 100644 index 0000000..662e7c8 --- /dev/null +++ b/Geekbot.net/Modules/mal.cs @@ -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("
", "") + .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("
", "") + .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..."); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index cd43c43..4cbc953 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -109,6 +109,7 @@ namespace Geekbot.net var fortunes = new FortunesProvider(RandomClient, logger); var checkEmImages = new CheckEmImageProvider(RandomClient, logger); var pandaImages = new PandaProvider(RandomClient, logger); + var malClient = new MalClient(redis, logger); services.AddSingleton(errorHandler); services.AddSingleton(redis); @@ -118,6 +119,7 @@ namespace Geekbot.net services.AddSingleton(fortunes); services.AddSingleton(checkEmImages); services.AddSingleton(pandaImages); + services.AddSingleton(malClient); logger.Information("[Geekbot] Connecting to Discord");