mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 15:07:42 +02:00
Dominant Color Command
This commit is contained in:
@@ -260,7 +260,7 @@ in the appropriate channel's topic to use it.
|
||||
|
||||
## Commands
|
||||
|
||||
Total: 575
|
||||
Total: 576
|
||||
|
||||
### Utility:
|
||||
|
||||
@@ -542,6 +542,7 @@ Total: 575
|
||||
* **birthstone:** Responds with the Birthstone for a month.
|
||||
* **character-count:** Responds with the character count of text.
|
||||
* **chinese-zodiac:** Responds with the Chinese Zodiac Sign for the given year.
|
||||
* **dominant-color:** Determines the dominant color of an image.
|
||||
* **face:** Determines the race, gender, and age of a face.
|
||||
* **gender:** Determines the gender of a name.
|
||||
* **has-transparency:** Determines if an image has transparency in it.
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const request = require('node-superfetch');
|
||||
const { rgbToHex } = require('../../util/Util');
|
||||
|
||||
module.exports = class DominantColorCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'dominant-color',
|
||||
aliases: ['dom-color', 'dominant-colour', 'dom-colour'],
|
||||
group: 'analyze',
|
||||
memberName: 'dominant-color',
|
||||
description: 'Determines the dominant color of an image.',
|
||||
throttling: {
|
||||
usages: 1,
|
||||
duration: 10
|
||||
},
|
||||
args: [
|
||||
{
|
||||
key: 'image',
|
||||
prompt: 'What image would you like to test?',
|
||||
type: 'image',
|
||||
default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 })
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg, { image }) {
|
||||
try {
|
||||
const { body } = await request.get(image);
|
||||
const data = await loadImage(body);
|
||||
const canvas = createCanvas(250, 250);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.drawImage(data, 0, 0, 1, 1);
|
||||
const imgData = ctx.getImageData(0, 0, 1, 1).data;
|
||||
const hexColor = `#${rgbToHex(imgData[0], imgData[1], imgData[2]).padStart(6, '0')}`;
|
||||
ctx.fillStyle = hexColor;
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
return msg.say(hexColor, { files: [{ attachment: canvas.toBuffer(), name: 'dominant-color.png' }] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "126.2.1",
|
||||
"version": "126.3.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -176,6 +176,11 @@ module.exports = class Util {
|
||||
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
|
||||
}
|
||||
|
||||
static rgbToHex(r, g, b) {
|
||||
if (r > 255 || g > 255 || b > 255) return null;
|
||||
return ((r << 16) | (g << 8) | b).toString(16);
|
||||
}
|
||||
|
||||
static magikToBuffer(magik) {
|
||||
return new Promise((res, rej) => {
|
||||
magik.toBuffer((err, buffer) => {
|
||||
|
||||
Reference in New Issue
Block a user