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..bc9eece --- /dev/null +++ b/src/apis/Deezer.js @@ -0,0 +1,23 @@ +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' + }) + } + + 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 eff4109..7a51550 100644 --- a/src/listeners/MessageListener.js +++ b/src/listeners/MessageListener.js @@ -7,7 +7,18 @@ module.exports = class MessageListener extends Listener { super({ discordEvents }, client) } - onMessageCreate (message) { - this.client.logger.info(message.content, { label: 'Message' }) + 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('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 new file mode 100644 index 0000000..0bab1cb --- /dev/null +++ b/src/structures/api/ApiWrapper.js @@ -0,0 +1,28 @@ +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('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') }