From e2ec916e1d62b8e39bf5385ab58631ae6e394cd1 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 7 Jun 2020 17:37:45 -0400 Subject: [PATCH] Convert Image Command --- README.md | 3 +- commands/edit-image/convert-image.js | 54 ++++++++++++++++++++++++++++ package.json | 4 +-- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 commands/edit-image/convert-image.js diff --git a/README.md b/README.md index c739371f..ec48b19b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/commands/edit-image/convert-image.js b/commands/edit-image/convert-image.js new file mode 100644 index 00000000..d3dd63ea --- /dev/null +++ b/commands/edit-image/convert-image.js @@ -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!`); + } + } +}; diff --git a/package.json b/package.json index 0a33801a..c8bf62fa 100644 --- a/package.json +++ b/package.json @@ -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" },