mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-17 16:19:12 +02:00
Begin migrating away from stupid "Oh no, an error occurred!"
This commit is contained in:
@@ -41,32 +41,28 @@ module.exports = class ReactionMemeCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg, { text, image }) {
|
||||
try {
|
||||
const { body } = await request.get(image);
|
||||
const base = await loadImage(body);
|
||||
const canvas = createCanvas(base.width, base.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(40);
|
||||
const lines = await wrapText(ctx, text, base.width - 10);
|
||||
const lineBreakLen = text.split('\n').length;
|
||||
const linesLen = (40 * lines.length)
|
||||
+ (40 * (lineBreakLen - 1))
|
||||
+ (14 * lines.length)
|
||||
+ (14 * (lineBreakLen - 1))
|
||||
+ 14;
|
||||
canvas.height += linesLen;
|
||||
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(40);
|
||||
ctx.textBaseline = 'top';
|
||||
ctx.fillStyle = 'white';
|
||||
ctx.fillRect(0, 0, base.width, linesLen);
|
||||
ctx.fillStyle = 'black';
|
||||
ctx.fillText(lines.join('\n'), 5, 5);
|
||||
ctx.drawImage(base, 0, linesLen);
|
||||
const attachment = canvas.toBuffer();
|
||||
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
|
||||
return msg.say({ files: [{ attachment, name: 'meme-gen-modern.png' }] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
const { body } = await request.get(image);
|
||||
const base = await loadImage(body);
|
||||
const canvas = createCanvas(base.width, base.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(40);
|
||||
const lines = await wrapText(ctx, text, base.width - 10);
|
||||
const lineBreakLen = text.split('\n').length;
|
||||
const linesLen = (40 * lines.length)
|
||||
+ (40 * (lineBreakLen - 1))
|
||||
+ (14 * lines.length)
|
||||
+ (14 * (lineBreakLen - 1))
|
||||
+ 14;
|
||||
canvas.height += linesLen;
|
||||
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(40);
|
||||
ctx.textBaseline = 'top';
|
||||
ctx.fillStyle = 'white';
|
||||
ctx.fillRect(0, 0, base.width, linesLen);
|
||||
ctx.fillStyle = 'black';
|
||||
ctx.fillText(lines.join('\n'), 5, 5);
|
||||
ctx.drawImage(base, 0, linesLen);
|
||||
const attachment = canvas.toBuffer();
|
||||
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
|
||||
return msg.say({ files: [{ attachment, name: 'meme-gen-modern.png' }] });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user