fix: improve type safety and store usage

This commit is contained in:
Aidan 2025-02-06 16:09:03 -06:00 committed by GitHub
parent 0b98ca7f7c
commit 75cc24ee2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,14 +7,37 @@
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { findStoreLazy } from "@webpack";
import { ChannelStore, SelectedChannelStore } from "@webpack/common"; import { ChannelStore, SelectedChannelStore } from "@webpack/common";
interface VoiceState {
userId: string;
channelId: string;
selfVideo: boolean;
deaf: boolean;
mute: boolean;
selfDeaf: boolean;
selfMute: boolean;
selfStream: boolean;
sessionId: string;
suppress: boolean;
requestToSpeakTimestamp: number | null;
}
interface VoiceStateUpdate {
voiceStates: Array<{
userId: string;
channelId: string;
selfVideo?: boolean;
}>;
}
const startSound = "https://raw.githubusercontent.com/redbaron2k7/videoStartNotifier/117738bff76699a89531a067e321b6406bffbc88/start.mp3"; const startSound = "https://raw.githubusercontent.com/redbaron2k7/videoStartNotifier/117738bff76699a89531a067e321b6406bffbc88/start.mp3";
const stopSound = "https://raw.githubusercontent.com/redbaron2k7/videoStartNotifier/117738bff76699a89531a067e321b6406bffbc88/stop.mp3"; const stopSound = "https://raw.githubusercontent.com/redbaron2k7/videoStartNotifier/117738bff76699a89531a067e321b6406bffbc88/stop.mp3";
const videoStates = new Map<string, boolean>(); const VoiceStateStore = findStoreLazy("VoiceStateStore");
function playNotification(isVideoOn: boolean) { function playNotification(isVideoOn: boolean): void {
new Audio(isVideoOn ? startSound : stopSound).play(); new Audio(isVideoOn ? startSound : stopSound).play();
} }
@ -38,29 +61,40 @@ export default definePlugin({
settings, settings,
flux: (() => { flux: (() => {
const lastKnownStates = new Map<string, boolean>();
return { return {
VOICE_STATE_UPDATES: ({ voiceStates }: { voiceStates: Array<{ userId: string, channelId: string, selfVideo?: boolean; }>; }) => { VOICE_STATE_UPDATES: ({ voiceStates }: VoiceStateUpdate): void => {
const currentChannelId = SelectedChannelStore.getVoiceChannelId(); const currentChannelId = SelectedChannelStore.getVoiceChannelId();
if (!currentChannelId) return; if (!currentChannelId) return;
const currentChannel = ChannelStore.getChannel(currentChannelId); const currentChannel = ChannelStore.getChannel(currentChannelId);
if (!currentChannel) return; if (!currentChannel) return;
const isPrivateChannel = currentChannel.type === 1 || currentChannel.type === 3; const isPrivateChannel = currentChannel.isPrivate();
if ((isPrivateChannel && !settings.store.playInPrivate) || if ((isPrivateChannel && !settings.store.playInPrivate) ||
(!isPrivateChannel && !settings.store.playInServer)) { (!isPrivateChannel && !settings.store.playInServer)) {
return; return;
} }
voiceStates.forEach(state => { const channelStates = VoiceStateStore.getVoiceStatesForChannel(currentChannelId) as Record<string, VoiceState>;
if (state.channelId !== currentChannelId) return;
const prevVideoState = videoStates.get(state.userId); voiceStates.forEach(state => {
if (state.selfVideo !== undefined && prevVideoState !== undefined && prevVideoState !== state.selfVideo) { if (!state?.channelId || state.channelId !== currentChannelId) return;
playNotification(state.selfVideo);
const lastKnownState = lastKnownStates.get(state.userId);
const currentState = Boolean(state.selfVideo);
if (typeof lastKnownState === "boolean" && lastKnownState !== currentState) {
playNotification(currentState);
}
lastKnownStates.set(state.userId, currentState);
if (!channelStates[state.userId]) {
lastKnownStates.delete(state.userId);
} }
videoStates.set(state.userId, state.selfVideo ?? false);
}); });
} }
}; };