diff --git a/app/services/discord/index.ts b/app/services/discord/index.ts index a6cea0b..7c9829e 100644 --- a/app/services/discord/index.ts +++ b/app/services/discord/index.ts @@ -1,5 +1,6 @@ import { Container } from "@/app/Container"; import { Data, GameBridge, Service } from "@/app/services"; +import { getAsBase64 } from "@/utils"; import Discord from "discord.js"; import DiscordConfig from "@/config/discord.json"; import modules from "./modules"; @@ -125,8 +126,10 @@ export class DiscordBot extends Service { try { const guild = this.getGuild(); if (!guild) return false; - this.data.lastDiscordGuildIcon = - this.discord.user.avatarURL() ?? guild.iconURL() ?? this.data.lastDiscordGuildIcon; + const iconURL = this.discord.user.avatarURL() ?? guild.iconURL(); + this.data.lastDiscordGuildIcon = iconURL + ? (await getAsBase64(iconURL)) ?? this.data.lastDiscordGuildIcon + : this.data.lastDiscordGuildIcon; await this.data.save(); await this.discord.user.setAvatar(path); await guild.setIcon(path, reason); @@ -163,9 +166,13 @@ export class DiscordBot extends Service { if (!this.ready || !(await this.overLvl2())) return false; try { const guild = this.getGuild(); - this.data.lastDiscordBanner = guild?.bannerURL() ?? this.data.lastDiscordBanner; + if (!guild) return false; + const bannerURL = guild.bannerURL(); + this.data.lastDiscordBanner = bannerURL + ? (await getAsBase64(bannerURL)) ?? this.data.lastDiscordBanner + : this.data.lastDiscordBanner; await this.data.save(); - await guild?.setBanner(url, reason); + await guild.setBanner(url, reason); return true; } catch { return false; diff --git a/utils.ts b/utils.ts index 486dd57..572028e 100644 --- a/utils.ts +++ b/utils.ts @@ -64,6 +64,20 @@ const LOOKUP_PATH = webappconfig.lookupPath; export const GMOD_PATH_MATCH = /^(?(?:lua|gamemodes)\/(?[-_.A-Za-z0-9]+?|)?(?:\/.*)?\/(?[-_.A-Za-z0-9]+)\.(?[a-z]*))?(?::-?(?\d+)-?(?\d+)?)?$/g; +export const getAsBase64 = async (url: string): Promise => { + if (!url.match(/^https?:\/\/.+/)) return null; + try { + const res = await axios.get(url, { responseType: "arraybuffer" }); + + const contentType = res.headers["content-type"] || "image/png"; + const base64 = Buffer.from(res.data, "binary").toString("base64"); + + return `data:${contentType};base64,${base64}`; + } catch (error) { + return null; + } +}; + export const getOrFetchGmodFile = async (path: PathLike) => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const [, fpath, addon, filename, ext, linenos, linenoe] =