Metrios/modules/meme_generator/index.js

140 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2021-05-09 18:29:49 +00:00
require("dotenv").config({path: process.env.MODULES_DIR + "/meme_generator/.env"});
const Imgflip = require("imgflip.com").Imgflip;
const imgflip = new Imgflip();
const fs = require("fs");
const shortcutMemes = {
"perhaps": 105577219,
"drake": 181913649
}
let meme = undefined;
let command = process.argv[2].substring(6);
let paramLimiter = command.indexOf(' ') === -1 ? command.length : command.indexOf(' ');
let memeParameter = command.substring(0, paramLimiter);
let paramDivider = command.indexOf('-') === -1 ? command.length : command.indexOf('-');
let text0 = command.substring(paramLimiter, paramDivider);
let text1 = command.substring(paramDivider + 2);
switch(memeParameter) {
case "help":
printHelp();
return;
case "shortcuts":
printShortcuts();
return;
default:
break;
}
if(/^\d+$/.test(memeParameter)) {
meme = memeParameter;
} else {
meme = shortcutMemes[memeParameter];
if(meme === undefined) {
printError();
return;
}
}
function requestMeme() {
return imgflip.api
.captionImage({
username: process.env.IMGFLIP_USER,
password: process.env.IMGFLIP_PASSWORD,
template_id: meme,
text0: text0 || ' ',
text1: text1 || ' '
})
.then(res => res)
.catch(err => console.error(err));
}
function printError() {
console.log("Failed to generate meme");
}
function printShortcuts() {
let fields = [];
for(let shortcut in shortcutMemes) {
fields.push({
name: shortcut,
value: "https://imgflip.com/memegenerator/" + shortcutMemes[shortcut],
inline: true
})
}
console.log(JSON.stringify({
status: "success",
action: "standard",
format: "embed",
profile: {
color: 0x0099f,
title: "Meme shortcuts",
url: undefined,
author: undefined,
description: "Meme shortcuts, linked to respective template id",
thumbnail: undefined,
fields: fields
}
}));
}
function printHelp() {
console.log(JSON.stringify({
status: "success",
action: "standard",
format: "embed",
profile: {
color: 0x0099f,
title: "Meme generator",
url: undefined,
author: undefined,
description: "Generate meme using imgflip API",
thumbnail: undefined,
fields: [
{
name: "`!meme <shortcut> <text 1/optional> <text 2/optional>`",
value: "Generate a meme using the given shortcut and the two provided texts (if provided)",
inline: false,
},
{
name: "`!meme <template id> <text 1/optional> <text 2/optional>`",
value: "Generate a meme using the given template id and the two provided texts (if provided)",
inline: false,
},
{
name: "`!meme shortcuts`",
value: "List available shortcuts",
inline: false,
},
{
name: "`!meme help`",
value: "Display this help menu",
inline: false,
},
]
}
}));
}
function formatOutput(result) {
if(result.success === true) {
console.log(result.data.url);
} else {
printError();
}
}
requestMeme().then(res => formatOutput(res));