Haha-Yes/commands/images/meme.js

122 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-11-01 14:34:46 +01:00
const { Command } = require('discord-akairo');
2020-07-16 09:22:17 +02:00
const attachment = require('../../utils/attachment');
2020-02-21 21:28:02 +01:00
const gm = require('gm').subClass({imageMagick: true});
2019-11-01 14:34:46 +01:00
const os = require('os');
const fetch = require('node-fetch');
const fs = require('fs');
class memeCommand extends Command {
constructor() {
super('meme', {
aliases: ['meme', 'impact'],
category: 'images',
2019-11-09 12:04:01 +01:00
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
2019-11-01 14:34:46 +01:00
args: [
{
id: 'link',
2020-07-16 09:22:17 +02:00
type: 'url',
2019-11-01 14:34:46 +01:00
},
{
id: 'message',
prompt: {
start: 'Please input a caption, say `cancel` to stop the command'
},
2019-11-01 14:34:46 +01:00
type: 'string',
match: 'rest'
2020-02-21 21:07:54 +01:00
},
{
id: 'fontSize',
match: 'option',
flag: '--fontSize',
2019-11-01 14:34:46 +01:00
}
],
description: {
content: 'Impact font on image (use ``|`` to separate top text and bottom text)',
usage: '[link to image] [topText|bottomText]',
examples: ['']
}
});
}
async exec(message, args) {
let options = args.message.trim().split('|');
2020-07-16 09:22:17 +02:00
let url;
if (args.link)
url = args.link.href;
else
url = await attachment(message);
2019-11-01 14:34:46 +01:00
if (options[0] == undefined)
options[0] = '';
else if (options[1] == undefined)
options[1] = '';
if (!url) {
return message.channel.send('You need an image to use this command!');
}
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
// Create new graphicsmagick instance
fetch(url)
.then(res => {
2020-02-21 21:28:02 +01:00
const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}`);
2019-11-01 14:34:46 +01:00
res.body.pipe(dest);
dest.on('finish', async () => {
2020-02-21 21:28:02 +01:00
let img = gm(`${os.tmpdir()}/${message.id}`);
2019-11-01 14:34:46 +01:00
// Set some defaults
const TOP_TEXT = options[0];
const BOTTOM_TEXT = options[1];
const FONT = './asset/impact.ttf';
const FONT_FILL = '#FFF';
const TEXT_POS = 'center';
const STROKE_COLOR = '#000';
const PADDING = 40;
2020-02-21 21:13:03 +01:00
img.format(function(err, format) {
if (err) {
2020-02-21 21:15:11 +01:00
console.error(err);
2020-02-21 21:13:03 +01:00
return message.channel.send('An error has occured, is it an image?');
}
2020-02-22 14:37:44 +01:00
let output = `${os.tmpdir()}/meme${message.id}.${format.toLowerCase()}`;
2020-02-22 14:38:52 +01:00
if (format.toLowerCase() == 'gif') img.coalesce();
2020-02-21 21:13:03 +01:00
// Get the image size to calculate top and bottom text positions
img.size(function(err, value) {
// Set text position for top and bottom
2020-02-22 01:59:29 +01:00
const TOP_POS = Math.abs((value.height / 2) - PADDING) * -0.9;
const BOTTOM_POS = ((value.height / 2) - PADDING * 3);
2020-02-21 21:13:03 +01:00
let FONT_SIZE = args.fontSize ? args.fontSize : (value.width / 10);
// Write text on image using graphicsmagick
img.font(FONT, FONT_SIZE)
.fill(FONT_FILL)
.stroke(STROKE_COLOR)
2020-02-21 21:13:03 +01:00
.drawText(0, TOP_POS, TOP_TEXT, TEXT_POS)
.drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS)
.write(output, function(err) {
loadingmsg.delete();
if (err) return message.channel.send('An error just occured! is it a static image?');
message.delete();
return message.channel.send(`Made by ${message.author.username}`,{files: [output]})
.catch(() => {
return message.channel.send('The image is too big to fit on discord!');
});
});
});
2019-11-01 14:34:46 +01:00
});
2020-02-21 21:13:03 +01:00
2019-11-01 14:34:46 +01:00
});
})
.catch((err) => {
return message.channel.send(`Please input a correct link \`${err}\``);
2019-11-01 14:34:46 +01:00
});
}
}
module.exports = memeCommand;