Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wont-stream committed Sep 27, 2024
1 parent c0d67bf commit 388b0a3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 43 deletions.
Binary file modified bun.lockb
Binary file not shown.
85 changes: 42 additions & 43 deletions plugins/arRPC/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
82 changes: 82 additions & 0 deletions plugins/arRPC/webpack.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 388b0a3

Please sign in to comment.