GCD Command

This commit is contained in:
Dragon Fire
2021-05-02 21:18:27 -04:00
parent ccebe89c2a
commit ef06abf451
4 changed files with 54 additions and 21 deletions
+3 -20
View File
@@ -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}`;
}
};
+36
View File
@@ -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));
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "139.1.0",
"version": "139.2.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"private": true,
+14
View File
@@ -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) => {