diff --git a/src/plugins/rpcEditor/ReplaceSettings.tsx b/src/plugins/rpcEditor/ReplaceSettings.tsx new file mode 100644 index 000000000..26024195b --- /dev/null +++ b/src/plugins/rpcEditor/ReplaceSettings.tsx @@ -0,0 +1,155 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { CheckedTextInput } from "@components/CheckedTextInput"; +import { Margins } from "@utils/margins"; +import { identity } from "@utils/misc"; +import { findByPropsLazy } from "@webpack"; +import { Card, Forms, React, Select, SnowflakeUtils, Switch } from "@webpack/common"; + +import { ActivityType, AppIdSetting, makeEmptyAppId } from "."; + +interface SettingsProps { + appIds: AppIdSetting[]; + update: () => void; + save: () => void; +} + +interface RpcApp { + id: string; + name: string; + icon: string; + flags: number; +} + +const RPCUtils = findByPropsLazy("fetchApplicationsRPC", "getRemoteIconURL"); + +const cachedApps: any = {}; +async function lookupApp(appId: string): Promise { + if (cachedApps[appId]) return cachedApps[appId]; + const socket: any = {}; + try { + await RPCUtils.fetchApplicationsRPC(socket, appId); + console.log(`Lookup finished for ${socket.application.name}`); + cachedApps[appId] = socket.application; + return socket.application; + } catch { + console.log(`Lookup failed for ${appId}`); + return null; + } +} + +function isValidSnowflake(v: string) { + const regex = /^\d{17,20}$/; + return regex.test(v) && !Number.isNaN(SnowflakeUtils.extractTimestamp(v)); +} + +export function ReplaceTutorial() { + return ( + <> + How to get an Application ID + + The method of getting an app's id will differ depending on what app it is. If the source code is available you can most likely find it inside the app's repository. + + + Another method is to start the app in question, then open Discord's console and look for a log from RPCServer saying something like + "cmd: 'SET_ACTIVITY'" with your app's name somewhere inside + + + + Note: ActivityTypes other than Playing will only show timestamps on Mobile. It's a Discord issue. + + + ); +} + +export function ReplaceSettings({ appIds, update, save }: SettingsProps) { + async function onChange(val: string | boolean, index: number, key: string) { + if (index === appIds.length - 1) + appIds.push(makeEmptyAppId()); + + appIds[index][key] = val; + + if (val && key === "appId") { + const tempApp = await lookupApp(val.toString()); + appIds[index].appName = tempApp?.name || "Unknown"; + } + + if (appIds[index].appId === "" && index !== appIds.length - 1) + appIds.splice(index, 1); + + save(); + update(); + } + + return ( + <> + { + appIds.map((setting, i) => + + { + onChange(value, i, "enabled"); + }} + className={Margins.bottom16} + hideBorder={true} + > + {setting.appName} + + Application ID + { + onChange(v, i, "appId"); + }} + validate={v => + !v || isValidSnowflake(v) || "Invalid appId, must be a snowflake" + } + /> + {setting.activityType === ActivityType.STREAMING && + <> + Stream URL + { + onChange(v, i, "streamUrl"); + }} + validate={st => !/https?:\/\/(www\.)?(twitch\.tv|youtube\.com)\/\w+/.test(st) && "Only Twitch and Youtube urls will work." || true} + /> + } + New activity type + explode()}/> + DataStore.set(APP_IDS_KEY, appIds)} + /> ); } - } + }, }); -function explode() { - -} - export default definePlugin({ name: "RPCEditor", - description: "Allows editing the type or content of any Rich Presence. (Configure in settings)", - authors: [Devs.nin0dev], + description: "Edit the type and content of any Rich Presence", + authors: [Devs.Nyako, Devs.nin0dev], patches: [ { - find: "LocalActivityStore", + find: '="LocalActivityStore",', replacement: { match: /LOCAL_ACTIVITY_UPDATE:function\((\i)\)\{/, replace: "$&$self.patchActivity($1.activity);", @@ -63,10 +100,30 @@ export default definePlugin({ } ], settings, - patchActivity(activity: any) { - // not finished, this'll change all activities to listening :husk: + settingsAboutComponent: () => , + + async start() { + appIds = await DataStore.get(APP_IDS_KEY) ?? [makeEmptyAppId()]; + }, + + patchActivity(activity: Activity) { + if (!activity) return; console.log(activity); - activity.type = 2; - activity.assets.large_text = null; // bomb premid image text + appIds.forEach(app => { + if (app.enabled && app.appId === activity.application_id) { + activity.type = app.activityType; + + if (app.activityType === ActivityType.STREAMING && app.streamUrl) { + activity.url = app.streamUrl; + } + + if (app.swapNameAndDetails) { + const media = activity.details; + activity.details = activity.name; + activity.name = media; + } + + } + }); }, });