mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-16 00:12:32 +02:00
GCD Command
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const { loadImage } = require('canvas');
|
const { loadImage } = require('canvas');
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
|
const { gcd } = require('../../util/Util');
|
||||||
|
|
||||||
module.exports = class AspectRatioCommand extends Command {
|
module.exports = class AspectRatioCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -28,28 +29,10 @@ module.exports = class AspectRatioCommand extends Command {
|
|||||||
try {
|
try {
|
||||||
const { body } = await request.get(image);
|
const { body } = await request.get(image);
|
||||||
const data = await loadImage(body);
|
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) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
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}`;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "139.1.0",
|
"version": "139.2.0",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
|||||||
@@ -111,6 +111,20 @@ module.exports = class Util {
|
|||||||
return crypto.createHash(algorithm).update(text).digest('hex');
|
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) {
|
static streamToArray(stream) {
|
||||||
if (!stream.readable) return Promise.resolve([]);
|
if (!stream.readable) return Promise.resolve([]);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user