From 388b0a399ef2277178deba0c213f4204d1b087e4 Mon Sep 17 00:00:00 2001 From: "wont.stream" Date: Fri, 27 Sep 2024 15:17:45 -0400 Subject: [PATCH] fix --- bun.lockb | Bin 40587 -> 40587 bytes plugins/arRPC/index.ts | 85 +++++++++++++++++++-------------------- plugins/arRPC/webpack.ts | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 43 deletions(-) create mode 100644 plugins/arRPC/webpack.ts diff --git a/bun.lockb b/bun.lockb index 6bab354a9c6a807af26b9b641008064144772dfa..1b82f288f8f00829848b30f4af35a6a4971cff1f 100644 GIT binary patch delta 21 ccmeC)%hbJ>X@g)M8>69~rJm7d@w^fP08je{p8x;= delta 21 acmeC)%hbJ>X@g)M8xsQvY!=TeF#rHaNCl_> diff --git a/plugins/arRPC/index.ts b/plugins/arRPC/index.ts index f352015..c6648eb 100644 --- a/plugins/arRPC/index.ts +++ b/plugins/arRPC/index.ts @@ -1,12 +1,17 @@ +import { findByProps } from "./webpack.ts"; +import type { ShelterApi } from "@uwu/shelter-defs"; + const { flux: { dispatcher: FluxDispatcher, stores: { ApplicationStore }, }, - http: { get }, -} = shelter; + http: { get, post }, +} = shelter as ShelterApi; + const apps = {}; const assetCache = {}; +const httpAssetCache = {}; const fetchAssetId = async (applicationId, assetName) => { // TO-DO Use APPLICATION_ASSETS_UPDATE and APPLICATION_ASSETS_FETCH @@ -35,63 +40,57 @@ const fetchAssetId = async (applicationId, assetName) => { return resource ? resource.id : null; }; -export class Socket extends WebSocket { - private _reconnectDelay: number; - private _url: string; - - constructor(url: string) { - super(url); - this._url = url; - this._reconnectDelay = 1000; - - /* - this.addEventListener("open", () => { - console.log(`Socket > Connected to ${new URL(url).hostname}`); - }); - */ - - this.addEventListener("close", () => { - setTimeout(() => { - this._reconnect(); - }, this._reconnectDelay); - }); +const getStuff = findByProps("fetchAssetIds", "getAssetImage"); +const fetchHttpAsset = async (applicationId, assetId) => { + if (!httpAssetCache[assetId]) { + try { + const response = await getStuff.fetchAssetIds(applicationId, [assetId]); - this.addEventListener("error", () => { - setTimeout(() => { - this._reconnect(); - }, this._reconnectDelay); - }); + httpAssetCache[assetId] = response[0]; + } catch (error) { + console.error("Request failed", error); + return null; + } } - private _reconnect() { - this.close(); - const newSocket = new Socket(this._url); - Object.setPrototypeOf(this, Object.getPrototypeOf(newSocket)); - Object.assign(this, newSocket); - } -} + return httpAssetCache[assetId]; +}; // https://images-ext-1.discordapp.net/external/1u9jSonO6pasZ41RA1LGqSbTysKHHF0MzoL0YDeTJg0/https/lh3.googleusercontent.com/fhDgRO0LrPPo9CDqsLQlxR3CVZc8xPmSi9Ja8DKAS5zhoWsZKdj2scyWqBUU2t4DHxK1xcbWKY2Q7cpj%3Dw544-h544-l90-rj?format=webp&width=300&height=300 export function onLoad() { - const ws = new Socket("ws://localhost:1337"); + const ws = new WebSocket("ws://localhost:1337"); ws.onmessage = async ({ data }) => { const msg = JSON.parse(data); - if (msg.activity?.assets?.large_image.startsWith("https://")) { - msg.activity.assets.large_image = `https://images-ext-1.discordapp.net/external/${msg.activity.assets.large_image.replace("https://", "https/")}?format=webp&width=300&height=300`; - } else { - if (msg.activity?.assets?.large_image) - msg.activity.assets.large_image = await fetchAssetId( + if (msg.activity?.assets?.large_image) { + if (msg.activity.assets.large_image.startsWith("https://")) { + msg.activity.assets.large_image = await fetchHttpAsset( msg.activity.application_id, msg.activity.assets.large_image, ); - if (msg.activity?.assets?.small_image) - msg.activity.assets.small_image = await fetchAssetId( + } else { + msg.activity.assets.large_image = await fetchAssetId( msg.activity.application_id, - msg.activity.assets.small_image, + msg.activity.assets.large_image, ); + } + + if (msg.activity.assets.small_image) { + if (msg.activity.assets.small_image.startsWith("https://")) { + msg.activity.assets.small_image = await fetchHttpAsset( + msg.activity.application_id, + msg.activity.assets.small_image, + ); + } else { + msg.activity.assets.small_image = await fetchAssetId( + msg.activity.application_id, + msg.activity.assets.small_image, + ); + } + } } + if (msg.activity) { // TODO - Support games from DB too lool const appId = msg.activity.application_id; diff --git a/plugins/arRPC/webpack.ts b/plugins/arRPC/webpack.ts new file mode 100644 index 0000000..996dbac --- /dev/null +++ b/plugins/arRPC/webpack.ts @@ -0,0 +1,82 @@ +// Original code taken from https://github.com/Vendicated/Vencord under GPLv3 license + +// biome-ignore lint/suspicious/noImplicitAnyLet: N/A +let wreq; +// biome-ignore lint/suspicious/noImplicitAnyLet: N/A +// biome-ignore lint/style/useConst: N/A +let cache; + +declare global { + interface Window { + // biome-ignore lint/suspicious/noExplicitAny: N/A + webpackChunkdiscord_app: any[]; + } +} + +window.webpackChunkdiscord_app.push([ + [Symbol()], + {}, + (x) => { + wreq = x; + }, +]); +window.webpackChunkdiscord_app.pop(); +cache = wreq.c; + +export const filters = { + byProps: + (...props) => + (m) => + props.every((p) => m[p] !== void 0), +}; + +export function handleModuleNotFound(method, ...filter) { + const errMsg = `webpack.${method} found no module. Filter: ${filter}`; + console.error(new Error(errMsg)); + return { error: errMsg }; +} + +export function find(filter, { isIndirect = false, isWaitFor = false } = {}) { + if (typeof filter !== "function") { + return handleModuleNotFound("find", "Invalid filter function"); + } + + for (const key in cache) { + const mod = cache[key]; + if (!mod.loaded || !mod?.exports) continue; + + const exports = mod.exports; + + if (filter(exports)) return isWaitFor ? [exports, key] : exports; + + if (exports.default && filter(exports.default)) { + const found = exports.default; + return isWaitFor ? [found, key] : found; + } + + if (typeof exports === "object") { + for (const nestedKey in exports) { + if (nestedKey.length > 3) continue; + + const nested = exports[nestedKey]; + if (nested && filter(nested)) { + return isWaitFor ? [nested, key] : nested; + } + } + } + } + + if (!isIndirect) { + return handleModuleNotFound("find", filter); + } + + return isWaitFor ? [null, null] : null; +} + +export function findByProps(...props) { + const result = find(filters.byProps(...props), { isIndirect: true }); + if (!result || result.error) { + return handleModuleNotFound("findByProps", ...props); + } + return result; +}