From e3a4d59b91a79505afcf960e2ae14f210136320c Mon Sep 17 00:00:00 2001 From: Wessel T Date: Sat, 23 Feb 2019 14:06:23 +0100 Subject: [PATCH] [Refractor 1.2] Some more things fixed up --- src/assets/i18n/en_us/strings.yml | 4 +- src/assets/i18n/nl/strings.yml | 4 +- src/commands/Discord/Developer/exec.js | 22 +- src/commands/Discord/Image/birb.js | 2 +- src/commands/Discord/Image/cat.js | 2 +- src/commands/Discord/Image/dog.js | 2 +- src/commands/Discord/Image/duck.js | 2 +- src/commands/Discord/Image/fox.js | 2 +- src/commands/Discord/Image/lizard.js | 2 +- src/commands/Discord/Image/penguin.js | 2 +- src/commands/Discord/Information/avatar.js | 132 ++++++----- src/commands/Discord/Information/commands.js | 110 ++++++---- src/commands/Discord/Information/commits.js | 80 +++++-- src/commands/Discord/Information/guild.js | 220 +++++++++++-------- src/commands/Discord/Information/stats.js | 203 +++++++++-------- src/commands/Discord/Information/user.js | 157 +++++++------ src/commands/Discord/Tags/tag-info.js | 2 +- src/commands/Discord/Utility/math.js | 2 +- src/commands/Discord/Utility/snipe.js | 2 +- src/core/registry/Discord/Commands.js | 2 +- 20 files changed, 555 insertions(+), 399 deletions(-) diff --git a/src/assets/i18n/en_us/strings.yml b/src/assets/i18n/en_us/strings.yml index 41b6823..de25622 100644 --- a/src/assets/i18n/en_us/strings.yml +++ b/src/assets/i18n/en_us/strings.yml @@ -87,9 +87,7 @@ info: avatar: '$[emoji#0] Here''s **$[user:full]** avatar' - commits: - - '$[emoji#1] **Total commits**: [`$[commits:total]`](https://github.com/PassTheWessel/wump/commits "Click here for a list of all commits")' - - '$[emoji#0] **10 most recent commits done on [`PassTheWessel/wump`](https://github.com/PassTheWessel/wump/)**: $[commits:list]' + commits: '$[emoji#0] **10 most recent commits done on [`PassTheWessel/wump`](https://github.com/PassTheWessel/wump/)**: $[commits:list]' commands: single: diff --git a/src/assets/i18n/nl/strings.yml b/src/assets/i18n/nl/strings.yml index aba3e80..d2f0cc2 100644 --- a/src/assets/i18n/nl/strings.yml +++ b/src/assets/i18n/nl/strings.yml @@ -87,9 +87,7 @@ info : avatar : '$[emoji#0] Hier is **$[user:full]** avatar' - commits : - - '$[emoji#1] **Totaal aantal commits**: [`$[commits:total]`](https://github.com/PassTheWessel/wump/commits "Click here for a list of all commits")' - - '$[emoji#0] **10 meest recente commits gedaan op [`PassTheWessel/wump`](https://github.com/PassTheWessel/wump/)**: $[commits:list]' + commits : '$[emoji#0] **10 meest recente commits gedaan op [`PassTheWessel/wump`](https://github.com/PassTheWessel/wump/)**: $[commits:list]' commands: multi : diff --git a/src/commands/Discord/Developer/exec.js b/src/commands/Discord/Developer/exec.js index feb343d..9d8048d 100644 --- a/src/commands/Discord/Developer/exec.js +++ b/src/commands/Discord/Developer/exec.js @@ -34,19 +34,23 @@ module.exports = class Exec extends DiscordCommand { try { result = execSync(args.join(' '), { timeout: 100000 }); } catch (ex) { - result = ex; errored = true; + 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: result || '$' }) - } - }); + if (raw) { + message.edit(`\`\`\`sh\n${result}\`\`\``); + } else { + 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: result || '$' }) + } + }); + } }; } diff --git a/src/commands/Discord/Image/birb.js b/src/commands/Discord/Image/birb.js index 9db8fa2..8845f70 100644 --- a/src/commands/Discord/Image/birb.js +++ b/src/commands/Discord/Image/birb.js @@ -57,7 +57,7 @@ module.exports = class Birb extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'birb')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/cat.js b/src/commands/Discord/Image/cat.js index 761c9ef..30e82d7 100644 --- a/src/commands/Discord/Image/cat.js +++ b/src/commands/Discord/Image/cat.js @@ -66,7 +66,7 @@ module.exports = class Cat extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'cat', '0')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/dog.js b/src/commands/Discord/Image/dog.js index 5cfad49..840a557 100644 --- a/src/commands/Discord/Image/dog.js +++ b/src/commands/Discord/Image/dog.js @@ -62,7 +62,7 @@ module.exports = class Dog extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'dog', '0')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/duck.js b/src/commands/Discord/Image/duck.js index 69bdc44..26cc134 100644 --- a/src/commands/Discord/Image/duck.js +++ b/src/commands/Discord/Image/duck.js @@ -57,7 +57,7 @@ module.exports = class Duck extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'duck')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/fox.js b/src/commands/Discord/Image/fox.js index 787f308..41206ea 100644 --- a/src/commands/Discord/Image/fox.js +++ b/src/commands/Discord/Image/fox.js @@ -58,7 +58,7 @@ module.exports = class Fox extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'fox')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/lizard.js b/src/commands/Discord/Image/lizard.js index cc4ea42..eb88d50 100644 --- a/src/commands/Discord/Image/lizard.js +++ b/src/commands/Discord/Image/lizard.js @@ -57,7 +57,7 @@ module.exports = class Lizard extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'lizard')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Image/penguin.js b/src/commands/Discord/Image/penguin.js index a2f7751..ff30af7 100644 --- a/src/commands/Discord/Image/penguin.js +++ b/src/commands/Discord/Image/penguin.js @@ -57,7 +57,7 @@ module.exports = class Penguin extends DiscordCommand { if (!msg) throw 'INVALID_STRING'; return msg.replace(/\$\[emoji#0]/g, this.bot.emote('image', 'penguin')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Information/avatar.js b/src/commands/Discord/Information/avatar.js index 7842cad..704637e 100644 --- a/src/commands/Discord/Information/avatar.js +++ b/src/commands/Discord/Information/avatar.js @@ -5,9 +5,12 @@ const sizeOf = require('../../../util/sizeOf'); const { extname } = require('path'); const { readFileSync } = require('fs'); // Sharp is an optional dependency -let s; -try { s = require('sharp'); } -catch (ex) { throw new Error('Sharp not found, not loading this command'); } +let sharp; +try { + sharp = require('sharp'); +} catch (ex) { + throw new Error('Sharp not found, not loading this command'); +} module.exports = class Avatar extends DiscordCommand { constructor(bot) { @@ -27,9 +30,9 @@ module.exports = class Avatar extends DiscordCommand { permissions: [ 'embedLinks' ] }); - this.mutable = { + this.static = { reg: [ /--large/i, /--(nearest|linear)/i, /--greyscale/i, /--jpeg/i, /--blur/i ], - req: { + REQ_DATA: { headers: { 'User-Agent' : this.bot.ua, 'Authorization': this.bot.conf.api.dbl @@ -37,78 +40,86 @@ module.exports = class Avatar extends DiscordCommand { } }; - try { this.mutable.thumb = readFileSync('./assets/img/thumb/votelocked.png'); } - catch(ex) { this.thumb = undefined; } + try { + this.static.thumb = readFileSync('./assets/img/thumb/votelocked.png'); + } catch (ex) { + this.static.thumb = undefined; + } + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg, args) { - // Resizing method, original args, extras and edited const oArgs = args; - const edited = this.mutable.reg[0].test(oArgs) || this.mutable.reg[1].test(oArgs) || this.mutable.reg[2].test(oArgs) || this.mutable.reg[3].test(oArgs) || this.mutable.reg[4].test(oArgs); - const extras = `${this.mutable.reg[2].exec(oArgs) ? 'GREYSCALE' : ''} ${this.mutable.reg[3].exec(oArgs) ? 'JPEG' : ''} ${this.mutable.reg[4].exec(oArgs) ? 'BLUR' : ''}`; + const edited = this.static.reg[0].test(oArgs) || this.static.reg[1].test(oArgs) || this.static.reg[2].test(oArgs) || this.static.reg[3].test(oArgs) || this.static.reg[4].test(oArgs); + const extras = `${this.static.reg[2].exec(oArgs) ? 'GREYSCALE' : ''} ${this.static.reg[3].exec(oArgs) ? 'JPEG' : ''} ${this.static.reg[4].exec(oArgs) ? 'BLUR' : ''}`; const resize = - this.mutable.reg[1].test(args) && this.mutable.reg[1].exec(args)[0].toLowerCase() === '--nearest' ? 'NEAREST' : - this.mutable.reg[1].test(args) && this.mutable.reg[1].exec(args)[0].toLowerCase() === '--linear' ? 'LINEAR_A' : 'LINEAR_B'; - // Strip args + this.static.reg[1].test(args) && this.static.reg[1].exec(args)[0].toLowerCase() === '--nearest' ? 'NEAREST' : + this.static.reg[1].test(args) && this.static.reg[1].exec(args)[0].toLowerCase() === '--linear' ? 'LINEAR_A' : 'LINEAR_B'; + args = args .join(' ') .trim() - .replace(this.mutable.reg[0], '') - .replace(this.mutable.reg[1], '') - .replace(this.mutable.reg[2], '') - .replace(this.mutable.reg[3], '') - .replace(this.mutable.reg[4], '') + .replace(this.static.reg[0], '') + .replace(this.static.reg[1], '') + .replace(this.static.reg[2], '') + .replace(this.static.reg[3], '') + .replace(this.static.reg[4], '') .split(' ') .filter((v) => { return v !== ''; }); - // Fetch user + let user = this.bot.REST.getUser(args[0] ? args.join(' ') : msg.author.id); if (!user) user = this.bot.users.get(msg.author.id); - // Base avatar URL of `user` + let img; const avatarURL = (user.avatar ? user.avatarURL : user.defaultAvatarURL).split('?')[0]; if (edited) { - // Image manipulation const manipulate = async () => { let tmp, dim; - // Fetching avatar of `user` + tmp = await w(avatarURL).send(); tmp = tmp.body; - // Resizing - if (this.mutable.reg[0].test(oArgs)) { + + if (this.static.reg[0].test(oArgs)) { dim = sizeOf(tmp); - tmp = await s(tmp) - .resize(dim.width * 2, dim.height * 2, { kernel: resize === 'LINEAR_B' ? s.kernel.linearB : resize === 'LINEAR_A' ? s.kernel.linearA : resize === 'NEAREST' ? s.kernel.nearest : s.kernel.linearA }) // { fit: 'inside' } + tmp = await sharp(tmp) + .resize(dim.width * 2, dim.height * 2, { + kernel: resize === 'LINEAR_B' ? sharp.kernel.linearB : + resize === 'LINEAR_A' ? sharp.kernel.linearA : + resize === 'NEAREST' ? sharp.kernel.nearest : + sharp.kernel.linearA + }) // { fit: 'inside' } .toBuffer(); } - // Utility customization - if (this.mutable.reg[2].test(oArgs)) tmp = await s(tmp).greyscale().toBuffer(); - if (this.mutable.reg[3].test(oArgs)) tmp = await s(tmp).jpeg({ quality: 5 }).toBuffer(); - if (this.mutable.reg[4].test(oArgs)) tmp = await s(tmp).blur(4).toBuffer(); + + if (this.static.reg[2].test(oArgs)) tmp = await sharp(tmp).greyscale().toBuffer(); + if (this.static.reg[3].test(oArgs)) tmp = await sharp(tmp).jpeg({ quality: 5 }).toBuffer(); + if (this.static.reg[4].test(oArgs)) tmp = await sharp(tmp).blur(4).toBuffer(); return tmp; }; - // Vote lock + const voters = this.bot.cache.get('votes') ? this.bot.cache.get('votes') : this.bot.cache.set('votes', []) && this.bot.cache.get('votes'); if (!this.bot.op(msg.author.id) && !voters.includes(msg.author.id)) { - // Fetch votes - const req = await w(`https://discordbots.org/api/bots/318057009188438016/check?userId=${msg.author.id}`, this.mutable.req).send(); + const req = await w(`https://discordbots.org/api/bots/318057009188438016/check?userId=${msg.author.id}`, this.static.REQ_DATA).send(); const res = req.json(); - // Succeed if code isn't `200` + if(req.statusCode !== 200) { img = await manipulate(); } else { if (res.voted === 0) { - // User didn't vote return msg.channel.createMessage({ embed: { - color : this.bot.col['votelock'], - thumbnail : { url: 'attachment://thumb.png' }, - description: this.localize(msg.author.locale['votelock'].join('\n'), { votelocked: true }) + color: this.bot.col.votelock, + thumbnail: { + url: 'attachment://thumb.png' + }, + description: this._localize(msg.author.locale.votelock.join('\n'), { votelocked: true }) } - }, this.mutable.thumb ? { file: this.mutable.thumb, name: 'thumb.png' } : undefined); + }, this.static.thumb ? { file: this.static.thumb, name: 'thumb.png' } : undefined); } else if (res.voted === 1) { - // User voted img = await manipulate(); voters.push(msg.author.id); } @@ -117,16 +128,17 @@ module.exports = class Avatar extends DiscordCommand { img = await manipulate(); } } else { - // Fetching avatar of `user` img = await w(avatarURL).send(); img = img.body; } msg.channel.createMessage({ embed: { - image : { url: `attachment://thumb${edited ? '.jpg' : extname(avatarURL)}` }, - color : this.bot.col['info']['avatar'], - description: `${this.localize(msg.author.locale['info']['avatar'], { user: user })} (\`${this.mutable.reg[0].test(oArgs.join(' ')) ? 'LARGE' : 'DEFAULT'} ${resize} ${extras}\`)` + color: this.bot.col.info.avatar, + image: { + url: `attachment://thumb${edited ? '.jpg' : extname(avatarURL)}` + }, + description: `${this._localize(msg.author.locale.info.avatar, { user: user })} (\`${this.static.reg[0].test(oArgs.join(' ')) ? 'LARGE' : 'DEFAULT'} ${resize} ${extras}\`)` } }, { file: img, @@ -134,21 +146,25 @@ module.exports = class Avatar extends DiscordCommand { }); } - localize(msg, extData) { - if (!msg) return ''; - - if (extData) { - if (extData.user) { - msg = msg.replace(/\$\[user:full]/g, `${extData.user.username}'${!extData.user.username.toLowerCase().endsWith('s') ? 's' : ''}`); - } - if (extData.votelocked) { - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('votelock', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('votelock', '1')); + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + if (extData.user) { + msg = msg.replace(/\$\[user:full]/g, `${extData.user.username}'${!extData.user.username.toLowerCase().endsWith('s') ? 's' : ''}`); + } + if (extData.votelocked) { + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('votelock', '0')) + .replace(/\$\[emoji#1]/g, this.bot.emote('votelock', '1')); + } } + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'avatar')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'avatar')); } }; \ No newline at end of file diff --git a/src/commands/Discord/Information/commands.js b/src/commands/Discord/Information/commands.js index 7fec835..7b31525 100644 --- a/src/commands/Discord/Information/commands.js +++ b/src/commands/Discord/Information/commands.js @@ -21,8 +21,18 @@ module.exports = class Commands extends DiscordCommand { permissions: [ 'embedLinks' ] }); - try { this.thumb = readFileSync('./assets/img/thumb/commands.png'); } - catch(ex) { this.thumb = undefined; } + this.static = { + inline: true + }; + + try { + this.static.thumb = readFileSync('./assets/img/thumb/commands.png'); + } catch (ex) { + this.static.thumb = undefined; + } + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg, args) { @@ -33,70 +43,81 @@ module.exports = class Commands extends DiscordCommand { msg.channel.createMessage({ embed: { - color : this.bot.col['info']['commands']['single'], - description: this.localize(msg.author.locale['info']['commands']['single'].join('\n'), { msg: msg, cmd: cmd }) + color: this.bot.col.info.commands.single, + description: this._localize(msg.author.locale.info.commands.single.join('\n'), { msg: msg, cmd: cmd }) } }); } else { msg.channel.createMessage({ embed: { - color : this.bot.col['info']['commands']['multi'], - thumbnail : { url: 'attachment://thumb.png' }, - description: this.localize(msg.author.locale['info']['commands']['multi'].join('\n'), { msg: msg }), - fields : [ + color: this.bot.col.info.commands.multi, + thumbnail: { + url: 'attachment://thumb.png' + }, + description: this._localize(msg.author.locale.info.commands.multi.join('\n'), { msg: msg }), + fields: [ this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'core' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][0], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'core' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[0], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'core' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined, this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'information' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][1], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'information' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[1], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'information' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined, this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'image' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][2], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'image' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[2], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'image' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined, this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'utility' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][3], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'utility' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[3], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'utility' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined, this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'developer' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][4], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'developer' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[4], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'developer' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined, this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'tags' && !v.extData.hidden).length >= 1 ? { - name : msg.author.locale['info']['commands']['fields'][5], - value : this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'tags' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), - inline: true + name: msg.author.locale.info.commands.fields[5], + value: this.bot.cmds.filter((v) => v.extData.category.toLowerCase() === 'tags' && !v.extData.hidden).map((v) => `**•** [\`${msg.prefix}${v.extData.name}\`](https://discordapp.com/channels/${msg.channel.guild.id}/${msg.channel.id}/${msg.id}) ${v.extData.description ? `**-** ${v.extData.description}` : ''}`).join('\n'), + inline: this.static.inline } : undefined ] } - }, - this.thumb ? { file: this.thumb, name: 'thumb.png' } : undefined); + }, this.static.thumb ? + { + file: this.static.thumb, + name: 'thumb.png' + } : undefined); } } localize(msg, extData) { - if (!msg) return ''; - - if (extData && extData.msg) msg = msg - .replace(/\$\[guild:name]/g, extData.msg.channel.guild.name) - .replace(/\$\[guild:prefix]/g, extData.msg.prefix) - .replace(/\$\[bot:invite]/g, this.bot.gatherInvite(8)); - - if (extData && extData.cmd) msg = msg - .replace(/\$\[command:name]/g, extData.cmd.name) - .replace(/\$\[command:syntax]/g, `${extData.msg.prefix}${extData.cmd.syntax}`) - .replace(/\$\[command:aliases]/g, extData.cmd.aliases.map((v) => `\`${v}\``).join(', ') || '`n/a`') - .replace(/\$\[command:arguments]/g, extData.cmd.argument.map((v) => `\`${v}\``).join(', ') || '`n/a`') - .replace(/\$\[command:cooldown#formatted]/g, moment.duration(extData.cmd.cooldown ? extData.cmd.cooldown : 0).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]')); - - return msg + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + if (extData.msg) { + msg = msg + .replace(/\$\[guild:name]/g, extData.msg.channel.guild.name) + .replace(/\$\[bot:invite]/g, this.bot.gatherInvite(8)) + .replace(/\$\[guild:prefix]/g, extData.msg.prefix); + } + if (extData.cmd) { + msg = msg + .replace(/\$\[command:name]/g, extData.cmd.name) + .replace(/\$\[command:syntax]/g, `${extData.msg.prefix}${extData.cmd.syntax}`) + .replace(/\$\[command:aliases]/g, extData.cmd.aliases.map((v) => `\`${v}\``).join(', ') || '`n/a`') + .replace(/\$\[command:arguments]/g, extData.cmd.argument.map((v) => `\`${v}\``).join(', ') || '`n/a`') + .replace(/\$\[command:cooldown#formatted]/g, moment.duration(extData.cmd.cooldown ? extData.cmd.cooldown : 0).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]')); + } + } + + return msg .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'commands', '0')) .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'commands', '1')) .replace(/\$\[emoji#2]/g, this.bot.emote('info', 'commands', '2')) @@ -105,5 +126,8 @@ module.exports = class Commands extends DiscordCommand { .replace(/\$\[emoji#5]/g, this.bot.emote('info', 'commands', '5')) .replace(/\$\[emoji#6]/g, this.bot.emote('info', 'commands', '6')) .replace(/\$\[emoji#7]/g, this.bot.emote('info', 'commands', '7')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Information/commits.js b/src/commands/Discord/Information/commits.js index feeccd6..2206d0a 100644 --- a/src/commands/Discord/Information/commits.js +++ b/src/commands/Discord/Information/commits.js @@ -1,6 +1,8 @@ const { DiscordCommand } = require('../../../core'); -const w = require('wumpfetch'); +const w = require('wumpfetch'); +const larg = require('larg'); +const { table } = require('table'); module.exports = class Stats extends DiscordCommand { constructor(bot) { super(bot, { @@ -19,26 +21,72 @@ module.exports = class Stats extends DiscordCommand { permissions: [ 'embedLinks' ] }); + this.static = { + BASE_URL: 'https://api.github.com', + REQ_DATA: { + headers: { + 'User-Agent' : this.bot.ua, + 'Authorization': this.bot.conf.api.dbl + } + }, + TABLE_CONFIG: { + border: { + topBody: `-`, + topJoin: `+`, + topLeft: `+`, + topRight: `+`, + + bottomBody: `-`, + bottomJoin: `+`, + bottomLeft: `+`, + bottomRight: `+`, + + bodyLeft: `|`, + bodyRight: `|`, + bodyJoin: `|`, + + joinBody: `-`, + joinLeft: `+`, + joinRight: `+`, + joinJoin: `+` + }, + drawHorizontalLine: (_, s) => { + return _ === 0 || _ === 1 || _ === s; + } + } + }; + + Object.freeze(this); + Object.freeze(this.static); } - async execute( msg, args ) { - let res = await w('https://api.github.com/repos/PassTheWessel/wump/commits', { headers: { 'User-Agent': this.bot.ua } }).send(); + async execute(msg, args) { + let res = await w(`${this.static.BASE_URL}/repos/PassTheWessel/wump/commits`, this.static.REQ_DATA).send(); res = res.json(); - msg.channel.createMessage({ - embed: { - color : this.bot.col['info']['commits'], - description: this.localize(msg.author.locale['info']['commits'].join('\n'), { total: res.length, commits: res.map((v) => `**•** [\`${v.sha.slice(0, 7)}\`](${v.html_url}) **⤏** ${this.bot.util.shorten(v.commit.message, 80)}`).slice(0, 10).join('\n') }) - } - }); + if (larg(args)['raw'] || larg(args)['r']) { + let data = [ [ 'Hash', 'Message' ] ]; + res.map((v) => data.push([ v.sha.slice(0, 7), this.bot.util.shorten(v.commit.message.replace(/\n/g, ' '), 80) ])); + msg.channel.createMessage(`\`\`\`${table(data.slice(0, 11), this.static.TABLE_CONFIG)}\`\`\``); + } else { + msg.channel.createMessage({ + embed: { + color: this.bot.col.info.commits, + description: this._localize(msg.author.locale.info.commits, { commits: res.map((v) => `**•** [\`${v.sha.slice(0, 7)}\`](${v.html_url}) **⤏** ${this.bot.util.shorten(v.commit.message, 80)}`).slice(0, 10).join('\n') }) + } + }); + } } - localize(msg, extData) { - if (!msg) return ''; - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'commits', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'commits', '1')) - .replace(/\$\[commits:list]/g, `\n${extData.commits}`) - .replace(/\$\[commits:total]/g, `\n${extData.total}`); + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'commits', '0')) + .replace(/\$\[commits:list]/g, `\n${extData.commits}`); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Information/guild.js b/src/commands/Discord/Information/guild.js index 747dec1..4adf71b 100644 --- a/src/commands/Discord/Information/guild.js +++ b/src/commands/Discord/Information/guild.js @@ -5,117 +5,155 @@ const moment = require('moment'); require('../../../util/moment/diff.js'); module.exports = class Guild extends DiscordCommand { constructor(bot) { super(bot, { - name : 'guild', - syntax : 'guild <...guild:string>', - aliases : [ '<...guild:string>' ], - argument : [], - description : 'Get a guild\'s information', + name : 'guild', + syntax : 'guild <...guild:str>', + aliases : [ '<...guild:str>' ], + argument : [], + description: 'Get a guild\'s information', - hidden : false, - enabled : true, - cooldown : 1000, - category : 'Information', - ownerOnly : false, - guildOnly : false, - permissions : [ 'embedLinks' ] + hidden : false, + enabled : true, + cooldown : 1000, + category : 'Information', + ownerOnly : false, + guildOnly : false, + permissions: [ 'embedLinks' ] }); - this.verifications = { '0': 'none', '1': 'low', '2': 'medium', '3': '(╯°□°)╯︵ ┻━┻', '4': '┻━┻ ミヽ(ಠ益ಠ)ノ彡┻━┻' }; - this.explicits = { '0': 'off', '1': 'everyone without roles', '2': 'everyone' }; - this.flags = { 'eu-central': '🇪🇺', 'london': '🇬🇧', 'amsterdam': '🇳🇱', 'japan': '🇯🇵', - 'brazil': '🇧🇷', 'us-west': '🇺🇸', 'hongkong': '🇭🇰', 'sydney': '🇦🇺', - 'singapore': '🇸🇬', 'us-central': '🇺🇸', 'eu-west': '🇪🇺', 'us-south': '🇺🇸', - 'us-east': '🇺🇸', 'frankfurt': '🇩🇪', 'russia': '🇷🇺' }; + this.static = { + inline: true, + + flags: { + 'japan': '🇯🇵', + 'russia': '🇷🇺', + 'brazil': '🇧🇷', + 'london': '🇬🇧', + 'sydney': '🇦🇺', + 'eu-west': '🇪🇺', + 'us-west': '🇺🇸', + 'us-east': '🇺🇸', + 'us-south': '🇺🇸', + 'hongkong': '🇭🇰', + 'amsterdam': '🇳🇱', + 'singapore': '🇸🇬', + 'frankfurt': '🇩🇪', + 'us-central': '🇺🇸', + 'eu-central': '🇪🇺' + }, + explicits: { + '0': 'off', + '1': 'everyone without roles', + '2': 'everyone' + }, + verifications: { + '0': 'none', + '1': 'low', + '2': 'medium', + '3': '(╯°□°)╯︵ ┻━┻', + '4': '┻━┻ ミヽ(ಠ益ಠ)ノ彡┻━┻' + } + }; + + Object.freeze(this); + Object.freeze(this.static); } async execute(msg, args) { - const g = this.bot.REST.getGuild((args.length >= 1 ? args.join(' ') : msg.channel.guild.id)); - - if (!g) return msg.channel.createMessage(this.localize(msg.author.locale['info']['guild']['invalid'])); + const $ = this.bot.REST.getGuild(args.length >= 1 ? args.join(' ') : msg.channel.guild.id); + if (!$) { + return msg.channel.createMessage(this._localize(msg.author.locale.info.guild.invalid)); + } + msg.channel.createMessage({ embed: { - color : this.bot.col['info']['guild'], - thumbnail: { url: g.iconURL }, - fields : [ + color: this.bot.col.info.guild, + thumbnail: { + url: $.iconURL + }, + fields: [ { - name: msg.author.locale['info']['guild']['fields']['guild']['name'], - value: this.localize(msg.author.locale['info']['guild']['fields']['guild']['value'].join('\n'), { guild: g }), - inline: true + name: msg.author.locale.info.guild.fields.guild.name, + value: this._localize(msg.author.locale.info.guild.fields.guild.value.join('\n'), $), + inline: this.static.inline }, { - name: msg.author.locale['info']['guild']['fields']['owner']['name'], - value: this.localize(msg.author.locale['info']['guild']['fields']['owner']['value'].join('\n'), { guild: g }), - inline: true + name: msg.author.locale.info.guild.fields.owner.name, + value: this._localize(msg.author.locale.info.guild.fields.owner.value.join('\n'), $), + inline: this.static.inline }, { - name: msg.author.locale['info']['guild']['fields']['members']['name'], - value: this.localize(msg.author.locale['info']['guild']['fields']['members']['value'].join('\n'), { guild: g }), - inline: true + name: msg.author.locale.info.guild.fields.members.name, + value: this._localize(msg.author.locale.info.guild.fields.members.value.join('\n'), $), + inline: this.static.inline } ] } }); } - localize(msg, extData) { - if (!msg) return ''; + _localize(msg, extData) { + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + const owner = this.bot.users.get(extData.ownerID); + owner.c = moment.preciseDiff(new Date(), new Date(owner.createdAt), true); + owner.cs = [ + owner.c.years >= 1 ? `${owner.c.years} years` : undefined, + owner.c.months >= 1 ? `${owner.c.months} months` : undefined, + owner.c.days >= 1 ? `${owner.c.days} days` : undefined, + owner.days <= 0 && owner.months <= 0 && owner.years <= 0 ? 'Less than a day' : undefined + ].join(' '); - if (extData && extData.guild) { - const o = this.bot.users.get(extData.guild.ownerID); - o.c = moment.preciseDiff(new Date(), new Date(o.createdAt), true); - o.cs = [ - o.c.years >= 1 ? `${o.c.years} years` : undefined, - o.c.months >= 1 ? `${o.c.months} months` : undefined, - o.c.days >= 1 ? `${o.c.days} days` : undefined, - o.days <= 0 && o.months <= 0 && o.years <= 0 ? 'Less than a day' : undefined - ].join(' '); - - extData.guild.c = moment.preciseDiff(new Date(), new Date(extData.guild.createdAt), true); - extData.guild.cs = [ - extData.guild.c.years >= 1 ? `${extData.guild.c.years} years` : undefined, - extData.guild.c.months >= 1 ? `${extData.guild.c.months} months` : undefined, - extData.guild.c.days >= 1 ? `${extData.guild.c.days} days` : undefined, - extData.guild.days <= 0 && extData.guild.months <= 0 && extData.guild.years <= 0 ? 'Less than a day' : undefined - ].join(' '); - - msg = msg - .replace(/\$\[guild:id]/g, extData.guild.id) - .replace(/\$\[guild:name]/g, extData.guild.name) - .replace(/\$\[guild:region]/g, `${this.flags[extData.guild.region] ? this.flags[extData.guild.region] : this.bot.emote('info', 'guild', '2')} ${extData.guild.region}`) - .replace(/\$\[guild:explicity]/g, this.explicits[extData.guild.explicitContentFilter]) - .replace(/\$\[guild:verification]/g, this.verifications[extData.guild.verificationLevel]) - .replace(/\$\[guild:fullChannels]/g , `${extData.guild.channels.filter((v) => v.type === 0).length} / ${extData.guild.channels.filter((v) => v.type === 2).length}`) - .replace(/\$\[guild:users#bot]/g, extData.guild.members.filter((v) => v.bot).length) - .replace(/\$\[guild:users#dnd]/g, extData.guild.members.filter((v) => v.status === 'dnd').length) - .replace(/\$\[guild:users#idle]/g, extData.guild.members.filter((v) => v.status === 'idle').length) - .replace(/\$\[guild:users#total]/g, extData.guild.members.filter((v) => !v.bot).length) - .replace(/\$\[guild:users#online]/g, extData.guild.members.filter((v) => v.status === 'online').length) - .replace(/\$\[guild:users#offline]/g, extData.guild.members.filter((v) => v.status === 'offline').length) - .replace(/\$\[guild:created@precise]/g, extData.guild.cs) - .replace(/\$\[guild:created@exact]/g, moment(extData.guild.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) - .replace(/\$\[guild:owner#id]/g , o.id) - .replace(/\$\[guild:owner#tag]/g , `${o.username}#${o.discriminator}`) - .replace(/\$\[guild:owner#created@precise]/g, o.cs) - .replace(/\$\[guild:owner#created@exact]/g, moment(o.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) - .replace(/\$\[guild:owner#avatar]/g, o.avatar ? o.avatarURL : o.defaultAvatarURL); + extData.c = moment.preciseDiff(new Date(), new Date(extData.createdAt), true); + extData.cs = [ + extData.c.years >= 1 ? `${extData.c.years} years` : undefined, + extData.c.months >= 1 ? `${extData.c.months} months` : undefined, + extData.c.days >= 1 ? `${extData.c.days} days` : undefined, + extData.days <= 0 && extData.months <= 0 && extData.years <= 0 ? 'Less than a day' : undefined + ].join(' '); + + msg = msg + .replace(/\$\[guild:id]/g, extData.id) + .replace(/\$\[guild:name]/g, extData.name) + .replace(/\$\[guild:region]/g, `${this.static.flags[extData.region] ? this.static.flags[extData.region] : this.bot.emote('info', 'guild', '2')} ${extData.region}`) + .replace(/\$\[guild:explicity]/g, this.static.explicits[extData.explicitContentFilter]) + .replace(/\$\[guild:verification]/g, this.static.verifications[extData.verificationLevel]) + .replace(/\$\[guild:fullChannels]/g , `${extData.channels.filter((v) => v.type === 0).length} / ${extData.channels.filter((v) => v.type === 2).length}`) + .replace(/\$\[guild:users#bot]/g, extData.members.filter((v) => v.bot).length) + .replace(/\$\[guild:users#dnd]/g, extData.members.filter((v) => v.status === 'dnd').length) + .replace(/\$\[guild:users#idle]/g, extData.members.filter((v) => v.status === 'idle').length) + .replace(/\$\[guild:users#total]/g, extData.members.filter((v) => !v.bot).length) + .replace(/\$\[guild:users#online]/g, extData.members.filter((v) => v.status === 'online').length) + .replace(/\$\[guild:users#offline]/g, extData.members.filter((v) => v.status === 'offline').length) + .replace(/\$\[guild:created@precise]/g, extData.cs) + .replace(/\$\[guild:created@exact]/g, moment(extData.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) + .replace(/\$\[guild:owner#id]/g , owner.id) + .replace(/\$\[guild:owner#tag]/g , `${owner.username}#${owner.discriminator}`) + .replace(/\$\[guild:owner#created@precise]/g, owner.cs) + .replace(/\$\[guild:owner#created@exact]/g, moment(owner.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) + .replace(/\$\[guild:owner#avatar]/g, owner.avatar ? owner.avatarURL : owner.defaultAvatarURL); + } + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'guild', '0')) + .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'guild', '1')) + .replace(/\$\[emoji#2]/g, this.bot.emote('info', 'guild', '2')) + .replace(/\$\[emoji#3]/g, this.bot.emote('info', 'guild', '3')) + .replace(/\$\[emoji#4]/g, this.bot.emote('info', 'guild', '4')) + .replace(/\$\[emoji#5]/g, this.bot.emote('info', 'guild', '5')) + .replace(/\$\[emoji#6]/g, this.bot.emote('info', 'guild', '6')) + .replace(/\$\[emoji#7]/g, this.bot.emote('info', 'guild', '7')) + .replace(/\$\[emoji#8]/g, this.bot.emote('info', 'guild', '8')) + .replace(/\$\[emoji#9]/g, this.bot.emote('info', 'guild', '9')) + .replace(/\$\[emoji#10]/g, this.bot.emote('info', 'guild', '10')) + .replace(/\$\[emoji#11]/g, this.bot.emote('info', 'guild', '11')) + .replace(/\$\[emoji#12]/g, this.bot.emote('info', 'guild', '12')) + .replace(/\$\[emoji#13]/g, this.bot.emote('info', 'guild', '13')) + .replace(/\$\[emoji#14]/g, this.bot.emote('info', 'guild', '14')) + .replace(/\$\[emoji#15]/g, this.bot.emote('info', 'guild', '15')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; } - - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'guild', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'guild', '1')) - .replace(/\$\[emoji#2]/g, this.bot.emote('info', 'guild', '2')) - .replace(/\$\[emoji#3]/g, this.bot.emote('info', 'guild', '3')) - .replace(/\$\[emoji#4]/g, this.bot.emote('info', 'guild', '4')) - .replace(/\$\[emoji#5]/g, this.bot.emote('info', 'guild', '5')) - .replace(/\$\[emoji#6]/g, this.bot.emote('info', 'guild', '6')) - .replace(/\$\[emoji#7]/g, this.bot.emote('info', 'guild', '7')) - .replace(/\$\[emoji#8]/g, this.bot.emote('info', 'guild', '8')) - .replace(/\$\[emoji#9]/g, this.bot.emote('info', 'guild', '9')) - .replace(/\$\[emoji#10]/g, this.bot.emote('info', 'guild', '10')) - .replace(/\$\[emoji#11]/g, this.bot.emote('info', 'guild', '11')) - .replace(/\$\[emoji#12]/g, this.bot.emote('info', 'guild', '12')) - .replace(/\$\[emoji#13]/g, this.bot.emote('info', 'guild', '13')) - .replace(/\$\[emoji#14]/g, this.bot.emote('info', 'guild', '14')) - .replace(/\$\[emoji#15]/g, this.bot.emote('info', 'guild', '15')); } }; \ No newline at end of file diff --git a/src/commands/Discord/Information/stats.js b/src/commands/Discord/Information/stats.js index d000750..8be3ca3 100644 --- a/src/commands/Discord/Information/stats.js +++ b/src/commands/Discord/Information/stats.js @@ -17,75 +17,103 @@ module.exports = class Stats extends DiscordCommand { hidden : false, enabled : true, - cooldown : 10000, + cooldown : 2500, category : 'Information', ownerOnly : false, guildOnly : false, permissions: [ 'embedLinks' ] }); - try { this.thumb = readFileSync('./assets/img/thumb/stats.png'); } - catch(ex) { this.thumb = undefined; } - - this.mutable = { + this.static = { + inline: true, TABLE_CONFIG: { border: { - topBody: `-`, - topJoin: `+`, - topLeft: `+`, - topRight: `+`, + topBody: `-`, + topJoin: `+`, + topLeft: `+`, + topRight: `+`, - bottomBody: `-`, - bottomJoin: `+`, - bottomLeft: `+`, - bottomRight: `+`, + bottomBody: `-`, + bottomJoin: `+`, + bottomLeft: `+`, + bottomRight: `+`, - bodyLeft: `|`, - bodyRight: `|`, - bodyJoin: `|`, + bodyLeft: `|`, + bodyRight: `|`, + bodyJoin: `|`, - joinBody: `-`, - joinLeft: `+`, - joinRight: `+`, - joinJoin: `+` + joinBody: `-`, + joinLeft: `+`, + joinRight: `+`, + joinJoin: `+` }, drawHorizontalLine: (_, s) => { - return _ === 0 || _ === 1 || _ === s; + return _ === 0 || _ === 1 || _ === s; } } }; - } - async execute( msg, args ) { - args = larg(args); - const message = await msg.channel.createMessage(this.localize(msg.author.locale['info']['stats']['fetching'])); - - const cpus = os.cpus(); - let platform = { - 'arch' : os.arch() ? os.arch() : '??x', + this.mutable.platform = { + 'arch' : os.arch() ? os.arch() : '32x', + 'cpus' : os.cpus(), 'release' : os.release() ? os.release() : '?.?.?', 'platform': os.platform() }; - switch (platform.platform) { - case 'aix' : platform.platform = 'Linux'; break; - case 'sunos' : platform.platform = 'Linux'; break; - case 'win32' : platform.platform = 'Windows'; break; - case 'linux' : platform.platform = 'Linux'; break; - case 'darwin' : platform.platform = 'Macintosh'; break; - case 'freebsd': platform.platform = 'Linux'; break; - case 'openbsd': platform.platform = 'Linux'; break; - case 'android': platform.platform = 'Android'; break; - default : platform.platform = 'Unknown'; + try { + this.static.thumb = readFileSync('./assets/img/thumb/stats.png'); + } catch (ex) { + this.thumb = undefined; } - let fields = []; + Object.freeze(this); + Object.freeze(this.static); + } - if (!args['no-bot']) { + async execute(msg, args) { + args = larg(args); + let fields = []; + const message = await msg.channel.createMessage(this._localize(msg.author.locale.info.stats.fetching)); + + switch (this.mutable.platform.platform) { + case 'aix' : this.mutable.platform.platform = 'Linux'; break; + case 'sunos' : this.mutable.platform.platform = 'Linux'; break; + case 'win32' : this.mutable.platform.platform = 'Windows'; break; + case 'linux' : this.mutable.platform.platform = 'Linux'; break; + case 'darwin' : this.mutable.platform.platform = 'Macintosh'; break; + case 'freebsd': this.mutable.platform.platform = 'Linux'; break; + case 'openbsd': this.mutable.platform.platform = 'Linux'; break; + case 'android': this.mutable.platform.platform = 'Android'; break; + default : this.mutable.platform.platform = 'Unknown'; + } + + + if (args['raw'] || args['r']) { + fields = { + raw: true, + content: [ + [ 'Key', 'Value' ], + [ 'Shards [C / T]', `${msg.channel.guild.shard.id} / ${this.bot.shards.size}` ], + [ 'Cache [U / G]', `${this.bot.cache.get('users').length || 0} / ${this.bot.cache.get('guilds').length || 0}`], + [ 'Channels', Object.keys(this.bot.channelGuildMap).length ], + [ 'Guilds', this.bot.guilds.size ], + [ 'Users', this.bot.users.size ], + [ '', '' ], + [ 'OS', `${this.mutable.platform.platform} ${this.mutable.platform.release} (${this.mutable.platform.arch})` ], + [ 'Uptime', moment.duration(this.bot.uptime).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]') ], + [ 'OS Uptime', moment.duration(os.uptime() * 1000).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]') ], + [ 'User-Agent', this.bot.ua ], + [ 'Memory Usage', `${this._formatBytes(process.memoryUsage().heapUsed)} / ${this._formatBytes(os.totalmem())} (${((process.memoryUsage().heapUsed / os.totalmem()) * 100).toFixed(2)}%)` ], + [ 'Process Hash', process.hash ], + [ 'Load average', os.loadavg().map(v => v.toFixed(2)).join(' / ') ] + ] + }; + } + + if (!args['no-bot'] && !args['raw'] && !args['r']) { fields.push({ - name : '❯ Bot information', - value : - [ + name: '❯ Bot information', + value: [ `${this.bot.emote('info', 'statistics', '7')} **User Agent**: ${this.bot.ua ? this.bot.ua : 'Unknown'}`, `${this.bot.emote('info', 'statistics', '8')} **Shards \`[Current / Total]\`**: ${msg.channel.guild.shard.id} **/** ${this.bot.shards.size}`, `${this.bot.emote('info', 'statistics', '9')} **Cache \`[Users / Guilds]\`**: ${this.bot.cache.get('users').length || 0} **/** ${this.bot.cache.get('guilds').length || 0}`, @@ -93,82 +121,73 @@ module.exports = class Stats extends DiscordCommand { `${this.bot.emote('info', 'statistics', '11')} **Guilds**: ${this.bot.guilds.size}`, `${this.bot.emote('info', 'statistics', '12')} **Users**: ${this.bot.users.size}` ].join('\n'), - inline: true + inline: this.static.inline }); } - if (!args['no-sys']) { + + if (!args['no-sys'] && !args['raw'] && !args['r']) { fields.push({ - name : '❯ System and process information', - value : - [ - `${this.bot.emote('info', 'statistics', '1')} **Operating System**: ${platform.platform} ${platform.release} **(**${platform.arch}**)**`, + name: '❯ System and process information', + value: [ + `${this.bot.emote('info', 'statistics', '1')} **Operating System**: ${this.mutable.platform.platform} ${this.mutable.platform.release} **(**${this.mutable.platform.arch}**)**`, `${this.bot.emote('info', 'statistics', '2')} **Process Hash**: ${process.hash}`, `${this.bot.emote('info', 'statistics', '3')} **Load Average**: ${os.loadavg().map(v => v.toFixed(2)).join(' **/** ')}`, `${this.bot.emote('info', 'statistics', '4')} **OS Uptime**: ${moment.duration(os.uptime() * 1000).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]')}`, `${this.bot.emote('info', 'statistics', '5')} **Uptime**: ${moment.duration(this.bot.uptime).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]')}`, - `${this.bot.emote('info', 'statistics', '6')} **Memory**: ${this.formatBytes(process.memoryUsage().heapUsed)} **/** ${this.formatBytes(os.totalmem())} **(**${((process.memoryUsage().heapUsed / os.totalmem()) * 100).toFixed(2)}%**)**` + `${this.bot.emote('info', 'statistics', '6')} **Memory**: ${this._formatBytes(process.memoryUsage().heapUsed)} **/** ${this._formatBytes(os.totalmem())} **(**${((process.memoryUsage().heapUsed / os.totalmem()) * 100).toFixed(2)}%**)**` ].join('\n'), - inline: true + inline: this.static.inline }); } - if (!args['no-deps']) { + + if (!args['no-deps'] && !args['raw'] && !args['r']) { fields.push({ - name : '❯ Runtime & Dependencies', - value : `**Node.js**: ${process.version}\n\n${Array.list(Object.keys(this.bot.pkg.dependencies).map((v, _) => `\`${v}\``))}`, - inline: true + name: '❯ Runtime & Dependencies', + value: `**Node.js**: ${process.version}\n\n${Array.list(Object.keys(this.bot.pkg.dependencies).map((v, _) => `\`${v}\``))}`, + inline: this.static.inline }); } - if (!args['no-cpu']) { + + if (!args['no-cpu'] && !args['raw'] && !args['r']) { fields.push({ - name : '❯ CPU information', - value : `\`\`\`ini\navailable=${cpus.length}\nendianness="${os.endianness()}"\n; List of all (available) CPU's\n${cpus.map((v, _) => `[${_ + 1}] ${v.model}`).join('\n')}\`\`\``, - inline: true + name: '❯ CPU information', + value: `\`\`\`ini\navailable=${this.mutable.platform.cpus.length}\nendianness="${os.endianness()}"\n; List of all (available) CPU's\n${this.mutable.platform.cpus.map((v, _) => `[${_ + 1}] ${v.model}`).join('\n')}\`\`\``, + inline: this.static.inline }); } - - if (args['raw']) { - fields = { - raw: true, - content: [ - [ 'Key', 'Value' ], - [ 'Shards [C / T]', `${msg.channel.guild.shard.id} / ${this.bot.shards.size}` ], - [ 'Cache [U / G]', `${this.bot.cache.get('users').length || 0} / ${this.bot.cache.get('guilds').length || 0}`], - [ 'Channels', Object.keys(this.bot.channelGuildMap).length ], - [ 'Guilds', this.bot.guilds.size ], - [ 'Users', this.bot.users.size ], - [ '', '' ], - [ 'OS', `${platform.platform} ${platform.release} (${platform.arch})` ], - [ 'Uptime', moment.duration(this.bot.uptime).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]') ], - [ 'OS Uptime', moment.duration(os.uptime() * 1000).format('YYYY[y] MM[M] DD[d] HH[h] mm[m] ss[s]') ], - [ 'User-Agent', this.bot.ua ], - [ 'Memory Usage', `${this.formatBytes(process.memoryUsage().heapUsed)} / ${this.formatBytes(os.totalmem())} (${((process.memoryUsage().heapUsed / os.totalmem()) * 100).toFixed(2)}%)` ], - [ 'Process Hash', process.hash ], - [ 'Load average', os.loadavg().map(v => v.toFixed(2)).join(' / ') ] - ] - }; - } msg.channel.createMessage( - fields.raw ? `\`\`\`${table(fields.content, this.mutable.TABLE_CONFIG)}\`\`\`` : + fields.raw ? `\`\`\`${table(fields.content, this.static.TABLE_CONFIG)}\`\`\`` : { content: '', embed: { - color : this.bot.col['info']['stats'], - fields : fields, - thumbnail: { url: !args['no-thumb'] ? 'attachment://thumb.png' : '' } + color: this.bot.col.info.stats, + fields: fields, + thumbnail: { + url: !args['no-thumb'] ? 'attachment://thumb.png' : '' + } } - }, - this.thumb && !fields.raw && !args['no-thumb'] ? { file: this.thumb, name: 'thumb.png' } : ''); + }, this.thumb && !fields.raw && !args['no-thumb'] ? + { + file: this.thumb, + name: 'thumb.png' + } : ''); + message.delete(); } - formatBytes(b) { + _formatBytes(b) { if(b < 1073741824) return(b / 1048576).toFixed(2) + ' MB'; else return(b / 1073741824).toFixed(2) + ' GB'; } - localize(msg) { - if (!msg) return ''; - return msg.replace(/\$\[emoji#0]/g, this.bot.emote('info', 'statistics', '0')); + _localize(msg) { + try { + if (!msg) throw 'INVALID_STRING'; + + return msg.replace(/\$\[emoji#0]/g, this.bot.emote('info', 'statistics', '0')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex.code}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Information/user.js b/src/commands/Discord/Information/user.js index b10b5d6..1f43329 100644 --- a/src/commands/Discord/Information/user.js +++ b/src/commands/Discord/Information/user.js @@ -1,77 +1,81 @@ const { DiscordCommand } = require('../../../core'); -const m = require('moment'); require('../../../util/moment/diff.js'); +const moment = require('moment'); require('../../../util/moment/diff.js'); -module.exports = class Guild extends DiscordCommand { +module.exports = class User extends DiscordCommand { constructor(bot) { super(bot, { - name : 'user', - syntax : 'user <...user:string>', - aliases : [ '<...user:string>' ], - argument : [], - description : 'Get a user\'s information', + name : 'user', + syntax : 'user <...user:string>', + aliases : [ '<...user:string>' ], + argument : [], + description: 'Get a user\'s information', - hidden : false, - enabled : true, - cooldown : 1000, - category : 'Information', - ownerOnly : false, - guildOnly : false, - permissions : [ 'embedLinks' ] + hidden : false, + enabled : true, + cooldown : 1000, + category : 'Information', + ownerOnly : false, + guildOnly : false, + permissions: [ 'embedLinks' ] }); - this.mutable.status = { - '_unknown': `${this.bot.emote('info', 'user', '6')}Unknown`, - 'online' : `${this.bot.emote('info', 'user', '7')}online`, - 'idle' : `${this.bot.emote('info', 'user', '8')}away`, - 'dnd' : `${this.bot.emote('info', 'user', '9')}do not disturb`, - 'offline' : `${this.bot.emote('info', 'user', '10')}offline` + this.static = { + status: { + '_unknown': `${this.bot.emote('info', 'user', '6')}Unknown`, + 'online' : `${this.bot.emote('info', 'user', '7')}online`, + 'idle' : `${this.bot.emote('info', 'user', '8')}away`, + 'dnd' : `${this.bot.emote('info', 'user', '9')}do not disturb`, + 'offline' : `${this.bot.emote('info', 'user', '10')}offline` + } }; } async execute(msg, args) { - const u = this.bot.REST.getUser((args.length >= 1 ? args.join(' ') : msg.author.id)); - if (!u) return msg.channel.createMessage(this._localize(msg.author.locale['info']['user']['invalid'])); + const $ = this.bot.REST.getUser(args.length >= 1 ? args.join(' ') : msg.author.id); + if (!$) { + return msg.channel.createMessage(this._localize(msg.author.locale.info.user.invalid)); + } - const mem = msg.channel.guild && msg.channel.guild.members.get(u.id) ? msg.channel.guild.members.get(u.id) : undefined; + const m = msg.channel.guild && msg.channel.guild.members.get($.id) ? msg.channel.guild.members.get($.id) : undefined; const structure = { embed: { - color: this.bot.col['info']['guild'], + color: this.bot.col.info.user, thumbnail: { - url: u.avatar ? u.avatarURL : u.defaultAvatarURL + url: $.avatar ? $.avatarURL : $.defaultAvatarURL }, fields: [ { - name: msg.author.locale['info']['user']['fields']['self']['name'], - value: this._localize(msg.author.locale['info']['user']['fields']['self']['value'].join('\n'), { user: u, member: mem }), + name: msg.author.locale.info.user.fields.self.name, + value: this._localize(msg.author.locale.info.user.fields.self.value.join('\n'), { user: $, member: m }), inline: true } ] } }; - if (mem) { + if (m) { structure.embed.fields.push({ - name: msg.author.locale['info']['user']['fields']['member']['name'], - value: this._localize(msg.author.locale['info']['user']['fields']['member']['value'].join('\n'), { user: u, member: mem, guildId: msg.channel.guild.id }), + name: msg.author.locale.info.user.fields.member.name, + value: this._localize(msg.author.locale.info.user.fields.member.value.join('\n'), { user: $, member: m, guildId: msg.channel.guild.id }), inline: true }); } - const mutual = this.bot.guilds.filter((v) => v.members.has(u.id)).map((v) => v.name); + const mutual = this.bot.guilds.filter((v) => v.members.has($.id)).map((v) => v.name); structure.embed.fields.push({ - name: msg.author.locale['info']['user']['fields']['mutual'], - value: mutual.length >= 1 ? this._limit(mutual, 25).join(' **/** ') : 'none', - inline: true + name: msg.author.locale.info.user.fields.mutual, + value: mutual.length >= 1 ? this._limit(mutual, 25).join(' **/** ') : 'none' }); msg.channel.createMessage(structure); } _limit(array, max) { - if (array.length <= max) return array; - else { + if (array.length <= max) { + return array; + } else { const length = array.length - max; array.splice(max, array.length - max); array.push(`And ${length} more...`); @@ -81,49 +85,56 @@ module.exports = class Guild extends DiscordCommand { } _localize(msg, extData) { - if (!msg) return ''; - if (extData) { - if (extData.user) { - const u = extData.user; - u.c = m.preciseDiff(new Date(), new Date(u.createdAt), true); - u.cs = [ - u.c.years >= 1 ? `${u.c.years} years` : undefined, - u.c.months >= 1 ? `${u.c.months} months` : undefined, - u.c.days >= 1 ? `${u.c.days} days` : undefined, - u.c.days <= 0 && u.c.months <= 0 && u.c.years <= 0 ? 'Less than a day' : undefined - ].join(' '); + try { + if (!msg) throw 'INVALID_STRING'; + + if (extData) { + if (extData.user) { + const u = extData.user; + u.c = moment.preciseDiff(new Date(), new Date(u.createdAt), true); + u.cs = [ + u.c.years >= 1 ? `${u.c.years} years` : undefined, + u.c.months >= 1 ? `${u.c.months} months` : undefined, + u.c.days >= 1 ? `${u.c.days} days` : undefined, + u.c.days <= 0 && u.c.months <= 0 && u.c.years <= 0 ? 'Less than a day' : undefined + ].join(' '); msg = msg .replace(/\$\[user:id]/g , u.id) .replace(/\$\[user:tag]/g , `${u.username}#${u.discriminator}`) - .replace(/\$\[user:created@exact]/g, m(u.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) + .replace(/\$\[user:created@exact]/g, moment(u.createdAt).format('YYYY[/]MM[/]DD HH[:]mm')) .replace(/\$\[user:created@precise]/g, u.cs) - .replace(/\$\[user:status@full]/g, extData.member && this.mutable.status[extData.member.status] ? this.mutable.status[extData.member.status] : this.mutable.status['_unknown']); + .replace(/\$\[user:status@full]/g, extData.member && this.static.status[extData.member.status] ? this.static.status[extData.member.status] : this.static.status['_unknown']); } - if (extData.member) { - const mem = extData.member; - mem.c = m.preciseDiff(new Date(), new Date(mem.joinedAt), true); - mem.cs = [ - mem.c.years >= 1 ? `${mem.c.years} years` : undefined, - mem.c.months >= 1 ? `${mem.c.months} months` : undefined, - mem.c.days >= 1 ? `${mem.c.days} days` : undefined, - mem.c.days <= 0 && mem.c.months <= 0 && mem.c.years <= 0 ? 'Less than a day' : undefined - ].join(' '); - msg = msg - .replace(/\$\[member:nickname]/g, mem && mem.nick ? this.bot.util.escapeMarkdown(mem.nick) : '*No nickname set*') - .replace(/\$\[member:roles]/g, mem.roles.length >= 1 ? mem.roles.map((v) => `<@&${v}>`).slice(0, 25).join(' **/** ') : 'n/a') - .replace(/\$\[member:created@exact]/g, m(mem.joinedAt).format('YYYY[/]MM[/]DD HH[:]mm')) - .replace(/\$\[member:created@precise]/g, mem.cs); - } - } - return msg - .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'user', '0')) - .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'user', '1')) - .replace(/\$\[emoji#2]/g, this.bot.emote('info', 'user', '2')) - .replace(/\$\[emoji#3]/g, this.bot.emote('info', 'user', '3')) - .replace(/\$\[emoji#4]/g, this.bot.emote('info', 'user', '4')) - .replace(/\$\[emoji#5]/g, this.bot.emote('info', 'user', '5')) - .replace(/\$\[emoji#11]/g, this.bot.emote('info', 'user', '11')); + if (extData.member) { + const m = extData.member; + m.c = moment.preciseDiff(new Date(), new Date(m.joinedAt), true); + m.cs = [ + m.c.years >= 1 ? `${m.c.years} years` : undefined, + m.c.months >= 1 ? `${m.c.months} months` : undefined, + m.c.days >= 1 ? `${m.c.days} days` : undefined, + m.c.days <= 0 && m.c.months <= 0 && m.c.years <= 0 ? 'Less than a day' : undefined + ].join(' '); + + msg = msg + .replace(/\$\[member:nickname]/g, m && m.nick ? this.bot.util.escapeMarkdown(m.nick) : '*No nickname set*') + .replace(/\$\[member:roles]/g, m.roles.length >= 1 ? this._limit(m.roles.map((v) => `<@&${v}>`), 25).join(' **/** ') : 'none') + .replace(/\$\[member:created@exact]/g, moment(m.joinedAt).format('YYYY[/]MM[/]DD HH[:]mm')) + .replace(/\$\[member:created@precise]/g, m.cs); + } + } + + return msg + .replace(/\$\[emoji#0]/g, this.bot.emote('info', 'user', '0')) + .replace(/\$\[emoji#1]/g, this.bot.emote('info', 'user', '1')) + .replace(/\$\[emoji#2]/g, this.bot.emote('info', 'user', '2')) + .replace(/\$\[emoji#3]/g, this.bot.emote('info', 'user', '3')) + .replace(/\$\[emoji#4]/g, this.bot.emote('info', 'user', '4')) + .replace(/\$\[emoji#5]/g, this.bot.emote('info', 'user', '5')) + .replace(/\$\[emoji#11]/g, this.bot.emote('info', 'user', '11')); + } catch (ex) { + return `LOCALIZE_ERROR:${ex}`; + } } }; \ No newline at end of file diff --git a/src/commands/Discord/Tags/tag-info.js b/src/commands/Discord/Tags/tag-info.js index cf6de29..840258a 100644 --- a/src/commands/Discord/Tags/tag-info.js +++ b/src/commands/Discord/Tags/tag-info.js @@ -58,6 +58,6 @@ module.exports = class TagInfo extends DiscordCommand { .replace(/\$\[emoji#3]/g, this.bot.emote('tags', 'info', '3')) .replace(/\$\[emoji#4]/g, this.bot.emote('tags', 'info', '4')) .replace(/\$\[emoji#5]/g, this.bot.emote('tags', 'info', '5')); - } catch(ex) {} + } catch (ex) {} } }; \ No newline at end of file diff --git a/src/commands/Discord/Utility/math.js b/src/commands/Discord/Utility/math.js index 6592d17..d0c6c11 100644 --- a/src/commands/Discord/Utility/math.js +++ b/src/commands/Discord/Utility/math.js @@ -62,7 +62,7 @@ module.exports = class Math extends DiscordCommand { } return msg.replace(/\$\[emoji#0]/g, this.bot.emote('util', 'math', '0')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/commands/Discord/Utility/snipe.js b/src/commands/Discord/Utility/snipe.js index 2cf24fc..d30186b 100644 --- a/src/commands/Discord/Utility/snipe.js +++ b/src/commands/Discord/Utility/snipe.js @@ -102,7 +102,7 @@ module.exports = class Snipe extends DiscordCommand { } return msg.replace(/\$\[emoji#0]/g, this.bot.emote('util', 'snipe')); - } catch(ex) { + } catch (ex) { return `LOCALIZE_ERROR:${ex.code}`; } } diff --git a/src/core/registry/Discord/Commands.js b/src/core/registry/Discord/Commands.js index 5f0eab9..fffffae 100644 --- a/src/core/registry/Discord/Commands.js +++ b/src/core/registry/Discord/Commands.js @@ -39,7 +39,7 @@ module.exports = class CommandRegistry { cmds.forEach((cmd) => { try { this.start(directory, categories[i], cmd); - } catch(ex) { process.handleError(err, 'LoadError', cyan('Discord')); } + } catch (ex) { process.handleError(err, 'LoadError', cyan('Discord')); } }); }); }