Rotate command

This commit is contained in:
Dragon Fire
2021-02-11 10:38:55 -05:00
parent 7e5c750014
commit faadaf8520
4 changed files with 71 additions and 3 deletions
+2 -1
View File
@@ -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.
+67
View File
@@ -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 };
}
};
+1 -1
View File
@@ -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}`);
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "129.2.0",
"version": "129.3.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {