From fd647e14e72192ecae290bd659af4134aa3d8494 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 29 Oct 2020 12:43:24 -0400 Subject: [PATCH] Fix lots of code dupe --- commands/analyze/gender.js | 3 ++- commands/edit-image/ace-attorney.js | 8 +------- commands/edit-image/charcoal.js | 12 ++---------- commands/edit-image/emboss.js | 12 ++---------- commands/edit-image/implode.js | 12 ++---------- commands/edit-image/liquid-rescale.js | 12 ++---------- commands/edit-image/noise.js | 13 ++----------- commands/edit-image/oil-painting.js | 12 ++---------- commands/edit-image/sketch.js | 12 ++---------- commands/edit-image/squish.js | 12 ++---------- commands/edit-image/swirl.js | 12 ++---------- commands/games-sp/jeopardy.js | 5 ++--- commands/games-sp/whos-that-pokemon.js | 2 +- commands/random-res/random-user.js | 4 ++-- commands/voice/airhorn.js | 5 ++--- commands/voice/dec-talk.js | 10 ++++------ commands/voice/soundboard.js | 6 ++---- commands/voice/vocodes.js | 11 ++++------- package.json | 2 +- util/Util.js | 21 +++++++++++++++++++++ 20 files changed, 60 insertions(+), 126 deletions(-) diff --git a/commands/analyze/gender.js b/commands/analyze/gender.js index 6c84b179..48b0ba3e 100644 --- a/commands/analyze/gender.js +++ b/commands/analyze/gender.js @@ -20,7 +20,8 @@ module.exports = class GenderCommand extends Command { { key: 'name', prompt: 'What name do you want to determine the gender of?', - type: 'string' + type: 'string', + max: 20 } ] }); diff --git a/commands/edit-image/ace-attorney.js b/commands/edit-image/ace-attorney.js index 9f02d1dc..33dbe290 100644 --- a/commands/edit-image/ace-attorney.js +++ b/commands/edit-image/ace-attorney.js @@ -53,13 +53,7 @@ module.exports = class AceAttorneyCommand extends Command { prompt: `What character do you want to use? Either ${list(Object.keys(characters), 'or')}.`, type: 'string', oneOf: Object.values(characters).reduce((a, b) => a.concat(b)), - parse: character => { - for (const [id, arr] of Object.entries(characters)) { - if (!arr.includes(character.toLowerCase())) continue; - return id; - } - return character.toLowerCase(); - } + parse: character => character.toLowerCase() }, { key: 'quote', diff --git a/commands/edit-image/charcoal.js b/commands/edit-image/charcoal.js index f12ef84e..d325a9e2 100644 --- a/commands/edit-image/charcoal.js +++ b/commands/edit-image/charcoal.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class CharcoalCommand extends Command { constructor(client) { @@ -38,20 +39,11 @@ module.exports = class CharcoalCommand extends Command { const magik = gm(body); magik.charcoal(1); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'sketch.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/emboss.js b/commands/edit-image/emboss.js index bba044e0..3c4ea383 100644 --- a/commands/edit-image/emboss.js +++ b/commands/edit-image/emboss.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class EmbossCommand extends Command { constructor(client) { @@ -38,20 +39,11 @@ module.exports = class EmbossCommand extends Command { const magik = gm(body); magik.emboss(); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'emboss.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/implode.js b/commands/edit-image/implode.js index 246ed98c..3912fba0 100644 --- a/commands/edit-image/implode.js +++ b/commands/edit-image/implode.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class ImplodeCommand extends Command { constructor(client) { @@ -45,20 +46,11 @@ module.exports = class ImplodeCommand extends Command { const magik = gm(body); magik.implode(level / 100); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'implode.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/liquid-rescale.js b/commands/edit-image/liquid-rescale.js index 620aaafa..09978f27 100644 --- a/commands/edit-image/liquid-rescale.js +++ b/commands/edit-image/liquid-rescale.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class LiquidRescaleCommand extends Command { constructor(client) { @@ -41,20 +42,11 @@ module.exports = class LiquidRescaleCommand extends Command { magik.out('50%'); magik.implode(0.25); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'liquid-rescale.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/noise.js b/commands/edit-image/noise.js index 1523e78b..4b6e663e 100644 --- a/commands/edit-image/noise.js +++ b/commands/edit-image/noise.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); -const { list } = require('../../util/Util'); +const { list, magikToBuffer } = require('../../util/Util'); const types = ['uniform', 'gaussian', 'multiplicative', 'impulse', 'laplacian', 'poisson']; module.exports = class NoiseCommand extends Command { @@ -48,20 +48,11 @@ module.exports = class NoiseCommand extends Command { const magik = gm(body); magik.noise(type); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'noise.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/oil-painting.js b/commands/edit-image/oil-painting.js index 45ffa657..3f3ba9e8 100644 --- a/commands/edit-image/oil-painting.js +++ b/commands/edit-image/oil-painting.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class OilPaintingCommand extends Command { constructor(client) { @@ -39,20 +40,11 @@ module.exports = class OilPaintingCommand extends Command { const magik = gm(body); magik.paint(5); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'old-painting.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/sketch.js b/commands/edit-image/sketch.js index 700a0339..36bf62e7 100644 --- a/commands/edit-image/sketch.js +++ b/commands/edit-image/sketch.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class SketchCommand extends Command { constructor(client) { @@ -41,20 +42,11 @@ module.exports = class SketchCommand extends Command { magik.out('-sketch'); magik.out('0x20+120'); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'sketch.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/squish.js b/commands/edit-image/squish.js index 1aa4b2ed..34f3bcce 100644 --- a/commands/edit-image/squish.js +++ b/commands/edit-image/squish.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class SquishCommand extends Command { constructor(client) { @@ -50,20 +51,11 @@ module.exports = class SquishCommand extends Command { magik.out('-liquid-rescale'); magik.out(command); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'squish.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/edit-image/swirl.js b/commands/edit-image/swirl.js index fdddc095..f6969072 100644 --- a/commands/edit-image/swirl.js +++ b/commands/edit-image/swirl.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const gm = require('gm').subClass({ imageMagick: true }); const request = require('node-superfetch'); +const { magikToBuffer } = require('../../util/Util'); module.exports = class SwirlCommand extends Command { constructor(client) { @@ -45,20 +46,11 @@ module.exports = class SwirlCommand extends Command { const magik = gm(body); magik.swirl(degrees); magik.setFormat('png'); - const attachment = await this.toBuffer(magik); + const attachment = await magikToBuffer(magik); if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); return msg.say({ files: [{ attachment, name: 'swirl.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - toBuffer(magik) { - return new Promise((res, rej) => { - magik.toBuffer((err, buffer) => { - if (err) return rej(err); - return res(buffer); - }); - }); - } }; diff --git a/commands/games-sp/jeopardy.js b/commands/games-sp/jeopardy.js index 0f133bf1..cf021d3d 100644 --- a/commands/games-sp/jeopardy.js +++ b/commands/games-sp/jeopardy.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { createCanvas, registerFont } = require('canvas'); const path = require('path'); +const { reactIfAble } = require('../../util/Util'); const { wrapText } = require('../../util/Canvas'); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'OPTIKorinna-Agency.otf'), { family: 'Korinna' }); @@ -47,9 +48,7 @@ module.exports = class JeopardyCommand extends Command { const connection = msg.guild ? this.client.voice.connections.get(msg.guild.id) : null; if (connection) { connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'jeopardy.mp3')); - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react('🔉'); - } + await reactIfAble(msg, this.client.user, '🔉'); } await msg.reply(`The category is: **${question.category.title.toUpperCase()}**. 30 seconds, good luck.`, { files: [{ attachment: clueCard, name: 'clue-card.png' }] diff --git a/commands/games-sp/whos-that-pokemon.js b/commands/games-sp/whos-that-pokemon.js index 0f307a8c..7b2fa80a 100644 --- a/commands/games-sp/whos-that-pokemon.js +++ b/commands/games-sp/whos-that-pokemon.js @@ -107,7 +107,7 @@ module.exports = class WhosThatPokemonCommand extends Command { ctx.font = '60px Pokemon'; ctx.textAlign = 'center'; ctx.textBaseline = 'bottom'; - ctx.lineWidth = 10; + ctx.lineWidth = 8; ctx.strokeStyle = '#3c5aa6'; ctx.strokeText(pokemon.name, 362, 158, 240); ctx.fillStyle = '#ffcb05'; diff --git a/commands/random-res/random-user.js b/commands/random-res/random-user.js index 53781ab1..ee8359b1 100644 --- a/commands/random-res/random-user.js +++ b/commands/random-res/random-user.js @@ -14,8 +14,8 @@ module.exports = class RandomUserCommand extends Command { run(msg) { if (msg.channel.type === 'dm') { const members = [this.client.user, msg.channel.recipient]; - return msg.say(`I choose ${members[Math.floor(Math.random() * members.length)].username}!`); + return msg.say(`I choose ${members[Math.floor(Math.random() * members.length)].tag}!`); } - return msg.say(`I choose ${msg.guild.members.cache.random().displayName}!`); + return msg.say(`I choose ${msg.guild.members.cache.random().user.tag}!`); } }; diff --git a/commands/voice/airhorn.js b/commands/voice/airhorn.js index 60239caf..9b5b4da6 100644 --- a/commands/voice/airhorn.js +++ b/commands/voice/airhorn.js @@ -1,5 +1,6 @@ const Command = require('../../structures/Command'); const path = require('path'); +const { reactIfAble } = require('../../util/Util'); const sounds = require('../../assets/json/airhorn'); module.exports = class AirhornCommand extends Command { @@ -34,9 +35,7 @@ module.exports = class AirhornCommand extends Command { } const airhorn = sounds[Math.floor(Math.random() * sounds.length)]; connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'airhorn', airhorn)); - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react('🔉'); - } + await reactIfAble(msg, this.client.user, '🔉'); return null; } }; diff --git a/commands/voice/dec-talk.js b/commands/voice/dec-talk.js index af127639..6611ae56 100644 --- a/commands/voice/dec-talk.js +++ b/commands/voice/dec-talk.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { Readable } = require('stream'); +const { reactIfAble } = require('../../util/Util'); const { LOADING_EMOJI_ID } = process.env; module.exports = class DECTalkCommand extends Command { @@ -54,18 +55,15 @@ module.exports = class DECTalkCommand extends Command { return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } try { - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react(LOADING_EMOJI_ID); - } + await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); const { body } = await request .get('http://tts.cyzon.us/tts') .query({ text }); connection.play(Readable.from([body])); - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react('🔉'); - } + await reactIfAble(msg, this.client.user, '🔉'); return null; } catch (err) { + await reactIfAble(msg, this.client.user, '⚠️'); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/voice/soundboard.js b/commands/voice/soundboard.js index 7ec7d29c..d8e6cb19 100644 --- a/commands/voice/soundboard.js +++ b/commands/voice/soundboard.js @@ -1,6 +1,6 @@ const Command = require('../../structures/Command'); const path = require('path'); -const { list } = require('../../util/Util'); +const { list, reactIfAble } = require('../../util/Util'); const sounds = require('../../assets/json/soundboard'); module.exports = class SoundboardCommand extends Command { @@ -69,9 +69,7 @@ module.exports = class SoundboardCommand extends Command { return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'soundboard', sound)); - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react('🔉'); - } + await reactIfAble(msg, this.client.user, '🔉'); return null; } }; diff --git a/commands/voice/vocodes.js b/commands/voice/vocodes.js index b0eeaf08..2203207c 100644 --- a/commands/voice/vocodes.js +++ b/commands/voice/vocodes.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { Readable } = require('stream'); -const { list } = require('../../util/Util'); +const { list, reactIfAble } = require('../../util/Util'); const voices = require('../../assets/json/vocodes'); const { LOADING_EMOJI_ID } = process.env; @@ -52,9 +52,7 @@ module.exports = class VocodesCommand extends Command { return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`); } try { - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react(LOADING_EMOJI_ID); - } + await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); const { body } = await request .post('https://mumble.stream/speak_spectrogram') .send({ @@ -62,11 +60,10 @@ module.exports = class VocodesCommand extends Command { text }); connection.play(Readable.from([Buffer.from(body.audio_base64, 'base64')])); - if (msg.channel.permissionsFor(this.client.user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { - await msg.react('🔉'); - } + await reactIfAble(msg, this.client.user, '🔉'); return null; } catch (err) { + await reactIfAble(msg, this.client.user, '⚠️'); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/package.json b/package.json index ea4ef34d..75ecc632 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.34.5", + "version": "119.34.6", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index 38fc22e6..aa16f434 100644 --- a/util/Util.js +++ b/util/Util.js @@ -162,6 +162,15 @@ module.exports = class Util { return today; } + static magikToBuffer(magik) { + return new Promise((res, rej) => { + magik.toBuffer((err, buffer) => { + if (err) return rej(err); + return res(buffer); + }); + }); + } + static embedURL(title, url, display) { return `[${title}](${url.replaceAll(')', '%29')}${display ? ` "${display}"` : ''})`; } @@ -172,6 +181,18 @@ module.exports = class Util { return str; } + static async reactIfAble(msg, user, emoji, fallbackEmoji) { + if (fallbackEmoji && !msg.channel.permissionsFor(user).has('USE_EXTERNAL_EMOJIS')) emoji = fallbackEmoji; + if (msg.channel.permissionsFor(user).has(['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'])) { + try { + await msg.react(emoji); + } catch { + return null; + } + } + return null; + } + static async verify(channel, user, { time = 30000, extraYes = [], extraNo = [] } = {}) { const filter = res => { const value = res.content.toLowerCase();