Haha-Yes/commands/images/poster.js

160 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-02-21 01:34:16 +01:00
const { Command } = require('discord-akairo');
2020-02-21 21:28:02 +01:00
const gm = require('gm').subClass({imageMagick: true});
2020-02-21 01:34:16 +01:00
const os = require('os');
const fetch = require('node-fetch');
const fs = require('fs');
class posterCommand extends Command {
constructor() {
super('poster', {
aliases: ['poster'],
category: 'images',
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [
{
id: 'link',
prompt: {
start: 'Please input a link to use, say `cancel` to stop the command'
},
type: 'string',
},
{
id: 'message',
prompt: {
start: 'Please input a caption, say `cancel` to stop the command'
},
type: 'string',
match: 'rest'
2020-02-21 20:00:11 +01:00
},
{
id: 'width',
match: 'option',
flag: '--width',
default: 50
},
{
id: 'height',
match: 'option',
flag: '--height',
default: 200
},
2020-02-21 20:05:48 +01:00
{
id: 'title',
match: 'option',
flag: '--titleSize',
},
{
id: 'subtext',
match: 'option',
flag: '--subtextSize',
},
2020-02-21 01:34:16 +01:00
],
description: {
2020-03-01 16:59:02 +01:00
content: 'Create demotivational poster (use ``|`` to separate top text and bottom text) WIP\nCan make use of --width, --height, --titleSize and --subtextSize to fine tune the image',
2020-02-22 23:53:07 +01:00
usage: '[link to image] [topText|bottomText]',
2020-02-21 20:00:11 +01:00
examples: ['https://cdn.discordapp.com/attachments/484013245158522909/679686216903426104/0nhsxje5vfg41-1.jpg NO PLEASE DON\'T | My mom said im not allowed to']
2020-02-21 01:34:16 +01:00
}
});
}
async exec(message, args) {
let options = args.message.trim().split('|');
if (options[0] == undefined)
options[0] = '';
else if (options[1] == undefined)
options[1] = '';
let url = args.link;
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}`);
2020-02-21 01:34:16 +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}`);
2020-02-21 01:34:16 +01:00
// Set some defaults
const TOP_TEXT = options[0];
const BOTTOM_TEXT = options[1];
const FONT = './asset/times.ttf';
const FONT_FILL = '#FFF';
const TEXT_POS = 'center';
const PADDING = 40;
2020-02-21 18:33:53 +01:00
img.format(function(err, format) {
2020-02-21 18:39:05 +01:00
if (err) {
2020-02-21 21:15:11 +01:00
console.error(err);
return message.channel.send('An error has occurred, is it an image?');
2020-02-21 18:39:05 +01:00
}
2020-02-22 14:38:52 +01:00
let output1 = `${os.tmpdir()}/poster${message.author.id}.${format.toLowerCase()}`;
let output2 = `${os.tmpdir()}/poster${message.id}.${format.toLowerCase()}`;
2020-02-21 18:33:53 +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
const TOP_POS = Math.abs((value.height / 2) - PADDING + 110);
const BOTTOM_POS = Math.abs((value.height / 2) - PADDING + 180);
2020-02-21 21:09:23 +01:00
let FONT_SIZE1 = args.title ? args.title : (value.width / 12);
let FONT_SIZE2 = args.subtext ? args.subtext : (value.width / 12) - 15;
2020-02-21 20:00:11 +01:00
let BORDER_WIDTH = args.width;
let BORDER_HEIGHT = args.height;
2020-02-22 14:38:52 +01:00
if (format.toLowerCase() == 'gif') img.coalesce();
2020-02-21 18:33:53 +01:00
// Write text on image using graphicsmagick
img.borderColor('black')
.border(3,3)
.borderColor('white')
.frame(1,1,0,0.5)
.border(1,1)
.borderColor('black')
2020-02-21 20:00:11 +01:00
.border(BORDER_WIDTH,BORDER_HEIGHT)
2020-02-21 18:33:53 +01:00
.fill(FONT_FILL)
.font(FONT, FONT_SIZE1)
.drawText(0, TOP_POS, TOP_TEXT, TEXT_POS)
.font(FONT, FONT_SIZE2)
.drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS)
2020-02-22 14:38:52 +01:00
.write(output1, function(err) {
if (err) {
console.error(err);
return message.channel.send('An error just occurred! is it a static image?');
2020-02-22 14:38:52 +01:00
}
2020-02-21 18:33:53 +01:00
// Chop the top part of the image
2020-02-22 14:38:52 +01:00
let img2 = gm(output1);
2020-02-21 20:16:21 +01:00
img2.chop(0, BORDER_HEIGHT / 2)
2020-02-22 14:38:52 +01:00
.write(output2, function(err) {
2020-02-21 18:33:53 +01:00
loadingmsg.delete();
if (err) {
console.error(err);
return message.channel.send('An error just occurred! is it a static image?');
2020-02-21 18:33:53 +01:00
}
2020-02-21 18:59:18 +01:00
message.delete();
2020-02-22 14:38:52 +01:00
return message.channel.send(`Made by ${message.author.username}`,{files: [output2]})
2020-02-21 18:36:24 +01:00
.catch(() => {
return message.channel.send('The image is too big to fit on discord!');
});
2020-02-21 18:33:53 +01:00
});
});
});
2020-02-21 01:34:16 +01:00
});
2020-02-21 18:33:53 +01:00
2020-02-21 01:34:16 +01:00
});
})
.catch((err) => {
console.error(err);
return message.channel.send(`Please input a correct link \`${err}\``);
});
}
}
module.exports = posterCommand;