From 91a243a0dc5f471f31b1645c0f5045859507e034 Mon Sep 17 00:00:00 2001 From: void Date: Wed, 22 Jan 2020 00:29:12 +0100 Subject: [PATCH] reworked Tautulli commands to use new API handler, moved resources to resources folder, renamed refreshlibrary command to refreshlibraries --- README.md | 2 +- src/api_handlers/api.js | 8 ++--- src/api_handlers/tautulli.js | 37 ++++++++++++++++++++++ src/commands/tautulli/libraries.js | 28 +++++++--------- src/commands/tautulli/refreshlibraries.js | 24 ++++++++++++++ src/commands/tautulli/refreshlibrary.js | 30 ------------------ src/index.js | 4 +-- src/resources/libraries.png | Bin 0 -> 556 bytes logo.png => src/resources/logo.png | Bin 9 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 src/commands/tautulli/refreshlibraries.js delete mode 100644 src/commands/tautulli/refreshlibrary.js create mode 100644 src/resources/libraries.png rename logo.png => src/resources/logo.png (100%) diff --git a/README.md b/README.md index 9fc1930..680ca9f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Mellow [![Discord](https://img.shields.io/badge/Discord-Invite-7289DA.svg?style=flat-square)](https://discord.gg/zx2BWp2) [![Docker](https://img.shields.io/badge/Docker-Hub-lightblue.svg?style=flat-square)](https://cloud.docker.com/u/voidp/repository/docker/voidp/mellow) [![Run on Repl.it](https://repl.it/badge/github/v0idp/Mellow)](https://repl.it/github/v0idp/Mellow)

- +

Mellow can communicate with several APIs like Ombi, Sonarr, Radarr and Tautulli which are related to home streaming to use those services directly in your Discord client. diff --git a/src/api_handlers/api.js b/src/api_handlers/api.js index 1831370..f7f0280 100644 --- a/src/api_handlers/api.js +++ b/src/api_handlers/api.js @@ -5,10 +5,10 @@ const Tautulli = require('./tautulli.js'); class APIHandler { constructor (config) { - this.ombi = new Ombi(config); - this.radarr = new Radarr(config); - this.sonarr = new Sonarr(config); - this.tautulli = new Tautulli(config); + this.ombi = new Ombi(config.ombi); + this.radarr = new Radarr(config.radarr); + this.sonarr = new Sonarr(config.sonarr); + this.tautulli = new Tautulli(config.tautulli); } } diff --git a/src/api_handlers/tautulli.js b/src/api_handlers/tautulli.js index ade19f4..0308597 100644 --- a/src/api_handlers/tautulli.js +++ b/src/api_handlers/tautulli.js @@ -1,6 +1,43 @@ +const { get, getURL } = require('./../util.js'); + class Tautulli { constructor(config) { this.config = config; + this.endpoints = { + "libraries": getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v2?apikey=' + config.apikey + '&cmd=get_libraries'), + "refresh": getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v2?apikey=' + config.apikey + '&cmd=refresh_libraries_list') + }; + } + + getLibraries() { + return new Promise((resolve, reject) => { + get({ + headers: {'accept' : 'application/json', + 'User-Agent': `Mellow/${process.env.npm_package_version}`}, + url: this.endpoints["libraries"] + }).then((response) => { + let jsonResponse = JSON.parse(response.body); + resolve(jsonResponse); + }).catch((err) => { + console.log(err); + reject(); + }); + }); + } + + refreshLibraries() { + return new Promise((resolve, reject) => { + get({ + headers: {'accept' : 'application/json', + 'User-Agent': `Mellow/${process.env.npm_package_version}`}, + url: this.endpoints["refresh"] + }).then(() => { + resolve(); + }).catch((err) => { + console.log(err); + reject(); + }); + }); } } diff --git a/src/commands/tautulli/libraries.js b/src/commands/tautulli/libraries.js index def86fc..d196ac4 100644 --- a/src/commands/tautulli/libraries.js +++ b/src/commands/tautulli/libraries.js @@ -1,6 +1,7 @@ const Discord = require('discord.js'); const commando = require('discord.js-commando'); -const {deleteCommandMessages, get, getURL} = require('../../util.js'); +const path = require('path'); +const { deleteCommandMessages } = require('../../util.js'); module.exports = class librariesCommand extends commando.Command { constructor (client) { @@ -15,30 +16,25 @@ module.exports = class librariesCommand extends commando.Command { } run (msg) { - const tautulli = this.client.webDatabase.webConfig.tautulli; - get({ - headers: {'accept' : 'application/json', - 'User-Agent': `Mellow/${process.env.npm_package_version}`}, - url: getURL(tautulli.host, tautulli.port, tautulli.ssl, tautulli.baseurl + '/api/v2?apikey=' + tautulli.apikey + '&cmd=get_libraries') - }).then((resolve) => { - let jsonObject = JSON.parse(resolve.body); + this.client.API.tautulli.getLibraries().then((jsonResponse) => { let libraryEmbed = new Discord.MessageEmbed() .setTitle('Server Libraries') .setTimestamp(new Date()) - .setThumbnail('https://i.imgur.com/pz9PoqR.png'); - for (let i = 0; i < Object.keys(jsonObject.response.data).length; i++) { - let obj = jsonObject.response.data[i]; + .attachFiles(path.join(__dirname, '..', '..', 'resources', 'libraries.png')) + .setThumbnail('attachment://libraries.png'); + for (let i = 0; i < Object.keys(jsonResponse.response.data).length; i++) { + let obj = jsonResponse.response.data[i]; if (obj.section_type == 'movie') { libraryEmbed.addField(obj.section_name, obj.count, true); } else if (obj.section_type == 'show') { libraryEmbed.addField(obj.section_name, `${obj.count} Shows\n${obj.parent_count} Seasons\n${obj.child_count} Episodes`, true); } } - deleteCommandMessages(msg, this.client); - msg.embed(libraryEmbed); - }).catch((error) => { - console.error(error); - return msg.reply('There was an error in your request.'); + deleteCommandMessages(msg); + return msg.embed(libraryEmbed); + }).catch(() => { + deleteCommandMessages(msg); + return msg.reply('Couldn\'t get libraries! Something went wrong.'); }); } }; diff --git a/src/commands/tautulli/refreshlibraries.js b/src/commands/tautulli/refreshlibraries.js new file mode 100644 index 0000000..716cdd7 --- /dev/null +++ b/src/commands/tautulli/refreshlibraries.js @@ -0,0 +1,24 @@ +const commando = require('discord.js-commando'); +const {deleteCommandMessages, get, getURL} = require('../../util.js'); + +module.exports = class refreshLibrariesCommand extends commando.Command { + constructor (client) { + super(client, { + 'name': 'refreshlibraries', + 'memberName': 'refreshlibraries', + 'group': 'tautulli', + 'description': 'refresh all libraries in tautulli', + 'examples': ['refreshlibraries'], + 'guildOnly': true + }); + } + + run (msg) { + this.client.API.tautulli.refreshLibraries().then(() => { + deleteCommandMessages(msg); + return msg.reply('Refreshed all libraries in Tautulli.'); + }).catch(() => { + return msg.reply('Couldn\'t refresh libraries! Something went wrong.'); + }); + } +}; diff --git a/src/commands/tautulli/refreshlibrary.js b/src/commands/tautulli/refreshlibrary.js deleted file mode 100644 index d6cbca9..0000000 --- a/src/commands/tautulli/refreshlibrary.js +++ /dev/null @@ -1,30 +0,0 @@ -const commando = require('discord.js-commando'); -const {deleteCommandMessages, get, getURL} = require('../../util.js'); - -module.exports = class refreshLibraryCommand extends commando.Command { - constructor (client) { - super(client, { - 'name': 'refreshlibrary', - 'memberName': 'refreshlibrary', - 'group': 'tautulli', - 'description': 'refresh all libraries in tautulli', - 'examples': ['refreshlibrary'], - 'guildOnly': true - }); - } - - run (msg) { - const tautulli = this.client.webDatabase.webConfig.tautulli; - get({ - headers: {'accept' : 'application/json', - 'User-Agent': `Mellow/${process.env.npm_package_version}`}, - url: getURL(tautulli.host, tautulli.port, tautulli.ssl, tautulli.baseurl + '/api/v2?apikey=' + tautulli.apikey + '&cmd=refresh_libraries_list') - }).then((resolve) => { - deleteCommandMessages(msg, this.client); - msg.reply('Refreshed all libraries in Tautulli.'); - }).catch((error) => { - console.error(error); - return msg.reply('There was an error in your request.'); - }); - } -}; diff --git a/src/index.js b/src/index.js index e17b459..7dac1a2 100644 --- a/src/index.js +++ b/src/index.js @@ -11,10 +11,10 @@ const start = function () { if (botConfig && botConfig.token) { bot = new BotClient(webDatabase, botConfig.ownerid, botConfig.commandprefix); bot.init().catch((err) => { - console.log('Failed initializing DiscordBot! Please check your bot settings.'); + console.log('Failed initializing DiscordBot! Please check your bot configurations.'); console.error(err); }); - } else console.log('There is no bot token provided. Please check your settings!'); + } else console.log('There is no bot token provided. Please check your configurations.'); new WebServer(webDatabase, bot).init(); }).catch(console.error); } diff --git a/src/resources/libraries.png b/src/resources/libraries.png new file mode 100644 index 0000000000000000000000000000000000000000..d422a8a98dba7526a8965d18d4576470e4a762f2 GIT binary patch literal 556 zcmV+{0@MA8P)F3 zG-61)qzE=P7S>iN@-GBi#V(CSLJA>W{Mteb<=9-;y94{!#|}Jm;K6z4&fI(7%-lN< z3PzB$S#$y_GJ$*1`)}Zn@eW|b_%3jo)3=|$2OJyk0|siNRF98NA9DI|&fxLfJgir!rCkhTQ@BDu7hQiA*zKjmJ>Dl*zUgM|0WlrCI3mb01t-B4kfgR)Bz=H7& z;2@{(XTY-YE#R;53NT$86;;KG(|scvAtxjay7{9{f0tE;gOaY>WPjeE8K_8VH$G5j zX|fTux&jSRcp1hCd