diff --git a/commands/meme-gen/3000-years.js b/commands/meme-gen/3000-years.js index 3b769db0..d5b863dc 100644 --- a/commands/meme-gen/3000-years.js +++ b/commands/meme-gen/3000-years.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 ThreeThousandYearsCommand extends Command { constructor(client) { @@ -25,10 +26,10 @@ module.exports = class ThreeThousandYearsCommand 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 }) } ] }); @@ -43,7 +44,8 @@ module.exports = class ThreeThousandYearsCommand extends Command { const canvas = createCanvas(base.width, base.height); const ctx = canvas.getContext('2d'); ctx.drawImage(base, 0, 0); - ctx.drawImage(avatar, 461, 127, 200, 200); + const { x, y, width, height } = centerImagePart(avatar, 200, 200, 461, 127); + ctx.drawImage(avatar, x, y, width, height); return msg.say({ files: [{ attachment: canvas.toBuffer(), name: '3000-years.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/meme-gen/demotivational.js b/commands/meme-gen/demotivational.js index 6cacb3b6..ea625249 100644 --- a/commands/meme-gen/demotivational.js +++ b/commands/meme-gen/demotivational.js @@ -2,7 +2,7 @@ const Command = require('../../structures/Command'); const { createCanvas, loadImage, registerFont } = require('canvas'); const request = require('node-superfetch'); const path = require('path'); -const { shortenText } = require('../../util/Canvas'); +const { shortenText, centerImagePart } = require('../../util/Canvas'); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Regular.ttf'), { family: 'Noto' }); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' }); registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Emoji.ttf'), { family: 'Noto' }); @@ -60,21 +60,8 @@ module.exports = class DemotivationalCommand extends Command { const ctx = canvas.getContext('2d'); ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); - let { width, height } = data; - const maxWidth = 602; - if (width > maxWidth) { - const ratio = maxWidth / width; - width = maxWidth; - height *= ratio; - } - const maxHeight = 402; - if (height > maxHeight) { - const ratio = maxHeight / height; - height = maxHeight; - width *= ratio; - } + const { y, width, height } = centerImagePart(data, 602, 402, 0, 44); const x = (canvas.width / 2) - (width / 2); - const y = 44 + ((402 / 2) - (height / 2)); ctx.fillStyle = 'white'; ctx.fillRect(x - 4, y - 4, width + 8, height + 8); ctx.fillStyle = 'black'; diff --git a/package.json b/package.json index 792e6078..b49f44c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.12.6", + "version": "112.12.7", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Canvas.js b/util/Canvas.js index 5f110e91..4470ca23 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -146,4 +146,21 @@ module.exports = class CanvasUtil { } return { x, y, width, height }; } + + static centerImagePart(data, maxWidth, maxHeight, widthOffset, heightOffest) { + let { width, height } = data; + if (width > maxWidth) { + const ratio = maxWidth / width; + width = maxWidth; + height *= ratio; + } + if (height > maxHeight) { + const ratio = maxHeight / height; + height = maxHeight; + width *= ratio; + } + const x = widthOffset + ((maxWidth / 2) - (width / 2)); + const y = heightOffest + ((maxHeight / 2) - (height / 2)); + return { x, y, width, height }; + } };