From ef9a25968d5ff3b9f11354185c90e3764f56bf19 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 18 Sep 2017 12:46:19 +0000 Subject: [PATCH] New meme API --- commands/image-edit/meme.js | 37 ++++++++++++++++++++++--------------- package.json | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/commands/image-edit/meme.js b/commands/image-edit/meme.js index 0b547c0f..80232202 100644 --- a/commands/image-edit/meme.js +++ b/commands/image-edit/meme.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const snekfetch = require('snekfetch'); const { list } = require('../../structures/Util'); +const { IMGFLIP_USER, IMGFLIP_PASS } = process.env; module.exports = class MemeCommand extends Command { constructor(client) { @@ -21,23 +22,21 @@ module.exports = class MemeCommand extends Command { key: 'top', prompt: 'What should the top row of the meme to be?', type: 'string', - default: '_', + default: ' ', validate: top => { if (top.length < 200) return true; return 'Invalid top text, please keep the top text under 200 characters.'; - }, - parse: top => encodeURIComponent(top) + } }, { key: 'bottom', prompt: 'What should the bottom row of the meme to be?', type: 'string', - default: '_', + default: ' ', validate: bottom => { if (bottom.length < 200) return true; return 'Invalid bottom text, please keep the bottom text under 200 characters.'; - }, - parse: bottom => encodeURIComponent(bottom) + } } ] }); @@ -45,17 +44,25 @@ module.exports = class MemeCommand extends Command { async run(msg, { type, top, bottom }) { try { - if (type === 'list') { - const { body } = await snekfetch - .get('https://memegen.link/api/templates/'); - const codes = Object.values(body).map(code => code.replace('https://memegen.link/api/templates/', '')); - return msg.say(list(codes)); - } + const memes = await snekfetch + .get('https://api.imgflip.com/get_memes'); + const memeList = memes.body.data.memes.map(meme => ({ + id: meme.id, + name: meme.name.toLowerCase() + })); + if (type === 'list') return msg.say(list(memeList.map(meme => meme.name), 'or'), { split: { char: ' ' } }); + if (!memes.some(meme => meme.name === type)) return msg.say(`Invalid type, please use ${msg.usage('list')}.`); const { body } = await snekfetch - .get(`https://memegen.link/api/templates/${type}/${top}/${bottom}`, { followRedirects: true }); - return msg.say({ files: [body.direct.masked] }); + .post('https://api.imgflip.com/caption_image') + .query({ + template_id: memes.find(meme => meme.name === type).id, + username: IMGFLIP_USER, + password: IMGFLIP_PASS, + text0: top, + text1: bottom + }); + return msg.say({ files: [body.data.url] }); } catch (err) { - if (err.status === 404) return msg.say(`Invalid meme type, please use ${msg.usage('list')} for a list.`); return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/package.json b/package.json index 93260af8..289275f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "41.0.0", + "version": "41.0.1", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {