diff --git a/commands/analyze/aspect-ratio.js b/commands/analyze/aspect-ratio.js index e813a889..176dd999 100644 --- a/commands/analyze/aspect-ratio.js +++ b/commands/analyze/aspect-ratio.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { loadImage } = require('canvas'); const request = require('node-superfetch'); +const { gcd } = require('../../util/Util'); module.exports = class AspectRatioCommand extends Command { constructor(client) { @@ -28,28 +29,10 @@ module.exports = class AspectRatioCommand extends Command { try { const { body } = await request.get(image); const data = await loadImage(body); - return msg.reply(`This image has an aspect ratio of **${this.ratio(data.width, data.height)}**.`); + const common = gcd(data.width, data.height); + return msg.reply(`This image has an aspect ratio of **${data.width / common}:${data.height / common}**.`); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - gcd(a, b) { - if (b > a) { - const temp = a; - a = b; - b = temp; - } - while (b !== 0) { - const m = a % b; - a = b; - b = m; - } - return a; - } - - ratio(x, y) { - const c = this.gcd(x, y); - return `${x / c}:${y / c}`; - } }; diff --git a/commands/edit-number/gcd.js b/commands/edit-number/gcd.js new file mode 100644 index 00000000..a1aa1ca5 --- /dev/null +++ b/commands/edit-number/gcd.js @@ -0,0 +1,36 @@ +const Command = require('../../structures/Command'); +const { gcd } = require('../../util/Util'); + +module.exports = class GcdCommand extends Command { + constructor(client) { + super(client, { + name: 'gcd', + aliases: ['greatest-common-denominator'], + group: 'edit-number', + memberName: 'gcd', + description: 'Determines two numbers\' greatest common denominator.', + args: [ + { + key: 'number1', + label: 'first number', + prompt: 'What is the first number you want to check?', + type: 'integer', + max: Number.MAX_SAFE_INTEGER, + min: 1 + }, + { + key: 'number2', + label: 'second number', + prompt: 'What is the second number you want to check?', + type: 'integer', + max: Number.MAX_SAFE_INTEGER, + min: 1 + } + ] + }); + } + + run(msg, { number1, number2 }) { + return msg.reply(gcd(number1, number2)); + } +}; diff --git a/package.json b/package.json index cc9fab63..fca10ae6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "139.1.0", + "version": "139.2.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true, diff --git a/util/Util.js b/util/Util.js index f6ab991e..401c9230 100644 --- a/util/Util.js +++ b/util/Util.js @@ -111,6 +111,20 @@ module.exports = class Util { return crypto.createHash(algorithm).update(text).digest('hex'); } + static gcd(a, b) { + if (b > a) { + const temp = a; + a = b; + b = temp; + } + while (b !== 0) { + const m = a % b; + a = b; + b = m; + } + return a; + } + static streamToArray(stream) { if (!stream.readable) return Promise.resolve([]); return new Promise((resolve, reject) => {