Image editing for commands that support

it
This commit is contained in:
Daniel Odendahl Jr
2017-11-19 21:20:02 +00:00
parent 86e48997a8
commit ad45cedd0e
10 changed files with 129 additions and 174 deletions
-53
View File
@@ -1,53 +0,0 @@
const { Command } = require('discord.js-commando');
const { createCanvas, loadImage } = require('canvas');
const snekfetch = require('snekfetch');
const { distort } = require('../../util/Canvas');
module.exports = class DistortCommand extends Command {
constructor(client) {
super(client, {
name: 'distort',
aliases: ['under-water'],
group: 'avatar-edit',
memberName: 'distort',
description: 'Draws a 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: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
default: ''
}
]
});
}
async run(msg, { level, user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
distort(ctx, level, 0, 0, avatar.width, avatar.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'distort.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
@@ -7,9 +7,9 @@ module.exports = class ContrastCommand extends Command {
constructor(client) {
super(client, {
name: 'contrast',
group: 'avatar-edit',
group: 'image-edit',
memberName: 'contrast',
description: 'Draws a user\'s avatar but with contrast.',
description: 'Draws an image or a user\'s avatar but with contrast.',
throttling: {
usages: 1,
duration: 15
@@ -17,8 +17,8 @@ module.exports = class ContrastCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'user',
default: ''
}
@@ -26,19 +26,20 @@ module.exports = class ContrastCommand extends Command {
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
contrast(ctx, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
contrast(ctx, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'contrast.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -3,14 +3,14 @@ const { createCanvas, loadImage } = require('canvas');
const snekfetch = require('snekfetch');
const { distort } = require('../../util/Canvas');
module.exports = class DistortTestCommand extends Command {
module.exports = class DistortCommand 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.',
name: 'distort',
aliases: ['under-water'],
group: 'image-edit',
memberName: 'distort',
description: 'Draws an image or a user\'s avatar but distorted.',
throttling: {
usages: 1,
duration: 15
@@ -41,12 +41,12 @@ module.exports = class DistortTestCommand extends Command {
}
try {
const { body } = await snekfetch.get(image);
const imageData = await loadImage(body);
const canvas = createCanvas(imageData.width, imageData.height);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.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' }] });
ctx.drawImage(data, 0, 0);
distort(ctx, level, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'distort.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
@@ -7,9 +7,9 @@ module.exports = class GlitchCommand extends Command {
constructor(client) {
super(client, {
name: 'glitch',
group: 'avatar-edit',
group: 'image-edit',
memberName: 'glitch',
description: 'Draws a user\'s avatar but glitched.',
description: 'Draws an image or a user\'s avatar but glitched.',
throttling: {
usages: 1,
duration: 15
@@ -17,29 +17,30 @@ module.exports = class GlitchCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'glitch.png'));
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
ctx.drawImage(base, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
ctx.drawImage(base, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'glitch.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -8,9 +8,9 @@ module.exports = class GreyscaleCommand extends Command {
super(client, {
name: 'greyscale',
aliases: ['grayscale'],
group: 'avatar-edit',
group: 'image-edit',
memberName: 'greyscale',
description: 'Draws a user\'s avatar in greyscale.',
description: 'Draws an image or a user\'s avatar in greyscale.',
throttling: {
usages: 1,
duration: 15
@@ -18,28 +18,29 @@ module.exports = class GreyscaleCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
greyscale(ctx, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
greyscale(ctx, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'greyscale.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -7,9 +7,9 @@ module.exports = class InvertCommand extends Command {
constructor(client) {
super(client, {
name: 'invert',
group: 'avatar-edit',
group: 'image-edit',
memberName: 'invert',
description: 'Draws a user\'s avatar but inverted.',
description: 'Draws an image or a user\'s avatar but inverted.',
throttling: {
usages: 1,
duration: 15
@@ -17,28 +17,29 @@ module.exports = class InvertCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
invert(ctx, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
invert(ctx, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'invert.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -6,9 +6,10 @@ module.exports = class PixelizeCommand extends Command {
constructor(client) {
super(client, {
name: 'pixelize',
group: 'avatar-edit',
aliases: ['jpeg', 'needs-more-jpeg'],
group: 'image-edit',
memberName: 'pixelize',
description: 'Draws a user\'s avatar but pixelized.',
description: 'Draws an image or a user\'s avatar but pixelized.',
throttling: {
usages: 1,
duration: 15
@@ -16,28 +17,29 @@ module.exports = class PixelizeCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 64
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 64
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(512, 512);
const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
ctx.drawImage(avatar, 0, 0, 512, 512);
ctx.drawImage(data, 0, 0, 512, 512);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'pixelize.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -7,9 +7,9 @@ module.exports = class SepiaCommand extends Command {
constructor(client) {
super(client, {
name: 'sepia',
group: 'avatar-edit',
group: 'image-edit',
memberName: 'sepia',
description: 'Draws a user\'s avatar in sepia.',
description: 'Draws an image or a user\'s avatar in sepia.',
throttling: {
usages: 1,
duration: 15
@@ -17,28 +17,29 @@ module.exports = class SepiaCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
sepia(ctx, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
sepia(ctx, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'sepia.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
@@ -7,9 +7,9 @@ module.exports = class SilhouetteCommand extends Command {
constructor(client) {
super(client, {
name: 'silhouette',
group: 'avatar-edit',
group: 'image-edit',
memberName: 'silhouette',
description: 'Draws a silhouette of a user\'s avatar.',
description: 'Draws a silhouette of an image or a user\'s avatar.',
throttling: {
usages: 1,
duration: 15
@@ -17,28 +17,29 @@ module.exports = class SilhouetteCommand extends Command {
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: ''
}
]
});
}
async run(msg, { user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
async run(msg, { image }) {
if (!image) {
image = msg.author.displayAvatarURL({
format: 'png',
size: 512
});
}
try {
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const { body } = await snekfetch.get(image);
const data = await loadImage(body);
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
silhouette(ctx, 0, 0, avatar.width, avatar.height);
ctx.drawImage(data, 0, 0);
silhouette(ctx, 0, 0, data.width, data.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'silhouette.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "55.1.0",
"version": "55.1.1",
"description": "Your personal server companion.",
"main": "XiaoBot.js",
"scripts": {