From c1f92a683e5dbe02f4c628fa752af7ed6510a26e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 26 Feb 2020 18:15:47 -0500 Subject: [PATCH] Cloc Command --- README.md | 3 ++- commands/util/cloc.js | 46 +++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 commands/util/cloc.js diff --git a/README.md b/README.md index 4961e33a..7eb48691 100644 --- a/README.md +++ b/README.md @@ -91,12 +91,13 @@ Xiao is a Discord bot coded in JavaScript with ## Commands -Total: 361 +Total: 362 ### Utility: * **eval:** Executes JavaScript code. * **changelog:** Responds with the bot's latest 10 commits. +* **cloc:** Responds with the bot's code line count. * **credit:** Responds with a command's credits list. * **donate:** Responds with the bot's donation links. * **help:** Displays a list of available commands, or detailed information for a specific command. diff --git a/commands/util/cloc.js b/commands/util/cloc.js new file mode 100644 index 00000000..fa160359 --- /dev/null +++ b/commands/util/cloc.js @@ -0,0 +1,46 @@ +const Command = require('../../structures/Command'); +const MessageEmbed = require('../../structures/MessageEmbed'); +const { formatNumber } = require('../../util/Util'); +const { promisify } = require('util'); +const exec = promisify(require('child_process').exec); +const path = require('path'); + +module.exports = class ClocCommand extends Command { + constructor(client) { + super(client, { + name: 'cloc', + group: 'util', + memberName: 'cloc', + description: 'Responds with the bot\'s code line count.', + guarded: true, + clientPermissions: ['EMBED_LINKS'] + }); + + this.cache = null; + } + + async run(msg) { + const cloc = this.cache || await this.cloc(); + const embed = new MessageEmbed() + .setColor(0x00AE86) + .setFooter(`${cloc.header.cloc_url} ${cloc.header.cloc_version}`) + .addField(`❯ Javascript (${formatNumber(cloc.JavaScript.nFiles)})`, + formatNumber(cloc.JavaScript.code), true) + .addField(`❯ JSON (${formatNumber(cloc.JSON.nFiles)})`, formatNumber(cloc.JSON.code), true) + .addField(`❯ Markdown (${formatNumber(cloc.Markdown.nFiles)})`, formatNumber(cloc.Markdown.code), true) + .addBlankField(true) + .addField(`❯ Total (${formatNumber(cloc.SUM.nFiles)})`, formatNumber(cloc.SUM.code)) + .addBlankField(true); + return msg.embed(embed); + } + + async cloc() { + const { stdout, stderr } = await exec( + path.join(__dirname, '..', '..', 'node_modules', '.bin', 'cloc'), + ['--json', '--exclude-dir=node_modules', path.join(__dirname, '..', '..')] + ); + if (stderr) throw new Error(stderr.trim()); + this.cache = JSON.parse(stdout.trim()); + return this.cache; + } +}; diff --git a/package.json b/package.json index 6708b260..f4bb1e1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "110.8.2", + "version": "110.9.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -34,6 +34,7 @@ "@vitalets/google-translate-api": "^3.0.0", "canvas": "^2.6.1", "cheerio": "^1.0.0-rc.3", + "cloc": "^2.5.1", "common-tags": "^1.8.0", "custom-translate": "^2.2.8", "discord.js": "github:discordjs/discord.js",