66 lines
No EOL
1.5 KiB
TypeScript
66 lines
No EOL
1.5 KiB
TypeScript
import {formatEmoji, Routes, Snowflake} from "discord.js";
|
|
import {DiscordClient} from "../Discord/DiscordClient";
|
|
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}`;
|
|
|
|
await this.client.RESTClient.post(
|
|
Routes.applicationEmojis(this.client.ApplicationId),
|
|
{
|
|
body: {
|
|
name: iconName,
|
|
image: iconDataUrl
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
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 ]
|
|
})
|
|
)
|
|
}
|
|
|
|
} |