diff --git a/commands/search/github.js b/commands/search/github.js index e395b1e0..cf4e3c55 100644 --- a/commands/search/github.js +++ b/commands/search/github.js @@ -1,7 +1,7 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); -const { shorten } = require('../../util/Util'); +const { shorten, base64 } = require('../../util/Util'); const { GITHUB_USERNAME, GITHUB_PASSWORD } = process.env; module.exports = class GitHubCommand extends Command { @@ -34,7 +34,7 @@ module.exports = class GitHubCommand extends Command { try { const { body } = await snekfetch .get(`https://api.github.com/repos/${author}/${repository}`) - .set({ Authorization: `Basic ${Buffer.from(`${GITHUB_USERNAME}:${GITHUB_PASSWORD}`).toString('base64')}` }); + .set({ Authorization: `Basic ${base64(`${GITHUB_USERNAME}:${GITHUB_PASSWORD}`)}` }); const embed = new MessageEmbed() .setColor(0xFFFFFF) .setAuthor('GitHub', 'https://i.imgur.com/e4HunUm.png', 'https://github.com/') diff --git a/commands/search/my-anime-list-anime.js b/commands/search/my-anime-list-anime.js index 4f331d64..e2caa72e 100644 --- a/commands/search/my-anime-list-anime.js +++ b/commands/search/my-anime-list-anime.js @@ -4,7 +4,7 @@ const snekfetch = require('snekfetch'); const { parseString } = require('xml2js'); const { promisify } = require('util'); const xml = promisify(parseString); -const { shorten, cleanXML } = require('../../util/Util'); +const { shorten, base64, cleanXML } = require('../../util/Util'); const { MAL_USERNAME, MAL_PASSWORD } = process.env; module.exports = class MyAnimeListAnimeCommand extends Command { @@ -31,7 +31,7 @@ module.exports = class MyAnimeListAnimeCommand extends Command { const { text } = await snekfetch .get('https://myanimelist.net/api/anime/search.xml') .query({ q: query }) - .set({ Authorization: `Basic ${Buffer.from(`${MAL_USERNAME}:${MAL_PASSWORD}`).toString('base64')}` }); + .set({ Authorization: `Basic ${base64(`${MAL_USERNAME}:${MAL_PASSWORD}`)}` }); const body = await xml(text); const data = body.anime.entry[0]; const embed = new MessageEmbed() diff --git a/commands/search/my-anime-list-manga.js b/commands/search/my-anime-list-manga.js index 2db94267..5041e7f0 100644 --- a/commands/search/my-anime-list-manga.js +++ b/commands/search/my-anime-list-manga.js @@ -4,7 +4,7 @@ const snekfetch = require('snekfetch'); const { parseString } = require('xml2js'); const { promisify } = require('util'); const xml = promisify(parseString); -const { shorten, cleanXML } = require('../../util/Util'); +const { shorten, base64, cleanXML } = require('../../util/Util'); const { MAL_USERNAME, MAL_PASSWORD } = process.env; module.exports = class MyAnimeListMangaCommand extends Command { @@ -31,7 +31,7 @@ module.exports = class MyAnimeListMangaCommand extends Command { const { text } = await snekfetch .get('https://myanimelist.net/api/manga/search.xml') .query({ q: query }) - .set({ Authorization: `Basic ${Buffer.from(`${MAL_USERNAME}:${MAL_PASSWORD}`).toString('base64')}` }); + .set({ Authorization: `Basic ${base64(`${MAL_USERNAME}:${MAL_PASSWORD}`)}` }); const body = await xml(text); const data = body.manga.entry[0]; const embed = new MessageEmbed() diff --git a/commands/search/twitter.js b/commands/search/twitter.js index 7b51b713..cb37c93a 100644 --- a/commands/search/twitter.js +++ b/commands/search/twitter.js @@ -1,6 +1,7 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const { base64 } = require('../../util/Util'); const { TWITTER_KEY, TWITTER_SECRET } = process.env; module.exports = class TwitterCommand extends Command { @@ -64,7 +65,7 @@ module.exports = class TwitterCommand extends Command { const { body } = await snekfetch .post('https://api.twitter.com/oauth2/token') .set({ - Authorization: `Basic ${Buffer.from(`${TWITTER_KEY}:${TWITTER_SECRET}`).toString('base64')}`, + Authorization: `Basic ${base64(`${TWITTER_KEY}:${TWITTER_SECRET}`)}`, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }) .send('grant_type=client_credentials'); diff --git a/commands/text-edit/base64.js b/commands/text-edit/base64.js index c47eac37..b1df79d6 100644 --- a/commands/text-edit/base64.js +++ b/commands/text-edit/base64.js @@ -1,4 +1,5 @@ const { Command } = require('discord.js-commando'); +const { base64 } = require('../../util/Util'); module.exports = class Base64Command extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class Base64Command extends Command { prompt: 'What text would you like to convert to Base64?', type: 'string', validate: text => { - if (Buffer.from(text).toString('base64').length < 2000) return true; + if (base64(text).length < 2000) return true; return 'Invalid text, your text is too long.'; } } @@ -23,6 +24,6 @@ module.exports = class Base64Command extends Command { } run(msg, { text }) { - return msg.say(Buffer.from(text).toString('base64')); + return msg.say(base64(text)); } }; diff --git a/util/Util.js b/util/Util.js index b6d4192e..d564f969 100644 --- a/util/Util.js +++ b/util/Util.js @@ -51,6 +51,10 @@ class Util { return arr; } + static base64(text) { + return Buffer.from(text).toString('base64'); + } + static cleanXML(text) { return text .replace(/
/g, '')