/* * Vencord, a Discord client mod * Copyright (c) 2024 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import "./styles.css"; import { playAudio } from "@api/AudioPlayer"; import { NavContextMenuPatchCallback } from "@api/ContextMenu"; import { definePluginSettings } from "@api/Settings"; import { Button } from "@components/Button"; import { Devs } from "@utils/constants"; import { classNameFactory } from "@utils/css"; import { t } from "@utils/translation"; import definePlugin, { OptionType } from "@utils/types"; import { findComponentByCodeLazy } from "@webpack"; import { ChannelActions, Constants, GuildStore, IconUtils, MediaEngineStore, Menu, RestAPI, SearchableSelect, SelectedChannelStore, TextInput, Toasts } from "@webpack/common"; const cl = classNameFactory("vc-exitsounds-"); const PlayIcon = findComponentByCodeLazy("4.96v14.08c0"); function GuildSelector() { const { soundGuildId } = settings.use(["soundGuildId"]); const options = Object.values(GuildStore.getGuilds()).map(g => ({ value: g.id, label: g.name })); return ( o.value === soundGuildId)} placeholder={t("exitSounds.ui.selectServer")} maxVisibleItems={6} closeOnSelect={true} onChange={v => settings.store.soundGuildId = v} renderOptionPrefix={o => { const guild = GuildStore.getGuild(o?.value); if (!guild?.icon) return null; return ( ); }} /> ); } function SoundIdInput() { const { soundId } = settings.use(["soundId"]); return (
settings.store.soundId = v} placeholder={t("exitSounds.ui.enterSoundId")} />
); } const settings = definePluginSettings({ soundGuildId: { type: OptionType.COMPONENT, description: t("exitSounds.settings.soundGuildId"), component: GuildSelector }, soundId: { type: OptionType.COMPONENT, description: t("exitSounds.settings.soundId"), component: SoundIdInput } }); const SoundButtonContext: NavContextMenuPatchCallback = (children, { sound }: { sound: { soundId: string; guildId: string; }; }) => { children.splice(1, 0, { settings.store.soundGuildId = sound.guildId; settings.store.soundId = sound.soundId; }} /> ); }; let original: typeof ChannelActions.selectVoiceChannel; export default definePlugin({ name: "ExitSounds", description: t("exitSounds.description"), authors: [Devs.prism], dependencies: ["AudioPlayerAPI"], settings, contextMenus: { "sound-button-context": SoundButtonContext }, start() { original = ChannelActions.selectVoiceChannel; ChannelActions.selectVoiceChannel = async (id: string | null, ...args: unknown[]) => { const { soundGuildId, soundId } = settings.store; const voiceId = SelectedChannelStore.getVoiceChannelId(); if (soundGuildId && soundId && voiceId !== id && !MediaEngineStore.isDeaf()) { try { await RestAPI.post({ url: Constants.Endpoints.SEND_SOUNDBOARD_SOUND(voiceId), body: { sound_id: soundId, source_guild_id: soundGuildId } }); await new Promise(r => setTimeout(r, 500)); } catch { Toasts.show({ message: t("exitSounds.toasts.somethingWentWrong"), id: Toasts.genId(), type: Toasts.Type.FAILURE }); } } return original(id, ...args); }; }, stop() { ChannelActions.selectVoiceChannel = original; } });