mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-07 06:45:31 +02:00
New meme API
This commit is contained in:
+22
-15
@@ -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!`);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiaobot",
|
||||
"version": "41.0.0",
|
||||
"version": "41.0.1",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Shard.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user