This commit is contained in:
Michel Fedde 2025-06-18 22:53:54 +02:00
parent 441715675c
commit a79898b2e9
48 changed files with 2062 additions and 1503 deletions

View file

@ -4,30 +4,30 @@ import {Nullable} from "../types/Nullable";
export class IconCache {
private existingIcons: Map<string, string> | undefined;
constructor(
private readonly client: DiscordClient
) {
}
public get(iconName: string): Snowflake | null {
if (!this.existingIcons?.has(iconName)) {
return null;
}
return this.existingIcons?.get(iconName) ?? null;
}
public getEmoji(iconName: string): string {
const id = this.get(iconName);
return formatEmoji({
id,
name: iconName
});
}
public async set(iconName: string, pngBuffer: Buffer) {
const pngBase64 = pngBuffer.toString("base64");
const iconDataUrl = `data:image/png;base64,${pngBase64}`;
@ -42,23 +42,23 @@ export class IconCache {
}
)
}
public async populate() {
if (this.existingIcons != null) {
return;
}
const existingEmojis: Nullable<DiscordIconRequest> = await this.client.RESTClient.get(
Routes.applicationEmojis(this.client.ApplicationId)
)
if (!existingEmojis) {
return;
}
this.existingIcons = new Map<string, string>(
existingEmojis.items.map((item) => {
return [ item.name, item.id ]
return [item.name, item.id]
})
)
}

View file

@ -5,29 +5,30 @@ 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 (const dirname of directory) {
const iconName = path.basename(dirname.name, '.svg').replaceAll('-','_');
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 => {
@ -48,8 +49,8 @@ export class IconDeployer {
)
}
)
await this.iconCache.set(iconName, pngBuffer);
}
}