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-02-22 23:53:07 +01:00
content : 'Create demotivational poster (use ``|`` to separate top text and bottom text) WIP\nCan make use of --width, --border, --titleSize and --subtextSize to fine tune the image' ,
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 13:01:13 +01:00
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 ) ;
2020-02-21 18:39:05 +01:00
return message . channel . send ( 'An error has occured, is it an image?' ) ;
}
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 occured! is it a static image?' ) ;
}
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 occured! is it a static image?' ) ;
}
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 ;