From 3fb382612ac4b259dc290fc868e268b02dcd7c5a Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 4 Jun 2020 12:37:23 -0400 Subject: [PATCH] Fix Bunny GIFs being too big --- commands/random-img/bunny.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/commands/random-img/bunny.js b/commands/random-img/bunny.js index 7edb14d3..c21dcde8 100644 --- a/commands/random-img/bunny.js +++ b/commands/random-img/bunny.js @@ -25,7 +25,17 @@ module.exports = class BunnyCommand extends Command { const { body } = await request .get('https://api.bunnies.io/v2/loop/random/') .query({ media: 'gif,png' }); - return msg.say({ files: [body.media.gif || body.media.poster] }); + let fileToSend; + let fileType = 'gif'; + const gif = await request.get(body.media.gif); + if (Buffer.byteLength(gif.body) > 8e+6) { + const poster = await request.get(body.media.poster); + fileToSend = poster.body; + fileType = 'png'; + } else { + fileToSend = gif.body; + } + return msg.say({ files: [{ attachment: fileToSend, name: `${body.id}.${fileType}` }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); }