This commit is contained in:
Daniel Odendahl Jr
2017-11-19 19:59:00 +00:00
parent c9df6d9a59
commit 36c17d8988
3 changed files with 85 additions and 0 deletions
+54
View File
@@ -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!`);
}
}
};
+1
View File
@@ -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;
+30
View File
@@ -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;