import { definePluginSettings } from "@api/Settings"; import { Logger } from "@utils/Logger"; import definePlugin, { OptionType } from "@utils/types"; import { FluxDispatcher } from "@webpack/common"; const logger = new Logger("muter"); const settings = definePluginSettings({ port: { description: "Port to connect to muter on.", type: OptionType.NUMBER, default: 3034, onChange: value => { port = value; socket.close(); connect(); } }, reconnect: { description: "Reconnect interval in seconds.", type: OptionType.NUMBER, default: 5, onChange: value => { reconnect = value * 1000; } } }); let port: number; let reconnect: number; let muted = false; let socket: WebSocket; function connect() { logger.info(`Attempting to connect to muter on port ${port}`); socket = new WebSocket(`ws://127.0.0.1:${port}`); socket.onopen = () => { logger.info("Connected to muter"); }; socket.onmessage = (event) => { muted = event.data.size > 0; FluxDispatcher.dispatch({ type: "AUDIO_SET_SELF_MUTE", context: "default", mute: muted }); }; socket.onclose = e => { if (!e.wasClean) { logger.error("Connection to muter was interrupted. Reconnecting..."); setTimeout(connect, reconnect); } else { logger.info("Connection to muter closed"); } }; } export default definePlugin({ name: "muter", description: "muter companion plugin. Mirrors system mutes to Discord.", authors: [{ name: "isitreallyalive", id: 1269669249056510026n }], settings, start() { port = settings.store.port; reconnect = settings.store.reconnect; connect(); // todo: try to fix weird virtual bug }, flux: { AUDIO_TOGGLE_SELF_MUTE: () => { const buf = new ArrayBuffer(1); const view = new Uint8Array(buf); if (!muted) view[0] = 1; socket.send(new Blob([buf])); } } });