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
+84 -88
View File
@@ -55,98 +55,94 @@ module.exports = class TweetCommand extends Command {
}
async run(msg, { user, text }) {
try {
if (!this.guestClient) this.guestClient = await api.getGuestClient();
const userData = await this.fetchUser(msg, user);
const avatar = await loadImage(userData.avatar);
const base1 = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'bg-1.png'));
const base2 = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'bg-2.png'));
const canvas = createCanvas(base1.width, base1.height + base2.height);
const ctx = canvas.getContext('2d');
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(23);
const lines = await wrapText(ctx, text, 710);
const lineBreakLen = text.split('\n').length;
const linesLen = (23 * lines.length)
if (!this.guestClient) this.guestClient = await api.getGuestClient();
const userData = await this.fetchUser(msg, user);
const avatar = await loadImage(userData.avatar);
const base1 = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'bg-1.png'));
const base2 = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'bg-2.png'));
const canvas = createCanvas(base1.width, base1.height + base2.height);
const ctx = canvas.getContext('2d');
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(23);
const lines = await wrapText(ctx, text, 710);
const lineBreakLen = text.split('\n').length;
const linesLen = (23 * lines.length)
+ (23 * (lineBreakLen - 1))
+ (9 * (lines.length - 1))
+ (9 * (lineBreakLen - 1));
canvas.height += linesLen;
const likes = randomRange(Math.ceil(userData.followers * 0.0015), Math.ceil(userData.followers * 0.002));
const retweets = randomRange(Math.ceil(userData.followers * 0.00015), Math.ceil(userData.followers * 0.0002));
const quotTweets = randomRange(Math.ceil(userData.followers * 0.000015), Math.ceil(userData.followers * 0.00002));
const replies = randomRange(Math.ceil(userData.followers * 0.000015), Math.ceil(userData.followers * 0.00002));
ctx.fillStyle = '#15202b';
ctx.fillRect(0, base1.height, canvas.width, linesLen);
ctx.drawImage(base1, 0, 0);
const base2StartY = base1.height + linesLen;
ctx.drawImage(base2, 0, base2StartY);
ctx.textBaseline = 'top';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillStyle = 'white';
ctx.fillText(userData.name, 105, 84);
if (userData.verified) {
const verified = await loadImage(
path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'verified.png')
);
const nameLen = ctx.measureText(userData.name).width;
ctx.drawImage(verified, 105 + nameLen + 4, 88, 18, 18);
}
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(17);
ctx.fillStyle = '#8899a6';
ctx.fillText(`@${userData.screenName}`, 106, 111);
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(23);
ctx.fillText(lines.join('\n'), 32, 164);
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
const time = moment().format('h:mm A ∙ MMMM D, YYYY ∙');
ctx.fillText(time, 31, base2StartY + 16);
const timeLen = ctx.measureText(time).width;
ctx.fillStyle = '#1b95e0';
ctx.fillText('Twitter for Xiao', 31 + timeLen + 6, base2StartY + 16);
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(16);
ctx.fillText(formatNumberK(replies), 87, base2StartY + 139);
ctx.fillText(formatNumberK(likes), 509, base2StartY + 139);
ctx.fillText(formatNumberK(retweets + quotTweets), 300, base2StartY + 139);
let currentLen = 31;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(retweets), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(retweets)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Retweets', currentLen, base2StartY + 77);
currentLen += ctx.measureText('Retweets').width;
currentLen += 10;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(quotTweets), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(quotTweets)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Quote Tweets', currentLen, base2StartY + 77);
currentLen += ctx.measureText('Quote Tweets').width;
currentLen += 10;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(likes), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(likes)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Likes', currentLen, base2StartY + 77);
ctx.beginPath();
ctx.arc(30 + 32, 84 + 32, 32, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(avatar, 30, 84, 64, 64);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'tweet.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
canvas.height += linesLen;
const likes = randomRange(Math.ceil(userData.followers * 0.0015), Math.ceil(userData.followers * 0.002));
const retweets = randomRange(Math.ceil(userData.followers * 0.00015), Math.ceil(userData.followers * 0.0002));
const quotTweets = randomRange(Math.ceil(userData.followers * 0.000015), Math.ceil(userData.followers * 0.00002));
const replies = randomRange(Math.ceil(userData.followers * 0.000015), Math.ceil(userData.followers * 0.00002));
ctx.fillStyle = '#15202b';
ctx.fillRect(0, base1.height, canvas.width, linesLen);
ctx.drawImage(base1, 0, 0);
const base2StartY = base1.height + linesLen;
ctx.drawImage(base2, 0, base2StartY);
ctx.textBaseline = 'top';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillStyle = 'white';
ctx.fillText(userData.name, 105, 84);
if (userData.verified) {
const verified = await loadImage(
path.join(__dirname, '..', '..', 'assets', 'images', 'tweet', 'verified.png')
);
const nameLen = ctx.measureText(userData.name).width;
ctx.drawImage(verified, 105 + nameLen + 4, 88, 18, 18);
}
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(17);
ctx.fillStyle = '#8899a6';
ctx.fillText(`@${userData.screenName}`, 106, 111);
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(23);
ctx.fillText(lines.join('\n'), 32, 164);
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
const time = moment().format('h:mm A ∙ MMMM D, YYYY ∙');
ctx.fillText(time, 31, base2StartY + 16);
const timeLen = ctx.measureText(time).width;
ctx.fillStyle = '#1b95e0';
ctx.fillText('Twitter for Xiao', 31 + timeLen + 6, base2StartY + 16);
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(16);
ctx.fillText(formatNumberK(replies), 87, base2StartY + 139);
ctx.fillText(formatNumberK(likes), 509, base2StartY + 139);
ctx.fillText(formatNumberK(retweets + quotTweets), 300, base2StartY + 139);
let currentLen = 31;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(retweets), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(retweets)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Retweets', currentLen, base2StartY + 77);
currentLen += ctx.measureText('Retweets').width;
currentLen += 10;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(quotTweets), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(quotTweets)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Quote Tweets', currentLen, base2StartY + 77);
currentLen += ctx.measureText('Quote Tweets').width;
currentLen += 10;
ctx.fillStyle = 'white';
ctx.font = this.client.fonts.get('Noto-Bold.ttf').toCanvasString(18);
ctx.fillText(formatNumberK(likes), currentLen, base2StartY + 77);
currentLen += ctx.measureText(formatNumberK(likes)).width;
currentLen += 5;
ctx.fillStyle = '#8899a6';
ctx.font = this.client.fonts.get('Noto-Regular.ttf').toCanvasString(18);
ctx.fillText('Likes', currentLen, base2StartY + 77);
ctx.beginPath();
ctx.arc(30 + 32, 84 + 32, 32, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(avatar, 30, 84, 64, 64);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'tweet.png' }] });
}
async fetchUser(msg, user) {