feat: mostly complete rating command

This commit is contained in:
TrèsAbhi committed 2024-04-24 16:22:48 -05:00
1 parent 983b96fd99
commit 1f30d1d2c3
11 files changed
+384 -32

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ import sharp from 'sharp';
const cache: Record<string, Buffer> = {};
export async function webpToPng(url: string) {
export async function iconPng(url: string) {
if (!url.endsWith('.webp')) return url;
if (!cache[url]) {
+6 -3
View File
@@ -1,9 +1,12 @@
import jsxToSvg from './jsxToSvg';
import { RenderConfiguration } from './renderConfiguration';
import { svgToPngThreaded } from './svgToPngThreaded';
export default async function jsxToPngThreaded(jsx: JSX.Element) {
const svg = await jsxToSvg(jsx);
export default async function jsxToPngThreaded(
jsx: JSX.Element,
renderConfiguration: RenderConfiguration,
) {
const svg = await jsxToSvg(jsx, renderConfiguration);
const png = await svgToPngThreaded(svg);
return png;
}
+6 -2
View File
@@ -1,5 +1,6 @@
import { readFileSync } from 'fs';
import satori from 'satori';
import { RenderConfiguration } from '../renderConfiguration';
import robotoBlack from './Roboto-Black.ttf';
import robotoBold from './Roboto-Bold.ttf';
import roboto from './Roboto.ttf';
@@ -10,9 +11,12 @@ const fonts = [
{ file: robotoBlack, weight: 900 as const },
];
export default async function jsxToSvg(jsx: JSX.Element) {
export default async function jsxToSvg(
jsx: JSX.Element,
renderConfiguration: RenderConfiguration,
) {
return await satori(jsx, {
width: 480,
width: renderConfiguration.width,
fonts: fonts.map(({ file, weight }) => ({
weight,
name: 'Roboto',
+11
View File
@@ -0,0 +1,11 @@
export interface RenderConfiguration {
width: number;
}
export class RenderConfiguration implements RenderConfiguration {
width = 480;
constructor(configuration?: Partial<RenderConfiguration>) {
Object.assign(this, configuration);
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { webpToPng } from './iconPng';
import { iconPng } from './iconPng';
import { tankIcon } from './tankIcon';
export function tankIconPng(id: number) {
return webpToPng(tankIcon(id));
return iconPng(tankIcon(id));
}
@@ -9,6 +9,7 @@ import {
} from 'discord.js';
import { InteractionReturnable } from '../../events/interactionCreate';
import jsxToPngThreaded from '../blitzkit/jsxToPngThreaded';
import { RenderConfiguration } from '../blitzkit/renderConfiguration';
export default async function normalizeInteractionReturnable(
returnable: InteractionReturnable,
@@ -17,11 +18,11 @@ export default async function normalizeInteractionReturnable(
const reply: InteractionEditReplyOptions &
InteractionReplyOptions &
MessageEditOptions = {};
const awaitedReturnable = await returnable;
const normalizedReturnable = Array.isArray(awaitedReturnable)
? awaitedReturnable
: [awaitedReturnable];
let renderConfiguration = new RenderConfiguration();
await Promise.all(
normalizedReturnable.map(async (item, index) => {
@@ -39,10 +40,12 @@ export default async function normalizeInteractionReturnable(
} else if (item instanceof AttachmentBuilder) {
if (!reply.files) reply.files = [];
reply.files.push(item);
} else if (item instanceof RenderConfiguration) {
renderConfiguration = item;
} else if (item === null) {
return;
} else {
const image = await jsxToPngThreaded(item);
const image = await jsxToPngThreaded(item, renderConfiguration);
images.push([index, image]);
}
}),