From 883147550f907391c831533dea809340050e0a4b Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 9 May 2021 20:29:49 +0200 Subject: [PATCH] Add meme generator module --- modules/meme_generator/.env.sample | 2 + modules/meme_generator/index.js | 139 ++++++++++++++++++++++++++++ modules/meme_generator/package.json | 16 ++++ 3 files changed, 157 insertions(+) create mode 100644 modules/meme_generator/.env.sample create mode 100644 modules/meme_generator/index.js create mode 100644 modules/meme_generator/package.json diff --git a/modules/meme_generator/.env.sample b/modules/meme_generator/.env.sample new file mode 100644 index 0000000..40bfb04 --- /dev/null +++ b/modules/meme_generator/.env.sample @@ -0,0 +1,2 @@ +IMGFLIP_USER= +IMGFLIP_PASSWORD= diff --git a/modules/meme_generator/index.js b/modules/meme_generator/index.js new file mode 100644 index 0000000..3e55303 --- /dev/null +++ b/modules/meme_generator/index.js @@ -0,0 +1,139 @@ +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 `", + value: "Generate a meme using the given shortcut and the two provided texts (if provided)", + inline: false, + }, + { + name: "`!meme