Skip to content

Commit 255a331

Browse files
Techbot121Meta Construct
authored andcommitted
save as base64 instead since the urls are invalidated
after they've been changed
1 parent edc3000 commit 255a331

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

app/services/discord/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Container } from "@/app/Container";
22
import { Data, GameBridge, Service } from "@/app/services";
3+
import { getAsBase64 } from "@/utils";
34
import Discord from "discord.js";
45
import DiscordConfig from "@/config/discord.json";
56
import modules from "./modules";
@@ -125,8 +126,10 @@ export class DiscordBot extends Service {
125126
try {
126127
const guild = this.getGuild();
127128
if (!guild) return false;
128-
this.data.lastDiscordGuildIcon =
129-
this.discord.user.avatarURL() ?? guild.iconURL() ?? this.data.lastDiscordGuildIcon;
129+
const iconURL = this.discord.user.avatarURL() ?? guild.iconURL();
130+
this.data.lastDiscordGuildIcon = iconURL
131+
? (await getAsBase64(iconURL)) ?? this.data.lastDiscordGuildIcon
132+
: this.data.lastDiscordGuildIcon;
130133
await this.data.save();
131134
await this.discord.user.setAvatar(path);
132135
await guild.setIcon(path, reason);
@@ -163,9 +166,13 @@ export class DiscordBot extends Service {
163166
if (!this.ready || !(await this.overLvl2())) return false;
164167
try {
165168
const guild = this.getGuild();
166-
this.data.lastDiscordBanner = guild?.bannerURL() ?? this.data.lastDiscordBanner;
169+
if (!guild) return false;
170+
const bannerURL = guild.bannerURL();
171+
this.data.lastDiscordBanner = bannerURL
172+
? (await getAsBase64(bannerURL)) ?? this.data.lastDiscordBanner
173+
: this.data.lastDiscordBanner;
167174
await this.data.save();
168-
await guild?.setBanner(url, reason);
175+
await guild.setBanner(url, reason);
169176
return true;
170177
} catch {
171178
return false;

utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ const LOOKUP_PATH = webappconfig.lookupPath;
6464
export const GMOD_PATH_MATCH =
6565
/^(?<path>(?:lua|gamemodes)\/(?<addon>[-_.A-Za-z0-9]+?|)?(?:\/.*)?\/(?<filename>[-_.A-Za-z0-9]+)\.(?<ext>[a-z]*))?(?::-?(?<linenos>\d+)-?(?<linenoe>\d+)?)?$/g;
6666

67+
export const getAsBase64 = async (url: string): Promise<string | null> => {
68+
if (!url.match(/^https?:\/\/.+/)) return null;
69+
try {
70+
const res = await axios.get(url, { responseType: "arraybuffer" });
71+
72+
const contentType = res.headers["content-type"] || "image/png";
73+
const base64 = Buffer.from(res.data, "binary").toString("base64");
74+
75+
return `data:${contentType};base64,${base64}`;
76+
} catch (error) {
77+
return null;
78+
}
79+
};
80+
6781
export const getOrFetchGmodFile = async (path: PathLike) => {
6882
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6983
const [, fpath, addon, filename, ext, linenos, linenoe] =

0 commit comments

Comments
 (0)