pnp-scheduler/source/Icons/IconDeployer.ts

56 lines
No EOL
1.4 KiB
TypeScript

import {REST, Routes} from "discord.js";
import path from "node:path";
import * as fs from "node:fs";
import svg2img from "svg2img";
import {IconCache} from "./IconCache";
export class IconDeployer {
static ICON_PATH = path.resolve('public/icons')
constructor(
private readonly iconCache: IconCache
) {}
public async ensureExistance() {
const directory = await fs.promises.opendir(IconDeployer.ICON_PATH);
const addIconPromises: Promise<void>[] = [];
for await (let dirname of directory) {
const iconName = path.basename(dirname.name, '.svg').replaceAll('-','_');
if (this.iconCache.get(iconName) !== null) {
continue;
}
addIconPromises.push(
this.addIcon(path.resolve(dirname.parentPath, dirname.name), iconName)
);
}
await Promise.all(addIconPromises);
}
private async addIcon(iconPath: string, iconName: string) {
const svgBuffer = await fs.promises.readFile(iconPath, 'utf-8');
const pngBuffer = await new Promise<Buffer>(resolve => {
svg2img(
svgBuffer,
{
format: "png",
resvg: {
fitTo: {
mode: "width",
value: 128
}
}
},
function (err, buffer) {
resolve(buffer);
}
)
}
)
await this.iconCache.set(iconName, pngBuffer);
}
}