Skip to content

Commit

Permalink
save as base64 instead since the urls are invalidated
Browse files Browse the repository at this point in the history
after they've been changed
  • Loading branch information
Techbot121 authored and Meta Construct committed Jan 6, 2025
1 parent edc3000 commit 255a331
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/services/discord/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ const LOOKUP_PATH = webappconfig.lookupPath;
export const GMOD_PATH_MATCH =
/^(?<path>(?:lua|gamemodes)\/(?<addon>[-_.A-Za-z0-9]+?|)?(?:\/.*)?\/(?<filename>[-_.A-Za-z0-9]+)\.(?<ext>[a-z]*))?(?::-?(?<linenos>\d+)-?(?<linenoe>\d+)?)?$/g;

export const getAsBase64 = async (url: string): Promise<string | null> => {
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] =
Expand Down

0 comments on commit 255a331

Please sign in to comment.