From 39c04281642e597be1228697a6b802d44a795da8 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 23 Mar 2020 00:00:31 -0400 Subject: [PATCH] Allow many more memes to support all images --- .../{avatar-edit => image-edit}/dexter.js | 21 +++++++++--------- .../{avatar-edit => meme-gen}/challenger.js | 22 +++++++++---------- commands/meme-gen/girl-worth-fighting-for.js | 19 ++++++++-------- .../look-at-this-photograph.js | 17 +++++++------- commands/meme-gen/ultimate-tattoo.js | 19 ++++++++-------- commands/meme-gen/worthless.js | 22 ++++++++++--------- 6 files changed, 62 insertions(+), 58 deletions(-) rename commands/{avatar-edit => image-edit}/dexter.js (67%) rename commands/{avatar-edit => meme-gen}/challenger.js (76%) rename commands/{avatar-edit => meme-gen}/look-at-this-photograph.js (78%) diff --git a/commands/avatar-edit/dexter.js b/commands/image-edit/dexter.js similarity index 67% rename from commands/avatar-edit/dexter.js rename to commands/image-edit/dexter.js index 0da5c97b..39fdd732 100644 --- a/commands/avatar-edit/dexter.js +++ b/commands/image-edit/dexter.js @@ -2,14 +2,15 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); +const { centerImagePart } = require('../../util/Canvas'); module.exports = class DexterCommand extends Command { constructor(client) { super(client, { name: 'dexter', - group: 'avatar-edit', + group: 'image-edit', memberName: 'dexter', - description: 'Draws a user\'s avatar over the screen of Dexter from Pokémon.', + description: 'Draws an image or a user\'s avatar over the screen of Dexter from Pokémon.', throttling: { usages: 1, duration: 10 @@ -24,26 +25,26 @@ module.exports = class DexterCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 256 }) } ] }); } - async run(msg, { user }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 256 }); + async run(msg, { image }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'dexter.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); ctx.rotate(-11 * (Math.PI / 180)); - ctx.drawImage(avatar, 234, 274, 225, 225); + const { x, y, width, height } = centerImagePart(avatar, 225, 225, 234, 274); + ctx.drawImage(avatar, x, y, width, height); ctx.rotate(11 * (Math.PI / 180)); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'dexter.png' }] }); } catch (err) { diff --git a/commands/avatar-edit/challenger.js b/commands/meme-gen/challenger.js similarity index 76% rename from commands/avatar-edit/challenger.js rename to commands/meme-gen/challenger.js index df2397cd..830cdf31 100644 --- a/commands/avatar-edit/challenger.js +++ b/commands/meme-gen/challenger.js @@ -2,16 +2,16 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); -const { silhouette } = require('../../util/Canvas'); +const { silhouette, centerImagePart } = require('../../util/Canvas'); module.exports = class ChallengerCommand extends Command { constructor(client) { super(client, { name: 'challenger', aliases: ['challenger-approaching'], - group: 'avatar-edit', + group: 'meme-gen', memberName: 'challenger', - description: 'Draws a user\'s avatar over Super Smash Bros.\'s "Challenger Approaching" screen.', + description: 'Draws an image or a user\'s avatar over Smash Bros.\'s "Challenger Approaching" screen.', throttling: { usages: 1, duration: 10 @@ -33,10 +33,10 @@ module.exports = class ChallengerCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 256 }) }, { key: 'silhouetted', @@ -48,16 +48,16 @@ module.exports = class ChallengerCommand extends Command { }); } - async run(msg, { user, silhouetted }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 256 }); + async run(msg, { image, silhouetted }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'challenger.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); - ctx.drawImage(silhouetted ? this.silhouetteImage(avatar) : avatar, 484, 98, 256, 256); + const { x, y, width, height } = centerImagePart(avatar, 256, 256, 484, 98); + ctx.drawImage(silhouetted ? this.silhouetteImage(avatar) : avatar, x, y, width, height); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'challenger.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/meme-gen/girl-worth-fighting-for.js b/commands/meme-gen/girl-worth-fighting-for.js index b8d84210..d01a4a29 100644 --- a/commands/meme-gen/girl-worth-fighting-for.js +++ b/commands/meme-gen/girl-worth-fighting-for.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); +const { centerImagePart } = require('../../util/Canvas'); module.exports = class GirlWorthFightingForCommand extends Command { constructor(client) { @@ -10,7 +11,7 @@ module.exports = class GirlWorthFightingForCommand extends Command { aliases: ['a-girl-worth-fighting-for', 'ling'], group: 'meme-gen', memberName: 'girl-worth-fighting-for', - description: 'Draws a user\'s avatar as the object of Ling\'s affection.', + description: 'Draws an image or a user\'s avatar as the object of Ling\'s affection.', throttling: { usages: 1, duration: 10 @@ -32,25 +33,25 @@ module.exports = class GirlWorthFightingForCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 256 }) } ] }); } - async run(msg, { user }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 256 }); + async run(msg, { image }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'girl-worth-fighting-for.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); - ctx.drawImage(avatar, 380, 511, 150, 150); + const { x, y, width, height } = centerImagePart(avatar, 150, 150, 380, 511); + ctx.drawImage(avatar, x, y, width, height); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'girl-worth-fighting-for.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/avatar-edit/look-at-this-photograph.js b/commands/meme-gen/look-at-this-photograph.js similarity index 78% rename from commands/avatar-edit/look-at-this-photograph.js rename to commands/meme-gen/look-at-this-photograph.js index e352a9a5..6c87fc3c 100644 --- a/commands/avatar-edit/look-at-this-photograph.js +++ b/commands/meme-gen/look-at-this-photograph.js @@ -8,9 +8,9 @@ module.exports = class LookAtThisPhotographCommand extends Command { super(client, { name: 'look-at-this-photograph', aliases: ['nickelback', 'look-at-this-photo', 'photograph'], - group: 'avatar-edit', + group: 'meme-gen', memberName: 'look-at-this-photograph', - description: 'Draws a user\'s avatar over Nickelback\'s photograph.', + description: 'Draws an image or a user\'s avatar over Nickelback\'s photograph.', throttling: { usages: 1, duration: 10 @@ -26,20 +26,19 @@ module.exports = class LookAtThisPhotographCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 256 }) } ] }); } - async run(msg, { user }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 256 }); + async run(msg, { image }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'look-at-this-photograph.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); diff --git a/commands/meme-gen/ultimate-tattoo.js b/commands/meme-gen/ultimate-tattoo.js index 93ec5b0e..db77fa0a 100644 --- a/commands/meme-gen/ultimate-tattoo.js +++ b/commands/meme-gen/ultimate-tattoo.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); +const { centerImagePart } = require('../../util/Canvas'); module.exports = class UltimateTattooCommand extends Command { constructor(client) { @@ -10,7 +11,7 @@ module.exports = class UltimateTattooCommand extends Command { aliases: ['the-ultimate-tattoo', 'tattoo'], group: 'meme-gen', memberName: 'ultimate-tattoo', - description: 'Draws a user\'s avatar as "The Ultimate Tattoo".', + description: 'Draws an image or a user\'s avatar as "The Ultimate Tattoo".', throttling: { usages: 1, duration: 10 @@ -31,26 +32,26 @@ module.exports = class UltimateTattooCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 256 }) } ] }); } - async run(msg, { user }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 256 }); + async run(msg, { image }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'ultimate-tattoo.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); ctx.rotate(-10 * (Math.PI / 180)); - ctx.drawImage(avatar, 84, 690, 300, 300); + const { x, y, width, height } = centerImagePart(avatar, 300, 300, 84, 690); + ctx.drawImage(avatar, x, y, width, height); ctx.rotate(10 * (Math.PI / 180)); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'ultimate-tattoo.png' }] }); } catch (err) { diff --git a/commands/meme-gen/worthless.js b/commands/meme-gen/worthless.js index daf9b06c..081e5cdf 100644 --- a/commands/meme-gen/worthless.js +++ b/commands/meme-gen/worthless.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); +const { centerImagePart } = require('../../util/Canvas'); module.exports = class WorthlessCommand extends Command { constructor(client) { @@ -10,7 +11,7 @@ module.exports = class WorthlessCommand extends Command { aliases: ['this-is-worthless'], group: 'meme-gen', memberName: 'worthless', - description: 'Draws a user\'s avatar over Gravity Falls\' "Oh, this? This is worthless." meme.', + description: 'Draws an image or a user\'s avatar over Gravity Falls\' "This is worthless." meme.', throttling: { usages: 1, duration: 10 @@ -26,31 +27,32 @@ module.exports = class WorthlessCommand extends Command { ], args: [ { - key: 'user', - prompt: 'Which user would you like to edit the avatar of?', - type: 'user', - default: msg => msg.author + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 }) } ] }); } - async run(msg, { user }) { - const avatarURL = user.displayAvatarURL({ format: 'png', size: 512 }); + async run(msg, { image }) { try { const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'worthless.png')); - const { body } = await request.get(avatarURL); + const { body } = await request.get(image); const avatar = await loadImage(body); const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); ctx.rotate(6 * (Math.PI / 180)); - ctx.drawImage(avatar, 496, 183, 400, 400); + const center1 = centerImagePart(avatar, 400, 400, 496, 183); + ctx.drawImage(avatar, center1.x, center1.y, center1.width, center1.height); ctx.rotate(-6 * (Math.PI / 180)); ctx.translate(canvas.width / 2, canvas.height / 2); ctx.rotate(160 * (Math.PI / 180)); ctx.translate(-(canvas.width / 2), -(canvas.height / 2)); - ctx.drawImage(avatar, 625, 55, 75, 75); + const center2 = centerImagePart(avatar, 75, 75, 625, 55); + ctx.drawImage(avatar, center2.x, center2.y, center2.width, center2.height); ctx.rotate(-160 * (Math.PI / 180)); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'worthless.png' }] }); } catch (err) {