Convert Image Command

This commit is contained in:
Dragon Fire
2020-06-07 17:37:45 -04:00
parent 99e37993e9
commit e2ec916e1d
3 changed files with 58 additions and 3 deletions
+2 -1
View File
@@ -224,7 +224,7 @@ in the appropriate channel's topic to use it.
## Commands
Total: 457
Total: 458
### Utility:
@@ -541,6 +541,7 @@ Total: 457
* **circle:** Draws an image or a user's avatar as a circle.
* **color:** Sends an image of the color you choose.
* **contrast:** Draws an image or a user's avatar but with contrast.
* **convert-image:** Converts an image or user's avatar from one format to another.
* **create-qr-code:** Converts text to a QR Code.
* **dexter:** Draws an image or a user's avatar over the screen of Dexter from Pokémon.
* **distort:** Draws an image or a user's avatar but distorted.
+54
View File
@@ -0,0 +1,54 @@
const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const { list } = require('../../util/Util');
const formats = {
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png'
};
module.exports = class ConvertImageCommand extends Command {
constructor(client) {
super(client, {
name: 'convert-image',
aliases: ['convert-img', 'image-convert', 'img-convert'],
group: 'edit-image',
memberName: 'convert-image',
description: 'Converts an image or user\'s avatar from one format to another.',
throttling: {
usages: 1,
duration: 10
},
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'format',
prompt: `What format do you want to convert the image to? Either ${list(Object.keys(formats), 'or')}.`,
type: 'string',
oneOf: Object.keys(formats),
parse: format => format.toLowerCase()
},
{
key: 'image',
prompt: 'Which image would you like to edit?',
type: 'image',
default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 })
}
]
});
}
async run(msg, { format, image }) {
try {
const { body } = await request.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(data, 0, 0);
return msg.say({ files: [{ attachment: canvas.toBuffer(formats[format]), name: `convert-image.${format}` }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "116.2.5",
"version": "116.3.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
@@ -60,7 +60,7 @@
"utf-8-validate": "^5.0.2"
},
"devDependencies": {
"eslint": "^7.0.0",
"eslint": "^7.2.0",
"eslint-config-amber": "^2.0.3",
"eslint-plugin-json": "^2.1.1"
},