Begin migrating away from stupid "Oh no, an error occurred!"

This commit is contained in:
Dragon Fire
2024-03-29 23:57:49 -04:00
parent 067545b818
commit 0b807767d1
130 changed files with 1692 additions and 2190 deletions
+23 -27
View File
@@ -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' }] });
}
};