diff --git a/README.md b/README.md index 92a5805b..d2906a78 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 595 +Total: 596 ### Utility: @@ -704,6 +704,7 @@ Total: 595 * **rejected:** Draws a "rejected" stamp over an image or a user's avatar. * **resize:** Draws an image or a user's avatar resized to the size you want. * **robohash:** Creates a robot based on the text you provide. +* **rotate:** Draws an image or a user's avatar but rotated a number of degrees. * **sepia:** Draws an image or a user's avatar in sepia. * **shields-io-badge:** Creates a badge from shields.io. * **silhouette:** Draws a silhouette of an image or a user's avatar. diff --git a/commands/edit-image/rotate.js b/commands/edit-image/rotate.js new file mode 100644 index 00000000..06284069 --- /dev/null +++ b/commands/edit-image/rotate.js @@ -0,0 +1,67 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const { centerImage } = require('../../util/Canvas'); + +module.exports = class RotateCommand extends Command { + constructor(client) { + super(client, { + name: 'rotate', + group: 'edit-image', + memberName: 'rotate', + description: 'Draws an image or a user\'s avatar but rotated a number of degrees.', + throttling: { + usages: 1, + duration: 10 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'degrees', + prompt: 'How many degrees do you want to rotate the image?', + type: 'integer', + min: -360, + max: 360 + }, + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image-or-avatar', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 }) + } + ] + }); + } + + async run(msg, { degrees, image }) { + try { + const { body } = await request.get(image); + const data = await loadImage(body); + const newDims = this.adjustCanvasSize(data.width, data.height, degrees); + const canvas = createCanvas(newDims.width, newDims.height); + const ctx = canvas.getContext('2d'); + ctx.translate(canvas.width / 2, canvas.height / 2); + ctx.rotate(degrees * (Math.PI / 180)); + ctx.translate(-(canvas.width / 2), -(canvas.height / 2)); + const { x, y, width, height } = centerImage(canvas, data); + ctx.drawImage(data, x, y, width, height); + ctx.translate(canvas.width / 2, canvas.height / 2); + ctx.rotate(-degrees * (Math.PI / 180)); + const attachment = canvas.toBuffer(); + if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); + return msg.say({ files: [{ attachment, name: 'rotate.png' }] }); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + adjustCanvasSize(width, height, angle) { + let cos = Math.cos(angle * (Math.PI / 180)); + let sin = Math.sin(angle * (Math.PI / 180)); + if (sin < 0) sin = -sin; + if (cos < 0) cos = -cos; + const newWidth = (height * sin) + (width * cos); + const newHeight = (height * cos) + (width * sin); + return { width: newWidth, height: newHeight }; + } +}; diff --git a/commands/other/screenshot.js b/commands/other/screenshot.js index 0598b9de..29f09c6b 100644 --- a/commands/other/screenshot.js +++ b/commands/other/screenshot.js @@ -41,7 +41,7 @@ module.exports = class ScreenshotCommand extends Command { try { if (!this.pornList) await this.fetchPornList(); const parsed = url.parse(site); - if (this.pornList.some(pornURL => parsed.host === pornURL) && !msg.channel.nsfw) { + if (!msg.channel.nsfw && this.pornList.some(pornURL => parsed.host === pornURL)) { return msg.reply('This site is NSFW.'); } const { body } = await request.get(`https://image.thum.io/get/width/1920/crop/675/noanimate/${site}`); diff --git a/package.json b/package.json index bdf143a1..22f25fd5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "129.2.0", + "version": "129.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {