diff --git a/lib/commands/AnilistApiCommands/AniListCommandsLogic.js b/lib/commands/AnilistApiCommands/AniListCommandsLogic.js index 173514f..493dfa0 100644 --- a/lib/commands/AnilistApiCommands/AniListCommandsLogic.js +++ b/lib/commands/AnilistApiCommands/AniListCommandsLogic.js @@ -1,4 +1,5 @@ -const cmdLib = require('../../../CommandLib'), +const cmdLib = require('../../CommandLib'), + anilistApi = require('../../api/AnilistApi'), yaml = require('js-yaml'), fsx = require('fs-extra'), templateFile = 'AniListCommandsTemplate.yaml'; @@ -44,13 +45,53 @@ class RichMediaInfo extends cmdLib.ExtendedRichEmbed { let template = null; +/** + * Initializes the module. + * @returns {Promise} + */ async function init() { - let templateString = fsx.readFile(templateFile, {encoding: 'utf-8'}); + let templateString = await fsx.readFile(templateFile, {encoding: 'utf-8'}); template = yaml.safeLoad(templateString); } +/** + * Registers the commands to the CommandHandler. + * @param commandHandler {cmdLib.CommandHandler} + * @returns {Promise} + */ +async function register(commandHandler) { + // creating commands + let animeSearch = new cmdLib.Command( + template.anime_search, + new cmdLib.Answer(async (m, k, s) => { + try { + let animeData = await anilistApi.searchAnimeByName(s); + return new RichMediaInfo(animeData); + } catch (err) { + return template.anime_search.not_found; + } + })); + + let mangaSearch = new cmdLib.Command( + template.manga_search, + new cmdLib.Answer(async (m, k, s) => { + try { + let mangaData = await anilistApi.searchMangaByName(s); + return new RichMediaInfo(mangaData); + } catch (err) { + return template.manga_search.not_found; + } + }) + ); + + // registering commands + commandHandler.registerCommand(template.anime_search.name, animeSearch); + commandHandler.registerCommand(template.manga_search.name, mangaSearch); +} + // -- exports -- // Object.assign(exports, { - init: init + init: init, + register: register }); diff --git a/lib/commands/AnilistApiCommands/AniListCommandsTemplate.yaml b/lib/commands/AnilistApiCommands/AniListCommandsTemplate.yaml index e69de29..400d567 100644 --- a/lib/commands/AnilistApiCommands/AniListCommandsTemplate.yaml +++ b/lib/commands/AnilistApiCommands/AniListCommandsTemplate.yaml @@ -0,0 +1,23 @@ +anime_search: + name: anime + permission: all + usage: anime [search query] + description: > + Searches AniList.co for the anime Title and returns information about + it if there is an result. + category: AniList + response: + not_found: > + I couldn't find the anime you were searching for :( + +manga_search: + name: manga + permission: all + usage: manga [search query] + description: > + Searches AniList.co for the manga Title and returns information about + it if there is an result. + category: AniList + response: + not_found: > + I couldn't find the manga you were searching for :(