Haha-Yes/commands/images/rotate.js

73 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-10-13 16:54:07 +02:00
const { Command } = require('discord-akairo');
const jimp = require('jimp');
const os = require('os');
class rotateCommand extends Command {
constructor() {
super('rotate', {
aliases: ['rotate'],
category: 'images',
2019-11-09 12:04:01 +01:00
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
2019-10-13 16:54:07 +02:00
args: [
{
id: 'link',
type: 'string',
},
{
id: 'rotate',
type: 'integer',
2020-03-18 13:14:08 +01:00
prompt: {
start: 'Please enter the number of degrees you want to rotate.',
retry: 'This doesn\'t look like a number to me, please try again.'
}
2019-10-13 16:54:07 +02:00
}
],
description: {
2020-03-18 13:14:08 +01:00
content: 'Rotate your image',
2019-10-13 16:54:07 +02:00
usage: '[link to image] [angle of rotation]',
examples: ['']
}
});
}
async exec(message, args) {
let output = `${os.tmpdir()}/rotated${message.id}.jpg`;
let Attachment = (message.attachments).array();
let url = args.link;
// Get attachment link
if (Attachment[0] && !args.link) {
url = Attachment[0].url;
}
if (!url) {
return message.channel.send('You need an image to use this command!');
}
2019-10-13 18:15:56 +02:00
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
2019-10-13 16:54:07 +02:00
jimp.read({
url: url
})
.then(image => {
return image
.rotate(args.rotate)
.write(output);
})
.then(() => {
2019-10-13 18:15:56 +02:00
loadingmsg.delete();
2019-10-13 16:54:07 +02:00
return message.channel.send({files: [output]});
})
.catch(error => {
console.error(error);
return message.channel.send('Oh no, an error just occured! Maybe the format of your image don\'t work?');
2019-10-13 16:54:07 +02:00
});
2019-10-13 16:54:07 +02:00
}
}
module.exports = rotateCommand;