From de766b9654ca4ebaf8eb4711b097034dd338057a Mon Sep 17 00:00:00 2001 From: Wessel T Date: Fri, 22 Feb 2019 06:22:10 +0100 Subject: [PATCH] [refractor 1] Fix a part of the clusterfuck that's called "commands" --- TODO.md | 6 ++ src/assets/i18n/en_us/strings.yml | 1 + src/assets/i18n/nl/strings.yml | 1 + src/commands/Discord/Developer/echo.js | 39 +++++----- src/commands/Discord/Developer/eval.js | 98 +++++++++++++----------- src/commands/Discord/Developer/exec.js | 100 +++++++++++++------------ src/commands/Discord/Games/splatoon.js | 63 ++++++++-------- src/commands/Discord/Image/birb.js | 37 ++++++--- src/commands/Discord/Image/cat.js | 46 +++++++++--- src/commands/Discord/Image/dog.js | 48 ++++++++---- src/commands/Discord/Image/duck.js | 39 +++++++--- src/commands/Discord/Image/fox.js | 37 +++++++-- src/commands/Discord/Image/lizard.js | 37 ++++++--- src/commands/Discord/Image/penguin.js | 37 ++++++--- src/commands/Discord/Tags/tag-add.js | 71 ++++++++---------- src/commands/Discord/Utility/math.js | 71 +++++++++++------- src/commands/Discord/Utility/ping.js | 45 ++++++----- src/commands/Discord/Utility/snipe.js | 93 ++++++++++++++++------- 18 files changed, 550 insertions(+), 319 deletions(-) diff --git a/TODO.md b/TODO.md index 74cbf60..6a16c30 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,12 @@ - [ ] = Planned - [x] = In progress +- [ ] Global + - [ ] Clean code / refractor + - [ ] Clean locales +- [ ] Information + - [ ] Commands + - [ ] Stats (rewrite) - [x] Utility - [x] Commands - [x] Tesseract (Donator) diff --git a/src/assets/i18n/en_us/strings.yml b/src/assets/i18n/en_us/strings.yml index 0e69a70..41b6823 100644 --- a/src/assets/i18n/en_us/strings.yml +++ b/src/assets/i18n/en_us/strings.yml @@ -78,6 +78,7 @@ util: snipe: fail: '$[emoji#0] No snipeable messages found' + embed: 'Embed Body' footer: 'Sniped by $[author:tag] ($[author:id])' info: diff --git a/src/assets/i18n/nl/strings.yml b/src/assets/i18n/nl/strings.yml index 9d19760..aba3e80 100644 --- a/src/assets/i18n/nl/strings.yml +++ b/src/assets/i18n/nl/strings.yml @@ -78,6 +78,7 @@ util : snipe : fail : '$[emoji#0] Geen snipebare berichten gevonden' + embed : 'Embed Body' footer : 'Sniped door $[author:tag] ($[author:id])' info : diff --git a/src/commands/Discord/Developer/echo.js b/src/commands/Discord/Developer/echo.js index 246094d..3a5a30b 100644 --- a/src/commands/Discord/Developer/echo.js +++ b/src/commands/Discord/Developer/echo.js @@ -3,33 +3,34 @@ const { DiscordCommand } = require('../../../core'); module.exports = class Echo extends DiscordCommand { constructor(bot) { super(bot, { - name : 'echo', - syntax : 'echo <...message:str>', - aliases : [], - argument : [ '<...message:str>' ], - description : 'Repeat your input', + name : 'echo', + syntax : 'echo <...message:str>', + aliases : [], + argument : [ '<...message:str>' ], + description: 'Repeat your input', - hidden : false, - enabled : true, - cooldown : 0, - category : 'Developer', - ownerOnly : true, - guildOnly : false, - permissions : [ 'embedLinks' ] + hidden : false, + enabled : true, + cooldown : 0, + category : 'Developer', + ownerOnly : true, + guildOnly : false, + permissions: [ ] }); } async execute(msg, args) { - if (args.length <= 0) return msg.channel.createMessage(this.localize(msg.author.locale['developer']['echo'])); + if (args.length <= 0) return msg.channel.createMessage(this._localize(msg.author.locale.developer.echo)); msg.channel.createMessage(args.join(' ')); } - localize(msg) { - // Empty - if (!msg) { - return ''; + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'echo')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - // Main - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'echo')); } }; \ No newline at end of file diff --git a/src/commands/Discord/Developer/eval.js b/src/commands/Discord/Developer/eval.js index 5e2fb88..1aa15a1 100644 --- a/src/commands/Discord/Developer/eval.js +++ b/src/commands/Discord/Developer/eval.js @@ -23,63 +23,77 @@ module.exports = class Eval extends DiscordCommand { } async execute(msg, args, user, guild) { - let result; - let silent = args.join(' ').trim().endsWith('--silent') || args.join(' ').trim().endsWith('-s') ? args.pop() : false; - let asynchr = args.join(' ').trim().includes('return') || args.join(' ').trim().includes('await'); + let result = '{}'; let errored = false; + + const raw = args.join(' ').trim().endsWith('--raw') ? args.pop() : false; + const silent = args.join(' ').trim().endsWith('--silent') || args.join(' ').trim().endsWith('-s') ? args.pop() : false; + const asynchr = args.join(' ').trim().includes('return') || args.join(' ').trim().includes('await'); - if (args.length <= 0) return msg.channel.createMessage(this.localize(msg.author.locale['developer']['eval']['args'])); + if (args.length <= 0) return msg.channel.createMessage(this._localize(msg.author.locale.developer.eval.args)); - const message = await msg.channel.createMessage(this.localize(msg.author.locale['developer']['eval']['busy'])); - - try { result = (asynchr ? eval(`(async() => {${args.join(' ')}})();`) : eval(args.join(' '))); } - catch (ex) { result = ex; errored = true; } - finally { - if (silent) return message.edit(this.localize(msg.author.locale['developer']['eval']['silent'])); - result = this.bot.util.escapeMarkdown(this.sanitize(String(result)), true); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.developer.eval.busy)); + try { + result = asynchr ? eval(`(async() => {${args.join(' ')}})();`) : eval(args.join(' ')); + } catch (ex) { + result = ex; + errored = true; + } finally { + if (silent) return message.edit(this._localize(msg.author.locale.developer.eval.silent)); + result = this.bot.util.escapeMarkdown(this.bot.util.shorten(this._sanitize(String(result)), true, 1850)); + + if (raw) return msg.channel.createMessage(`\`\`\`js\n${result}\`\`\``); message.edit({ content: '', embed: { - color : (errored ? this.bot.col['developer']['eval']['failure'] : this.bot.col['developer']['eval']['success']), - description: this.localize(msg.author.locale['developer']['eval']['result'].join('\n'), { resultType: errored ? msg.author.locale['developer']['eval']['types'][1] : msg.author.locale['developer']['eval']['types'][0], resultMessage: this.bot.util.shorten(result, 1850) || '{}' }) + color: errored ? this.bot.col.developer.eval.failure : this.bot.col.developer.eval.success, + description: this._localize(msg.author.locale.developer.eval.result.join('\n'), { resultType: errored ? msg.author.locale.developer.eval.types[1] : msg.author.locale.developer.eval.types[0], resultMessage: result || '{}' }) } }); }; } - sanitize(msg) { - // Return nothing if empty - if (!msg) return msg; - // API tokens - for (let _ in this.bot.conf['api']) { - msg = msg.replace(new RegExp(this.bot.conf['api'][_], 'gi'), '<--snip-->'); + _sanitize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + for (let _ in this.bot.conf.api) { + msg = msg.replace(new RegExp(this.bot.conf.api[_], 'gi'), '<--snip-->'); + } + + for (let _ in this.bot.conf.discord.webhook) { + msg = msg.replace(new RegExp(this.bot.conf.discord.webhook[_], 'gi'), '<--snip-->'); + } + + return msg + .replace(new RegExp(this.bot.token, 'gi'), '<--snip-->') + .replace(new RegExp(this.bot.conf['discord']['token'], 'gi'), '<--snip-->'); + } catch (ex) { + return `SANITIZE_ERROR:${ex.code}`; } - // Webhook information - for (let _ in this.bot.conf.discord.webhook) { - msg = msg.replace(new RegExp(this.bot.conf.discord.webhook[_], 'gi'), '<--snip-->'); - } - // Bot tokens - return msg - .replace(new RegExp(this.bot.token, 'gi'), '<--snip-->') - .replace(new RegExp(this.bot.conf['discord']['token'], 'gi'), '<--snip-->'); } - localize(msg, extData) { - // Empty - if (!msg) return ''; - // extData - if (extData && extData.resultType) { - msg = msg.replace(/\$\[result:type]/g, extData.resultType); + _localize(msg, extData) { + try { + if (!msg) return 'INVALID_STRING'; + + if (extData) { + if (extData.resultType) { + msg = msg.replace(/\$\[result:type]/g, extData.resultType); + } + if (extData.resultMessage) { + msg = msg.replace(/\$\[result:message]/g, extData.resultMessage); + } + } + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'eval', '0')) + .replace(/\$\[emoji#1]/g, this.bot.emote('developer', 'eval', '1')) + .replace(/\$\[emoji#2]/g, this.bot.emote('developer', 'eval', '2')) + .replace(/\$\[emoji#3]/g, this.bot.emote('developer', 'eval', '3')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - if (extData && extData.resultMessage) { - msg = msg.replace(/\$\[result:message]/g, extData.resultMessage); - } - // Main - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'eval', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('developer', 'eval', '1')) - .replace(/\$\[emoji#2]/g, this.bot.emote('developer', 'eval', '2')) - .replace(/\$\[emoji#3]/g, this.bot.emote('developer', 'eval', '3')); } }; \ No newline at end of file diff --git a/src/commands/Discord/Developer/exec.js b/src/commands/Discord/Developer/exec.js index 5a4b346..feb343d 100644 --- a/src/commands/Discord/Developer/exec.js +++ b/src/commands/Discord/Developer/exec.js @@ -5,13 +5,11 @@ const { execSync } = require('child_process'); module.exports = class Exec extends DiscordCommand { constructor(bot) { super(bot, { - // Information name : 'exec', syntax : 'exec <...cmd:str>', aliases : [], - argument : [ ], + argument : [ '<...cmd:str>' ], description: 'Execute a command', - // Checks hidden : false, enabled : true, cooldown : 0, @@ -22,66 +20,76 @@ module.exports = class Exec extends DiscordCommand { }); } - async execute(msg, args, user, guild) { + async execute(msg, args) { let result = '$'; - let raw = args.join(' ').trim().endsWith('--raw') ? args.pop() : false; - let silent = args.join(' ').trim().endsWith('--silent') || args.join(' ').trim().endsWith('-s') ? args.pop() : false; - let errored = false; + const raw = args.join(' ').trim().endsWith('--raw') ? args.pop() : false; + const silent = args.join(' ').trim().endsWith('--silent') || args.join(' ').trim().endsWith('-s') ? args.pop() : false; + const errored = false; - if (args.length <= 0) return msg.channel.createMessage(this.localize(msg.author.locale['developer']['exec']['args'])); + if (args.length <= 0) return msg.channel.createMessage(this._localize(msg.author.locale.developer.exec.args)); - const message = await msg.channel.createMessage(this.localize(msg.author.locale['developer']['exec']['busy'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.developer.exec.busy)); - try { result = execSync(args.join(' '), { timeout: 100000 }); } - catch (ex) { result = ex; errored = true; } - finally { - if (silent) return message.edit(this.localize(msg.author.locale['developer']['exec']['silent'])); - result = this.bot.util.escapeMarkdown(this.sanitize(String(result))); + try { + result = execSync(args.join(' '), { timeout: 100000 }); + } catch (ex) { + result = ex; errored = true; + } finally { + if (silent) return message.edit(this._localize(msg.author.locale.developer.exec.silent)); + result = this.bot.util.escapeMarkdown(this.bot.util.shorten(this._sanitize(String(result)), 1850), true); if (raw) return msg.channel.createMessage(`\`\`\`sh\n${result}\`\`\``); message.edit({ content: '', embed: { - color : (errored ? this.bot.col['developer']['exec']['failure'] : this.bot.col['developer']['exec']['success']), - description: this.localize(msg.author.locale['developer']['exec']['result'].join('\n'), { resultType: errored ? msg.author.locale['developer']['exec']['types'][1] : msg.author.locale['developer']['exec']['types'][0], resultMessage: this.bot.util.shorten(result, 1850) || '$' }) + color: errored ? this.bot.col.developer.exec.failure : this.bot.col.developer.exec.success, + description: this._localize(msg.author.locale.developer.exec.result.join('\n'), { resultType: errored ? msg.author.locale.developer.exec.types[1] : msg.author.locale.developer.exec.types[0], resultMessage: result || '$' }) } }); }; } - sanitize(msg) { - // Return nothing if empty - if (!msg) return msg; - // API tokens - for (let _ in this.bot.conf['api']) { - msg = msg.replace(new RegExp(this.bot.conf['api'][_], 'gi'), '<--snip-->'); + _sanitize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + for (let _ in this.bot.conf.api) { + msg = msg.replace(new RegExp(this.bot.conf.api[_], 'gi'), '<--snip-->'); + } + + for (let _ in this.bot.conf.discord.webhook) { + msg = msg.replace(new RegExp(this.bot.conf.discord.webhook[_], 'gi'), '<--snip-->'); + } + + return msg + .replace(new RegExp(this.bot.token, 'gi'), '<--snip-->') + .replace(new RegExp(this.bot.conf['discord']['token'], 'gi'), '<--snip-->'); + } catch (ex) { + return `SANITIZE_ERROR:${ex.code}`; } - // Webhook information - for (let _ in this.bot.conf.discord.webhook) { - msg = msg.replace(new RegExp(this.bot.conf.discord.webhook[_], 'gi'), '<--snip-->'); - } - // Bot tokens - return msg - .replace(new RegExp(this.bot.token, 'gi'), '<--snip-->') - .replace(new RegExp(this.bot.conf['discord']['token'], 'gi'), '<--snip-->'); } - localize(msg, extData) { - // Empty - if (!msg) return ''; - // extData - if (extData && extData.resultType) { - msg = msg.replace(/\$\[result:type]/g, extData.resultType); + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + if (extData.resultType) { + msg = msg.replace(/\$\[result:type]/g, extData.resultType); + } + if (extData.resultMessage) { + msg = msg.replace(/\$\[result:message]/g, extData.resultMessage); + } + } + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'exec', '0')) + .replace(/\$\[emoji#1]/g, this.bot.emote('developer', 'exec', '1')) + .replace(/\$\[emoji#2]/g, this.bot.emote('developer', 'exec', '2')) + .replace(/\$\[emoji#3]/g, this.bot.emote('developer', 'exec', '3')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - if (extData && extData.resultMessage) { - msg = msg.replace(/\$\[result:message]/g, extData.resultMessage); - } - // Main - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('developer', 'exec', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('developer', 'exec', '1')) - .replace(/\$\[emoji#2]/g, this.bot.emote('developer', 'exec', '2')) - .replace(/\$\[emoji#3]/g, this.bot.emote('developer', 'exec', '3')); } -}; +}; \ No newline at end of file diff --git a/src/commands/Discord/Games/splatoon.js b/src/commands/Discord/Games/splatoon.js index f2e4290..7d44912 100644 --- a/src/commands/Discord/Games/splatoon.js +++ b/src/commands/Discord/Games/splatoon.js @@ -1,14 +1,6 @@ -/* - m = Maps - r = Request - w = Wumpfetch -*/ - const { DiscordCommand } = require('../../../core'); const w = require('wumpfetch'); -const l = require('larg'); -const s = require('../../../util/smartSearch'); module.exports = class Splatoon extends DiscordCommand { constructor(bot) { @@ -21,41 +13,50 @@ module.exports = class Splatoon extends DiscordCommand { hidden : false, enabled : true, - cooldown : 1000, + cooldown : 2000, category : 'Games', ownerOnly : false, guildOnly : false, - permissions: [ 'embedLinks' ] + permissions: [] }); - this.mutable.BASE_URL = 'https://splatoon.ink'; - this.mutable.REQ_DATA = { - headers: { - 'User-Agent': this.bot.ua + this.static = { + BASE_URL: 'https://splatoon.ink', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } } }; + + Object.freeze(this); + Object.freeze(this.static); } - async execute(msg, args) { - let r = await w(`${this.mutable.BASE_URL}/schedule2.json`, this.mutable.REQ_DATA).send(); - r = r.json(); + async execute(msg) { + const req = await w(`${this.mutable.BASE_URL}/schedule2.json`, this.static.REQ_DATA).send(); + const res = req.json(); - msg.channel.createMessage(this._localize(msg.author.locale.games.splatoon.join('\n'), { maps: r })); + msg.channel.createMessage(this._localize(msg.author.locale.games.splatoon.join('\n'), res)); } _localize(msg, extData) { - if (extData && extData.maps) { - const m = extData.maps; - return msg - .replace(/\$\[maps:turf@first]/g, m.modes.regular[0].maps[0]) - .replace(/\$\[maps:ranked@first]/g, m.modes.gachi[0].maps[0]) - .replace(/\$\[maps:league@first]/g, m.modes.league[0].maps[0]) - .replace(/\$\[maps:turf@second]/g, m.modes.regular[0].maps[1]) - .replace(/\$\[maps:ranked@second]/g, m.modes.gachi[0].maps[1]) - .replace(/\$\[maps:league@second]/g, m.modes.league[0].maps[1]); + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + return msg + .replace(/\$\[maps:turf@first]/g, extData.modes.regular[0].maps[0]) + .replace(/\$\[maps:turf@second]/g, extData.modes.regular[0].maps[1]) + .replace(/\$\[maps:ranked@first]/g, extData.modes.gachi[0].maps[0]) + .replace(/\$\[maps:league@first]/g, extData.modes.league[0].maps[0]) + .replace(/\$\[maps:ranked@second]/g, extData.modes.gachi[0].maps[1]) + .replace(/\$\[maps:league@second]/g, extData.modes.league[0].maps[1]); + } + + return msg; + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - - return msg; } -}; - +}; \ No newline at end of file diff --git a/src/commands/Discord/Image/birb.js b/src/commands/Discord/Image/birb.js index 36a3388..9db8fa2 100644 --- a/src/commands/Discord/Image/birb.js +++ b/src/commands/Discord/Image/birb.js @@ -19,27 +19,46 @@ module.exports = class Birb extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: 'https://some-random-api.ml', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); - let img = await w('https://some-random-api.ml/birbimg', { headers: { 'User-Agent': this.bot.ua } }).send(); + let img = await w(`${this.static.BASE_URL}/birbimg`, this.static.REQ_DATA).send(); img = img.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['birb'], - image : { url: img.link ? img.link : '' }, - description: `*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.link ? img.link : 'https://www.google.com/'})*` + color: this.bot.col.image.birb, + image: { + url: img.link || '' + }, + description: `*[\`${msg.author.locale.image.failed_cache}\`](${img && img.link ? img.link : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'birb')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'birb')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/cat.js b/src/commands/Discord/Image/cat.js index c558692..761c9ef 100644 --- a/src/commands/Discord/Image/cat.js +++ b/src/commands/Discord/Image/cat.js @@ -20,32 +20,54 @@ module.exports = class Cat extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + IMAGES: 37, + BASE_URL: [ + 'https://wessel.meek.moe/lucifer', + 'https://aws.random.cat/meow', + 'https://catfact.ninja' + ], + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; } async execute(msg, args) { - let img = l(args)['lucifer'] ? `https://wessel.meek.moe/lucifer/${Math.floor(Math.random() * 37)}.jpg` : 'https://aws.random.cat/meow' - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + let img = l(args)['lucifer'] ? `${this.static.BASE_URL[0]}/${Math.floor(Math.random() * this.static.IMAGES)}.jpg` : this.static.BASE_URL[1]; + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); - if (img === 'https://aws.random.cat/meow') { - img = await w(img, { headers: { 'User-Agent': this.bot.ua } }).send(); + if (img === this.static.BASE_URL[1]) { + img = await w(img, this.static.REQ_DATA).send(); img = img.json(); } - let res = await w('https://catfact.ninja/fact?max_length=1500', { headers: { 'User-Agent': this.bot.ua } }).send(); + + let res = await w(`${this.static.BASE_URL[2]}/fact?max_length=1500`, this.static.REQ_DATA).send(); res = res.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['cat'], - image : { url: img.file ? img.file : img }, - description: `${this.bot.emote('image', 'cat', '1')} *Random Fact* **>** ${res.fact}\n\n*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.file ? img.file : img})*` + color: this.bot.col.image.cat, + image: { + url: img.file || img + }, + description: `${this.bot.emote('image', 'cat', '1')} *Random Fact* **>** ${res.fact}\n\n*[\`${msg.author.locale.image.failed_cache}\`](${img && img.file ? img.file : img})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'cat', '0')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'cat', '0')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/dog.js b/src/commands/Discord/Image/dog.js index f7f0880..5cfad49 100644 --- a/src/commands/Discord/Image/dog.js +++ b/src/commands/Discord/Image/dog.js @@ -13,35 +13,57 @@ module.exports = class Dog extends DiscordCommand { hidden : false, enabled : true, - cooldown : 5000, + cooldown : 2500, category : 'Image', ownerOnly : false, guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: [ + 'https://dog.ceo', + 'https://some-random-api.ml' + ], + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); - - let img = await w('https://dog.ceo/api/breeds/image/random', { headers: { 'User-Agent': this.bot.ua } }).send(); - let res = await w('https://some-random-api.ml/dogfact', { headers: { 'User-Agent': this.bot.ua } }).send(); - img = img.json(); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); + + let res = await w(`${this.static.BASE_URL[1]}/dogfact`, this.static.REQ_DATA).send(); + let img = await w(`${this.static.BASE_URL[0]}/api/breeds/image/random`, this.static.REQ_DATA).send(); res = res.json(); + img = img.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['dog'], - image : { url: img.message ? img.message : '' }, - description: `${this.bot.emote('image', 'dog', '1')} *Random Fact* **>** ${res.fact.slice(0, 1990)}\n\n*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.message ? img.message : 'https://www.google.com/'})*` + color: this.bot.col.image.dog, + image: { + url: img.message || '' + }, + description: `${this.bot.emote('image', 'dog', '1')} *Random Fact* **>** ${res.fact.slice(0, 1950)}\n\n*[\`${msg.author.locale.image.failed_cache}\`](${img && img.message ? img.message : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'dog', '0')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'dog', '0')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/duck.js b/src/commands/Discord/Image/duck.js index 9d0f79f..69bdc44 100644 --- a/src/commands/Discord/Image/duck.js +++ b/src/commands/Discord/Image/duck.js @@ -13,33 +13,52 @@ module.exports = class Duck extends DiscordCommand { hidden : false, enabled : true, - cooldown : 5000, + cooldown : 2500, category : 'Image', ownerOnly : false, guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: 'https://random-d.uk', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale['image']['fetching'])); - let img = await w('https://random-d.uk/api/v1/random', { headers: { 'User-Agent': this.bot.ua } }).send(); + let img = await w(`${this.static.BASE_URL}/api/v1/random`, this.static.REQ_DATA).send(); img = img.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['duck'], - image : { url: img.url ? img.url : '' }, - description: `*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.url ? img.url : 'https://www.google.com/'})*` + color: this.bot.col.image.duck, + image: { + url: img.url || '' + }, + description: `*[\`${msg.author.locale.image.failed_cache}\`](${img && img.url ? img.url : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'duck')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'duck')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/fox.js b/src/commands/Discord/Image/fox.js index 4a74849..787f308 100644 --- a/src/commands/Discord/Image/fox.js +++ b/src/commands/Discord/Image/fox.js @@ -19,26 +19,47 @@ module.exports = class Fox extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: 'https://randomfox.ca', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); - let img = await w('https://randomfox.ca/floof/', { headers: { 'User-Agent': this.bot.ua } }).send(); + let img = await w(`${this.static.BASE_URL}/floof`, this.static.REQ_DATA).send(); img = img.json(); + msg.channel.createMessage({ embed: { - color : this.bot.col['image']['fox'], - image : { url: img.image ? img.image : '' }, - description: `*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.image ? img.image : 'https://www.google.com/'})*` + color: this.bot.col.image.fox, + image: { + url: img.image || '' + }, + description: `*[\`${msg.author.locale.image.failed_cache}\`](${img && img.image ? img.image : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'fox')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'fox')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/lizard.js b/src/commands/Discord/Image/lizard.js index 08ba5e2..cc4ea42 100644 --- a/src/commands/Discord/Image/lizard.js +++ b/src/commands/Discord/Image/lizard.js @@ -19,27 +19,46 @@ module.exports = class Lizard extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: 'https://nekos.life', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); - let img = await w('https://nekos.life/api/v2/img/lizard', { headers: { 'User-Agent': this.bot.ua } }).send(); + let img = await w(`${this.static.BASE_URL}/api/v2/img/lizard`, this.static.REQ_DATA).send(); img = img.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['lizard'], - image : { url: img.url ? img.url : '' }, - description: `*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.url ? img.url : 'https://www.google.com/'})*` + color: this.bot.col.image.Lizard, + image: { + url: img.url || '' + }, + description: `*[\`${msg.author.locale.image.failed_cache}\`](${img && img.url ? img.url : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'lizard')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'lizard')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Image/penguin.js b/src/commands/Discord/Image/penguin.js index cc3fd5e..a2f7751 100644 --- a/src/commands/Discord/Image/penguin.js +++ b/src/commands/Discord/Image/penguin.js @@ -19,27 +19,46 @@ module.exports = class Penguin extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + this.static = { + BASE_URL: 'https://animals.anidiots.guide', + REQ_DATA: { + headers: { + 'User-Agent': this.bot.ua + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const message = await msg.channel.createMessage(this.localize(msg.author.locale['image']['fetching'])); + const message = await msg.channel.createMessage(this._localize(msg.author.locale.image.fetching)); - let img = await w('https://animals.anidiots.guide/penguin', { headers: { 'User-Agent': this.bot.ua } }).send(); + let img = await w(`${this.static.BASE_URL}/penguin`, this.static.REQ_DATA).send(); img = img.json(); msg.channel.createMessage({ embed: { - color : this.bot.col['image']['penguin'], - image : { url: img.link ? img.link : '' }, - description: `*[\`${msg.author.locale['image']['failed_cache']}\`](${img && img.link ? img.link : 'https://www.google.com/'})*` + color: this.bot.col.image.Penguin, + image: { + url: img.link || '' + }, + description: `*[\`${msg.author.locale.image.failed_cache}\`](${img && img.link ? img.link : 'https://www.google.com/'})*` } }); + message.delete(); } - localize(msg) { - if (!msg) return ''; - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('image', 'penguin')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'penguin')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Tags/tag-add.js b/src/commands/Discord/Tags/tag-add.js index 5a29bb3..be58a8b 100644 --- a/src/commands/Discord/Tags/tag-add.js +++ b/src/commands/Discord/Tags/tag-add.js @@ -46,49 +46,44 @@ module.exports = class TagAdd extends DiscordCommand { return v.length >= 1; }); - try { - let mess; + let mess; - if (!components[0]) { - mess = await msg.channel.createMessage(this._localize(msg.author.locale.tags.add.name.join('\n'))); - const name = await this.bot.collector.awaitMessage(msg.channel.id, msg.author.id, 30e3); + if (!components[0]) { + mess = await msg.channel.createMessage(this._localize(msg.author.locale.tags.add.name.join('\n'))); + const name = await this.bot.collector.awaitMessage(msg.channel.id, msg.author.id, 30e3); - if (!name || name.content.toLowerCase() === '--cancel') { - return mess.edit(this._localize(msg.author.locale.cancelled)); - } - - tag.name = name.content.slice(0, 50).toLowerCase().trim(); - } else tag.name = components[0].slice(0, 50).toLowerCase().trim(); - - if (!components[1]) { - mess = mess ? await mess.edit(this._localize(msg.author.locale.tags.add.content.join('\n'))) : await msg.channel.createMessage(this._localize(msg.author.locale.tags.add.content.join('\n'))); - const content = await this.bot.collector.awaitMessage(msg.channel.id, msg.author.id, 30e3); - - if (!content || content.content.toLowerCase() === '--cancel') { - return mess.edit(this._localize(msg.author.locale.cancelled)); - } - - tag.content = content.content.slice(0, 1950).trim(); - } else { - components.shift(); - tag.content = components.join('|').slice(0, 1950).trim(); + if (!name || name.content.toLowerCase() === '--cancel') { + return mess.edit(this._localize(msg.author.locale.cancelled)); } - if (tag.content && tag.name && mess) { - mess.delete().catch(() => { return; }); - } - } catch(ex) { - throw ex; - } finally { - let entry = await this.bot.m.connection.collection('dTags').findOne({ name: tag.name, 'author.guild': msg.channel.guild.id }); - if (entry !== null || this.bot.cmds.filter((v) => v.extData.name === tag.name || v.extData.aliases.includes(tag.name)).length > 0) { - return msg.channel.createMessage(this._localize(msg.author.locale.tags.add.invalid)); + tag.name = name.content.slice(0, 50).replace(/\n/g, '').toLowerCase().trim(); + } else tag.name = components[0].slice(0, 50).replace(/\n/g, '').toLowerCase().trim(); + + if (!components[1]) { + mess = mess ? await mess.edit(this._localize(msg.author.locale.tags.add.content.join('\n'))) : await msg.channel.createMessage(this._localize(msg.author.locale.tags.add.content.join('\n'))); + const content = await this.bot.collector.awaitMessage(msg.channel.id, msg.author.id, 30e3); + + if (!content || content.content.toLowerCase() === '--cancel') { + return mess.edit(this._localize(msg.author.locale.cancelled)); } - entry = new this.bot.schema.tag(tag); - await entry.save(); - msg.channel.createMessage(this._localize(msg.author.locale.tags.add.success, { name: this.bot.util.escapeMarkdown(tag.name) })); + tag.content = content.content.slice(0, 1950).trim(); + } else { + components.shift(); + tag.content = components.join('|').slice(0, 1950).trim(); } + + if (tag.content && tag.name && mess) { + mess.delete().catch(() => { return; }); + } + let entry = await this.bot.m.connection.collection('dTags').findOne({ name: tag.name, 'author.guild': msg.channel.guild.id }); + if (entry !== null || this.bot.cmds.filter((v) => v.extData.name === tag.name || v.extData.aliases.includes(tag.name)).length > 0) { + return msg.channel.createMessage(this._localize(msg.author.locale.tags.add.invalid)); + } + + entry = new this.bot.schema.tag(tag); + await entry.save(); + msg.channel.createMessage(this._localize(msg.author.locale.tags.add.success, { name: this.bot.util.escapeMarkdown(tag.name) })); } _localize(msg, extData) { @@ -102,7 +97,7 @@ module.exports = class TagAdd extends DiscordCommand { .replace(/\$\[emoji#2]/g, this.bot.emote('tags', 'add', '2')) .replace(/\$\[emoji#3]/g, this.bot.emote('tags', 'add', '3')) .replace(/\$\[emoji#4]/g, this.bot.emote('tags', 'add', '4')) - .replace(/\$\[emoji#4]/g, this.bot.emote('tags', 'add', '5')) - .replace(/\$\[emoji#5]/g, this.bot.emote('tags', 'add', '6')); + .replace(/\$\[emoji#5]/g, this.bot.emote('tags', 'add', '5')) + .replace(/\$\[emoji#6]/g, this.bot.emote('tags', 'add', '6')); } }; \ No newline at end of file diff --git a/src/commands/Discord/Utility/math.js b/src/commands/Discord/Utility/math.js index 007ebc7..6592d17 100644 --- a/src/commands/Discord/Utility/math.js +++ b/src/commands/Discord/Utility/math.js @@ -6,47 +6,64 @@ const { eval: mEval, config } = require('mathjs'); module.exports = class Math extends DiscordCommand { constructor(bot) { super(bot, { - name : 'math', - syntax : 'math <...equation:str>', - aliases : [], - argument : [ '<...equation:str>' ], - description : 'Evaluate an equation', + name : 'math', + syntax : 'math <...equation:str>', + aliases : [], + argument : [ '<...equation:str>' ], + description: 'Evaluate an equation', - hidden : false, - enabled : true, - cooldown : 5000, - category : 'Utility', - ownerOnly : false, - guildOnly : false, - permissions : [] + hidden : false, + enabled : true, + cooldown : 5000, + category : 'Utility', + ownerOnly : false, + guildOnly : false, + permissions: [] }); - this.regex = /eval|import|parse|simplify|derivative|createUnit/gi; + this.static = { + regex: /eval|import|parse|simplify|derivative|createUnit/gi + }; + + Object.freeze(this); + Object.freeze(this.static); config({ number: 'BigNumber', precision: 64 }); } async execute(msg, args) { - let sandbox = null; - let message = null; - let blocked = undefined; - let origMsg = await msg.channel.createMessage(this.localize(msg.author.locale['util']['math']['busy'])); - let equation = 0; + let mess = await msg.channel.createMessage(this._localize(msg.author.locale.util.math.busy)); + + let sandbox = null; + let message = null; + let blocked = undefined; + let equation = 0; try { - equation = (args[0] ? args.join(' ') : '2+2-1'); - blocked = this.regex.exec(equation); - if (blocked && blocked[0] && !this.bot.op(msg.author.id)) throw msg.author.locale['util']['math']['blocked'].replace(/\$\[math:function]/g, blocked[0]); + equation = args[0] ? args.join(' ') : '2+2-1'; + blocked = this.static.regex.exec(equation); + if (blocked && blocked[0] && !this.bot.op(msg.author.id)) throw this._localize(msg.author.locale.util.math.blocked, blocked[0]); const vm = new NodeVM({ timeout: 10000, sandbox: { mEval: mEval }, require: { external: true } }); sandbox = vm.run('module.exports = function(equation) { return mEval(`${equation}`) }'); - message = `${this.bot.util.shorten(`${this.bot.emote('util', 'math', '1' )} ${msg.author.mention} **>** \`${this.bot.util.escapeMarkdown(equation.replace(/ /gi, ''), false, true)}\` = \`${sandbox(equation) || 0}\``, 2000)}`; + message = this.bot.util.shorten(`${this.bot.emote('util', 'math', '1' )} ${msg.author.mention} **>** \`${this.bot.util.escapeMarkdown(equation, false, true)}\` = \`${sandbox(equation) || 0}\``, 2000); } catch (ex) { - message = `${this.bot.util.shorten(`${this.bot.emote( 'util', 'math', '2' )} ${msg.author.mention} **>** \`${this.bot.util.escapeMarkdown(equation.replace(/ /gi, ''), false, true)}\` = \`${this.bot.util.escapeMarkdown(ex.message ? ex.message : ex, false, true)}\``, 2000)}`; - } finally { origMsg.edit(message); } + message = this.bot.util.shorten(`${this.bot.emote( 'util', 'math', '2' )} ${msg.author.mention} **>** \`${this.bot.util.escapeMarkdown(equation, false, true)}\` = \`${this.bot.util.escapeMarkdown(ex.message ? ex.message : ex, false, true)}\``, 2000); + } finally { + mess.edit(message); + } } - localize(msg) { - if (!msg) return ''; - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('util', 'math', '0')); + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + msg = msg.replace(/\$\[math:function]/g, extData); + } + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('util', 'math', '0')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Utility/ping.js b/src/commands/Discord/Utility/ping.js index 260cbb8..682f402 100644 --- a/src/commands/Discord/Utility/ping.js +++ b/src/commands/Discord/Utility/ping.js @@ -17,44 +17,53 @@ module.exports = class Ping extends DiscordCommand { guildOnly : false, permissions: [ 'embedLinks' ] }); + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg) { - const now = Date.now(); + const now = Date.now(); + let health = msg.author.locale.util.ping.health[0]; const latency = msg.channel.guild ? msg.channel.guild.shard.latency : undefined; - let health; - if (isNaN(latency)) health = msg.author.locale['util']['ping']['health'][0]; - else if (latency < 200) health = `${msg.author.locale['util']['ping']['health'][1]} (**${this.bot.util.getPercentage(200, latency).toFixed()}%** \`[${'='.repeat(10 - Math.round(10* latency / 200))}${'-'.repeat(Math.round(10* latency / 200))}]\`)`; - else if (latency < 400) health = `${msg.author.locale['util']['ping']['health'][2]} (**${this.bot.util.getPercentage(400, latency).toFixed()}%** \`[${'='.repeat(10 - Math.round(10* latency / 400))}${'-'.repeat(Math.round(10* latency / 400))}]\`)`; - else if (latency > 400) health = `${msg.author.locale['util']['ping']['health'][3]} Bad (**0%** \`[----------]\`)`; + if (isNaN(latency)) health = msg.author.locale.util.ping.health[0]; + else if (latency < 200) health = `${msg.author.locale.util.ping.health[1]} (**${this.bot.util.getPercentage(200, latency).toFixed()}%** \`[${'='.repeat(10 - Math.round(10* latency / 200))}${'-'.repeat(Math.round(10* latency / 200))}]\`)`; + else if (latency < 400) health = `${msg.author.locale.util.ping.health[2]} (**${this.bot.util.getPercentage(400, latency).toFixed()}%** \`[${'='.repeat(10 - Math.round(10* latency / 400))}${'-'.repeat(Math.round(10* latency / 400))}]\`)`; + else if (latency > 400) health = `${msg.author.locale.util.ping.health[3]} (**0%** \`[----------]\`)`; const message = await msg.channel.createMessage({ embed: { - color : this.bot.col['util']['ping']['0'], - description: this.localize(msg.author.locale['util']['ping']['busy']) + color: this.bot.col.util.ping[0], + description: this._localize(msg.author.locale.util.ping.busy) } }); message.edit({ embed: { - color : latency < 200 ? this.bot.col['util']['ping']['1'] : latency > 200 && latency < 400 ? this.bot.col['util']['ping']['2'] : this.bot.col['util']['ping']['3'], - description: this.localize(msg.author.locale['util']['ping']['result'].join('\n'), { latency: latency, now: now, health: health }) + color: latency < 200 ? this.bot.col.util.ping[1] : latency > 200 && latency < 400 ? this.bot.col.util.ping[2] : this.bot.col.util.ping[3], + description: this._localize(msg.author.locale.util.ping.result.join('\n'), { latency: latency, now: now, health: health }) } }); } - localize(msg, extData = {}) { - if (!msg) return ''; - - if (extData && extData.now) msg = msg.replace(/\$\[roundtrip]/g, Date.now() - extData.now); - if (extData && extData.health) msg = msg.replace(/\$\[health]/g, extData.health); - if (extData && extData.latency) msg = msg.replace(/\$\[latency]/g, extData.latency); - - return msg + _localize(msg, extData = {}) { + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + if (extData.now) msg = msg.replace(/\$\[roundtrip]/g, Date.now() - extData.now); + if (extData.health) msg = msg.replace(/\$\[health]/g, extData.health); + if (extData.latency) msg = msg.replace(/\$\[latency]/g, extData.latency); + } + + return msg .replace(/\$\[emoji#0]/g, this.bot.emote('util', 'ping', '0')) .replace(/\$\[emoji#1]/g, this.bot.emote('util', 'ping', '1')) .replace(/\$\[emoji#2]/g, this.bot.emote('util', 'ping', '2')) .replace(/\$\[emoji#3]/g, this.bot.emote('util', 'ping', '3')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Utility/snipe.js b/src/commands/Discord/Utility/snipe.js index 5e89046..2cf24fc 100644 --- a/src/commands/Discord/Utility/snipe.js +++ b/src/commands/Discord/Utility/snipe.js @@ -14,43 +14,75 @@ module.exports = class Snipe extends DiscordCommand { cooldown : 1000, category : 'Utility', ownerOnly : false, - guildOnly : false, + guildOnly : true, permissions: [ 'embedLinks' ] }); + + this.static = { + EMBED_FIELD_LIMIT: 1024 + }; + + Object.freeze(this); + Object.freeze(this.static); } execute(msg) { const message = this.bot.cache.get(`${msg.channel.id}:SNIPE`); - if (!message) return msg.channel.createMessage(this.localize(msg.author.locale['util']['snipe']['fail'])); + if (!message) return msg.channel.createMessage(this._localize(msg.author.locale.util.snipe.fail)); let structure = { - author : { - name : `${message.author.username}#${message.author.discriminator} (${message.author.id})`, + author: { + name: `${message.author.username}#${message.author.discriminator} (${message.author.id})`, icon_url: message.author.avatar ? message.author.avatarURL : message.author.defaultAvatarURL }, - color : this.bot.col['util']['snipe'], - footer : { - text : this.localize(msg.author.locale['util']['snipe']['footer'], { msg: msg }), + color: this.bot.col['util']['snipe'], + fields: [], + footer: { + text: this._localize(msg.author.locale.util.snipe.footer, msg), icon_url: msg.author.avatar ? msg.author.avatarURL : msg.author.defaultAvatarURL }, timestamp: new Date(message.timestamp).toISOString() }; - if (message.embeds.length > 0 && 'url' in message.embeds[0]) structure.url = message.embeds[0].url; - if (message.embeds.length > 0 && 'type' in message.embeds[0]) structure.type = message.embeds[0].type; - if (message.embeds.length > 0 && 'title' in message.embeds[0]) structure.title = message.embeds[0].title; - if (message.embeds.length > 0 && 'image' in message.embeds[0]) structure.image = message.embeds[0].image; - if (message.embeds.length > 0 && 'video' in message.embeds[0]) structure.video = message.embeds[0].video; - if (message.embeds.length > 0 && 'fields' in message.embeds[0]) structure.fields = message.embeds[0].fields; - if (message.embeds.length > 0 && 'provider' in message.embeds[0]) structure.provider = message.embeds[0].provider; - if (message.embeds.length > 0 && 'thumbnail' in message.embeds[0]) structure.thumbnail = message.embeds[0].thumbnail; - if (message.embeds.length > 0 && 'description' in message.embeds[0]) structure.description = (`${message.content ? message.content : ''}\n\n${message.embeds[0].description}`).slice(0, 2048); - else if (message.content !== '' ) structure.description = message.content; + if (message.embeds.length > 0) { + if ('url' in message.embeds[0]) structure.url = message.embeds[0].url; + if ('type' in message.embeds[0]) structure.type = message.embeds[0].type; + if ('title' in message.embeds[0]) structure.title = message.embeds[0].title; + if ('image' in message.embeds[0]) structure.image = message.embeds[0].image; + if ('video' in message.embeds[0]) structure.video = message.embeds[0].video; + if ('fields' in message.embeds[0]) structure.fields = message.embeds[0].fields; + if ('provider' in message.embeds[0]) structure.provider = message.embeds[0].provider; + if ('thumbnail' in message.embeds[0]) structure.thumbnail = message.embeds[0].thumbnail; + if ('description' in message.embeds[0]) { + if (!message.content) structure.description = message.embeds[0].description; + else { + if (message.embeds[0].description.length <= this.mutable.EMBED_FIELD_LIMIT) { + structure.description = message.content; + structure.fields.push({ + name: 'Embed body', + value: message.embeds[0].description + }); + } else {{ + structure.description = message.content; + structure.fields.push({ + name: msg.author.locale.util.snipe.embed, + value: message.embeds[0].description.substring(0, this.mutable.EMBED_FIELD_LIMIT) + }, { + name: '.', + value: message.embeds[0].description.substring(this.mutable.EMBED_FIELD_LIMIT, this.mutable.EMBED_FIELD_LIMIT * 2) + }); + }} + } + } + } else if (message.content !== '' ) { + structure.description = message.content; + } + if (!('image' in structure) && message.attachments.length > 0) { structure.image = { - url : message.attachments[0].url, - width : message.attachments[0].width, - height : message.attachments[0].height, + url: message.attachments[0].url, + width: message.attachments[0].width, + height: message.attachments[0].height, proxy_url: message.attachments[0].proxy_url }; } @@ -59,14 +91,19 @@ module.exports = class Snipe extends DiscordCommand { this.bot.cache.delete(`${msg.channel.id}:SNIPE`); } - localize(msg, extData) { - if (!msg) return ''; + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; - if (extData && extData.msg) msg = msg - .replace(/\$\[author:tag]/g, `${extData.msg.author.username}#${extData.msg.author.discriminator}`) - .replace(/\$\[author:id]/g, extData.msg.author.id); - - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('util', 'snipe')); + if (extData) { + msg = msg + .replace(/\$\[author:id]/g, extData.author.id) + .replace(/\$\[author:tag]/g, `${extData.author.username}#${extData.author.discriminator}`); + } + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('util', 'snipe')); + } catch(ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file