From 8dbf8a980b9dd195da0ba26411ba1b6da5a7c4d3 Mon Sep 17 00:00:00 2001 From: metehus Date: Thu, 9 Apr 2020 18:27:54 -0300 Subject: [PATCH 1/3] a --- src/listeners/MessageListener.js | 11 +++++++++-- src/structures/api/ApiWrapper.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/structures/api/ApiWrapper.js diff --git a/src/listeners/MessageListener.js b/src/listeners/MessageListener.js index eff4109..eb1ee08 100644 --- a/src/listeners/MessageListener.js +++ b/src/listeners/MessageListener.js @@ -7,7 +7,14 @@ module.exports = class MessageListener extends Listener { super({ discordEvents }, client) } - onMessageCreate (message) { - this.client.logger.info(message.content, { label: 'Message' }) + onMessageCreate ({ content }) { + // isso é só pra teste até lançar as nova parada do discord e a gente fazer com base nelas + + if (!content) return + + if (content.startsWith('spn!city search')) { + const query = content.replace('spn!city search ', '') + console.log(query) + } } } \ No newline at end of file diff --git a/src/structures/api/ApiWrapper.js b/src/structures/api/ApiWrapper.js new file mode 100644 index 0000000..5f8f852 --- /dev/null +++ b/src/structures/api/ApiWrapper.js @@ -0,0 +1,11 @@ +const { createOptionHandler } = require('../../utils') + +module.exports = class ApiWrapper { + constructor (options, client) { + options = createOptionHandler('Loader', options) + + this.name = options.optional('name', this.constructor.name) + this.provider = options.optional('provider', null) + this.envVars = options.optional('provider', null) + } +} \ No newline at end of file From 20d4fc0337535092bee228576ece3c11d17f6c6e Mon Sep 17 00:00:00 2001 From: metehus Date: Mon, 20 Apr 2020 22:04:54 -0300 Subject: [PATCH 2/3] api --- package.json | 1 + src/apis/Deezer.js | 27 +++++++++++++++++++++++++ src/listeners/MessageListener.js | 10 +++++++--- src/loaders/ApiLoader.js | 24 ++++++++++++++++++++++ src/loaders/ListenerLoader.js | 2 +- src/loaders/index.js | 3 ++- src/structures/Loader.js | 25 ++++++++++++++++++----- src/structures/api/ApiWrapper.js | 19 +++++++++++++++++- src/structures/api/index.js | 3 +++ src/structures/base/Switchblade.js | 32 ++++++++++++++++++++---------- src/structures/index.js | 3 ++- 11 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 src/apis/Deezer.js create mode 100644 src/loaders/ApiLoader.js create mode 100644 src/structures/api/index.js diff --git a/package.json b/package.json index 269870b..2e41792 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/SwitchbladeBot/switchblade-next#readme", "dependencies": { + "axios": "^0.19.2", "chalk": "^3.0.0", "eris": "^0.11.2", "winston": "^3.2.1" diff --git a/src/apis/Deezer.js b/src/apis/Deezer.js new file mode 100644 index 0000000..fcc29e0 --- /dev/null +++ b/src/apis/Deezer.js @@ -0,0 +1,27 @@ +const { ApiWrapper } = require('../structures/api') +const axios = require('axios') + +class DeezerAPI extends ApiWrapper { + constructor(client) { + super({ name: 'deezer' }, client) + } + + load () { + return axios.create({ + baseURL: 'https://api.deezer.com/', + responseType: 'json' + }) + } + + getTrack (id) { + return this.request('fala') + } + + search(q) { + return this.request.get('search', { + params: { q } + }).then(r => r.data) + } +} + +module.exports = DeezerAPI diff --git a/src/listeners/MessageListener.js b/src/listeners/MessageListener.js index eb1ee08..7a51550 100644 --- a/src/listeners/MessageListener.js +++ b/src/listeners/MessageListener.js @@ -7,14 +7,18 @@ module.exports = class MessageListener extends Listener { super({ discordEvents }, client) } - onMessageCreate ({ content }) { + onMessageCreate ({ content, channel }) { // isso é só pra teste até lançar as nova parada do discord e a gente fazer com base nelas if (!content) return - if (content.startsWith('spn!city search')) { - const query = content.replace('spn!city search ', '') + if (content.startsWith('psh!user get')) { + const query = content.replace('psh!user get ', '') console.log(query) + this.client.apis.deezer.search(query).then(a => { + console.log(a.data[0]) + this.client.createMessage(channel.id, a.data[0].artist.picture_big) + }) } } } \ No newline at end of file diff --git a/src/loaders/ApiLoader.js b/src/loaders/ApiLoader.js new file mode 100644 index 0000000..e03426d --- /dev/null +++ b/src/loaders/ApiLoader.js @@ -0,0 +1,24 @@ +const { Loader, ApiWrapper } = require('../structures') + +module.exports = class ApiLoader extends Loader { + constructor(client) { + super({ critical: true, name: 'APIs', priority: Loader.LoaderPriority.HIGH }, client) + this.client = client + this.client.apis = {} + } + + load() { + return this.loadFiles('src/apis') + } + + loadFile(NewAPI) { + const api = new NewAPI(this.client) + if (!(api instanceof ApiWrapper)) throw new Error(`Failed to load ${api.name}: not an api`) + + if (api.preLoad()) { + this.client.apis[api.name] = api + return true + } + return false + } +} \ No newline at end of file diff --git a/src/loaders/ListenerLoader.js b/src/loaders/ListenerLoader.js index 28ae8b6..ae68bc1 100644 --- a/src/loaders/ListenerLoader.js +++ b/src/loaders/ListenerLoader.js @@ -2,7 +2,7 @@ const { Loader, Listener } = require('../structures') module.exports = class ListenerLoader extends Loader { constructor(client) { - super({ critical: true, name: 'Listeners' }, client) + super({ critical: true, name: 'Listeners', priority: Loader.LoaderPriority.LOW }, client) this.listeners = [] } diff --git a/src/loaders/index.js b/src/loaders/index.js index a2b5a80..183be7c 100644 --- a/src/loaders/index.js +++ b/src/loaders/index.js @@ -1,3 +1,4 @@ module.exports = { - ListenerLoader: require('./ListenerLoader.js') + ListenerLoader: require('./ListenerLoader.js'), + ApiLoader: require('./ApiLoader.js') } \ No newline at end of file diff --git a/src/structures/Loader.js b/src/structures/Loader.js index df403b7..00997f0 100644 --- a/src/structures/Loader.js +++ b/src/structures/Loader.js @@ -1,5 +1,17 @@ const { createOptionHandler, FileUtils } = require('../utils') +/** + * Loader priority, in this order: + * HIGH + * NORMAL + * LOW + */ +const LoaderPriority = { + HIGH: 0, + NORMAL: 1, + LOW: 2 +} + module.exports = class Loader { /** * @param {Object} opts @@ -12,6 +24,7 @@ module.exports = class Loader { this.critical = options.optional('critical', false) this.name = options.optional('name', this.constructor.name) + this.priority = options.optional('priority', LoaderPriority.NORMAL) this.client = client } @@ -22,7 +35,7 @@ module.exports = class Loader { if (!success) throw new Error(`Unhandled error`) return success } catch (e) { - this.client.logger.error(`Failed to load ${this.name}`, { label: 'Loader' }) + this.client.logger.error(`Failed to load ${this.name}`, { label: 'Loaders' }) return false } } @@ -32,7 +45,7 @@ module.exports = class Loader { let success = 0 let fails = 0 const errorFunction = e => { - this.client.logger.error(e) + this.client.logger.error(e, { label: 'Loaders'}) fails++ } const successFunction = file => { @@ -45,8 +58,8 @@ module.exports = class Loader { } } await FileUtils.requireDirectory(path, successFunction, errorFunction, recursive).then(() => { - if (fails) this.client.logger.warn(`${success} types of ${this.name} loaded, ${fails} failed.`, { label: this.name }) - else this.client.logger.info(`All ${success} types of ${this.name} loaded without errors.`, { label: this.name }) + if (fails) this.client.logger.warn(`${success} types of ${this.name} loaded, ${fails} failed.`, { label: 'Loaders' }) + else this.client.logger.info(`All ${success} types of ${this.name} loaded without errors.`, { label: 'Loaders' }) }) return true } @@ -58,4 +71,6 @@ module.exports = class Loader { loadFile(file) { throw new Error(`The ${this.name} loader has not implemented the loadFile() function`) } -} \ No newline at end of file +} + +module.exports.LoaderPriority = LoaderPriority diff --git a/src/structures/api/ApiWrapper.js b/src/structures/api/ApiWrapper.js index 5f8f852..0bab1cb 100644 --- a/src/structures/api/ApiWrapper.js +++ b/src/structures/api/ApiWrapper.js @@ -6,6 +6,23 @@ module.exports = class ApiWrapper { this.name = options.optional('name', this.constructor.name) this.provider = options.optional('provider', null) - this.envVars = options.optional('provider', null) + this.envVars = options.optional('envVars', null) + this.request = null + } + + preLoad () { + if (this.envVars) { + for (const envVar of this.envVars) + if (!process.env[envVar]) return false + } + + this.request = this.load(); + + if (!this.request) return false; + return true + } + + load () { + return null; } } \ No newline at end of file diff --git a/src/structures/api/index.js b/src/structures/api/index.js new file mode 100644 index 0000000..b6de275 --- /dev/null +++ b/src/structures/api/index.js @@ -0,0 +1,3 @@ +module.exports = { + ApiWrapper: require('./ApiWrapper.js') +} \ No newline at end of file diff --git a/src/structures/base/Switchblade.js b/src/structures/base/Switchblade.js index 1445af8..61d615c 100644 --- a/src/structures/base/Switchblade.js +++ b/src/structures/base/Switchblade.js @@ -1,8 +1,9 @@ const { CommandClient } = require('eris') const Loaders = require('../../loaders') +const { LoaderPriority } = require('../Loader.js') const { readFileSync } = require('fs') -const chalk = require('chalk') const winston = require('winston') +const chalk = require('chalk') module.exports = class Switchblade extends CommandClient { constructor(token, options, commandOptions) { @@ -15,7 +16,7 @@ module.exports = class Switchblade extends CommandClient { if (process.env.NODE_ENV !== 'production') console.log(readFileSync('bigtitle.txt', 'utf8').toString().replace(/{UNICODE}/g, '\u001b[')) this.logger.info('Starting switchblade...', { label: 'Switchblade' }) this.initializeLoaders() - + this.connect() } @@ -38,16 +39,27 @@ module.exports = class Switchblade extends CommandClient { } } - async initializeLoaders () { + async initializeLoaders() { + const loaders = [[], [], []] for (let file in Loaders) { const loader = new Loaders[file](this) - let success = true - try { - success = await loader.preLoad() - } catch (error) { - this.logger.error(error.stack, { label: 'Loaders', stack: error.stack }) - } finally { - if (!success && loader.critical) process.exit(1) + loaders[loader.priority].push(loader) + } + + for (let loaderPriority of loaders) { + const priority = Object.keys(LoaderPriority)[loaders.indexOf(loaderPriority)] + this.logger.info(`Starting to load ${chalk.yellow(priority)}`, { label: 'Loaders' }) + + for (let loader of loaderPriority) { + let success = true + + try { + success = await loader.preLoad() + } catch (error) { + this.logger.error(error.stack, { label: 'Loaders', stack: error.stack }) + } finally { + if (!success && loader.critical) process.exit(1) + } } } } diff --git a/src/structures/index.js b/src/structures/index.js index 35d466e..a5523aa 100644 --- a/src/structures/index.js +++ b/src/structures/index.js @@ -1,5 +1,6 @@ module.exports = { // Loader Loader: require('./Loader.js'), - Listener: require('./Listener.js') + Listener: require('./Listener.js'), + ApiWrapper: require('./api/ApiWrapper.js') } From 210430a4fdd0cf30290674f6f636567e94cae8c4 Mon Sep 17 00:00:00 2001 From: metehus Date: Sun, 3 May 2020 22:29:53 -0300 Subject: [PATCH 3/3] a --- src/apis/Deezer.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/apis/Deezer.js b/src/apis/Deezer.js index fcc29e0..bc9eece 100644 --- a/src/apis/Deezer.js +++ b/src/apis/Deezer.js @@ -13,10 +13,6 @@ class DeezerAPI extends ApiWrapper { }) } - getTrack (id) { - return this.request('fala') - } - search(q) { return this.request.get('search', { params: { q }