Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit afc7b592a4c2c079556b42b4cbea29d28acdd5c4
parent c5a57e07679d47f7cf0e592db87ee16c62124a77
Author: Bruno <b-coimbra@hotmail.com>
Date:   Fri, 20 Mar 2020 21:59:44 -0300

Merge remote-tracking branch 'upstream/master'

Diffstat:
Mlib/commands/ship.ts | 67++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 36 insertions(+), 31 deletions(-)

diff --git a/lib/commands/ship.ts b/lib/commands/ship.ts @@ -1,9 +1,9 @@ import { FORMATS } from '../extensions'; -import { Message, RichEmbed } from 'discord.js'; +import { Message, Attachment, RichEmbed } from 'discord.js'; import Jimp from 'jimp'; -const TEMPLATE = "../resources/ship-template.png"; +const TEMPLATE = "./resources/ship-template.png"; const RESPONSES = [ { range: [0, 9], message: "Fate has decided you two aren't made for each other. :fearful:" }, @@ -76,38 +76,43 @@ export default home_scope => { const die = Math.floor(Math.random() * 100); const response = `${get_percentage(die)} ${get_response(die)}`; - const compose_images = ({ first, second }) => { + const error_msg = e => + message.answer("Unable to calculate the love grade :(" + + `:\n${e.message}`.format('```')); + + const compose_images = async ({ first, second }) => { const padding = 4; const size = 128; const filename = "ship-result.png"; - Jimp.read(TEMPLATE).then(template => { - Jimp.read(first).then(fst => { - return template.blit(fst.resize(Jimp.AUTO, size), 0, 0); - }).then(composed => { - Jimp.read(second).then(snd => { - return composed.blit(snd.resize(Jimp.AUTO, size), - snd.bitmap.width * 2 + padding, 0); - }).then(image => { - const embed = new RichEmbed() - .setColor('#b943e8') - .setTitle(`Love grade between` + - `${message.mentions.users.first().username} & ` + - `${message.mentions.users.last().username}`) - .setDescription(response) - .attachFiles([{ - attachment: image.bitmap.data, - name: filename - }]) - .setImage(`attachment://${filename}`); - - message.channel.send(embed); - }) - }); - }).catch(() => { - message.answer("Unable to calculate the love grade :("); - }) - } + const template = await Jimp.read(TEMPLATE); + const fst = await Jimp.read(first); + const composed = template.blit(fst.resize(Jimp.AUTO, size), 0, 0); + const snd = await Jimp.read(second); + + const image = await composed.blit( + snd.resize(Jimp.AUTO, size), + snd.bitmap.width * 2 + padding, 0); + + image.getBuffer('image/png', (e : Error, buffer : Buffer) => { + if (e && e.message) + return error_msg(e); + + const attachment = new Attachment(buffer, filename); + const embed = new RichEmbed() + .setColor('#b943e8') + .setTitle(`Love grade between ` + + `${message.mentions.users.first().username} & ` + + `${message.mentions.users.last().username}`) + .setDescription(response) + .attachFile(attachment) + .setImage(`attachment://${filename}`); + + message.channel.send(embed); + }); + }; - compose_images(user_avatars); + try { + compose_images(user_avatars); + } catch (e) { error_msg(e); } };