diff --git a/commands/image-edit/distort-test.js b/commands/image-edit/distort-test.js new file mode 100644 index 00000000..4b7108fb --- /dev/null +++ b/commands/image-edit/distort-test.js @@ -0,0 +1,54 @@ +const { Command } = require('discord.js-commando'); +const { createCanvas, loadImage } = require('canvas'); +const snekfetch = require('snekfetch'); +const { distort } = require('../../util/Canvas'); + +module.exports = class DistortTestCommand extends Command { + constructor(client) { + super(client, { + name: 'distort-test', + aliases: ['under-water-test'], + group: 'avatar-edit', + memberName: 'distort-test', + description: 'Draws an image or user\'s avatar but distorted.', + throttling: { + usages: 1, + duration: 15 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'level', + prompt: 'What level of distortion would you like to use?', + type: 'integer' + }, + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: '' + } + ] + }); + } + + async run(msg, { level, image }) { + if (!image) { + image = msg.author.displayAvatarURL({ + format: 'png', + size: 512 + }); + } + try { + const { body } = await snekfetch.get(image); + const imageData = await loadImage(body); + const canvas = createCanvas(imageData.width, imageData.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(imageData, 0, 0); + distort(ctx, level, 0, 0, imageData.width, imageData.height); + return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'distort-test.png' }] }); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/types/emoji.js b/types/emoji.js index d444b6fd..feb9b80e 100644 --- a/types/emoji.js +++ b/types/emoji.js @@ -7,6 +7,7 @@ class EmojiArgumentType extends ArgumentType { } validate(value, msg) { + if (!value) return false; const matches = value.match(/^(?:<:([a-zA-Z0-9_]+):)?([0-9]+)>?$/); if (matches && msg.client.emojis.has(matches[2])) return true; if (!msg.guild) return false; diff --git a/types/image.js b/types/image.js new file mode 100644 index 00000000..95f7881b --- /dev/null +++ b/types/image.js @@ -0,0 +1,30 @@ +const { ArgumentType } = require('discord.js-commando'); + +class ImageArgumentType extends ArgumentType { + constructor(client) { + super(client, 'image'); + } + + async validate(value, msg) { + const attachment = msg.attachments.first(); + if (!attachment) { + const valid = await this.client.registry.types.get('user').validate(value, msg); + return valid; + } + if (!attachment.height || !attachment.width) return false; + if (attachment.size > 8e+6) return false; + return true; + } + + parse(value, msg) { + if (!msg.attachments.size) { + return this.client.registry.types.get('user').parse(value, msg).displayAvatarURL({ + format: 'png', + size: 512 + }); + } + return msg.attachments.first().url; + } +} + +module.exports = ImageArgumentType;