/* * Vencord, a Discord client mod * Copyright (c) 2026 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import { NavContextMenuPatchCallback } from "@api/ContextMenu"; import { definePluginSettings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { sendMessage } from "@utils/discord"; import definePlugin, { OptionType } from "@utils/types"; import { findComponentByCodeLazy } from "@webpack"; import { ChannelActionCreators, ChannelStore, GuildStore, Menu, React, RestAPI, Select, SettingsRouter, TextInput, UserStore, VoiceStateStore, } from "@webpack/common"; const HeaderBarIcon = findComponentByCodeLazy(".HEADER_BAR_BADGE_BOTTOM,", 'position:"bottom"'); // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- interface MoveItem { id: string; userId: string; guildId: string; enabled: boolean; /** true = disconnect on join, false = auto-move loop */ disconnectOnJoin: boolean; /** delay between moves in ms (default: 2000) */ delay: number; /** server-mute on join */ serverMute: boolean; /** server-deafen on join */ serverDeafen: boolean; sendDm: boolean; dmMessage: string; } interface MoveState { timer: ReturnType | null; guildId: string; } const ACTION_DELAY_MS = 1000; /** Pause for a given number of milliseconds */ const wait = (ms: number) => new Promise((r) => setTimeout(r, ms)); // --------------------------------------------------------------------------- // State // --------------------------------------------------------------------------- const activeMoves = new Map(); // --------------------------------------------------------------------------- // Settings // --------------------------------------------------------------------------- const settings = definePluginSettings({ moveListManager: { type: OptionType.COMPONENT, description: "Manage auto-move targets", component: () => , }, }); // --------------------------------------------------------------------------- // Persistence helpers // --------------------------------------------------------------------------- function getMoveList(): MoveItem[] { try { const stored = Vencord.Settings.plugins.autoMover?.moveList; return stored ? JSON.parse(stored) : []; } catch { return []; } } function setMoveList(list: MoveItem[]) { Vencord.Settings.plugins.autoMover.moveList = JSON.stringify(list); } // --------------------------------------------------------------------------- // Move logic // --------------------------------------------------------------------------- function stopMoving(userId: string) { const state = activeMoves.get(userId); if (state) { if (state.timer) clearTimeout(state.timer); activeMoves.delete(userId); } } function getRandomVoiceChannel( guildId: string, excludeChannelId: string, ): string | null { const channels = ChannelStore.getMutableGuildChannelsForGuild(guildId); // Channel type 2 = GUILD_VOICE const voiceChannels = Object.values(channels).filter( (c) => (c as any).type === 2 && c.id !== excludeChannelId, ); if (voiceChannels.length === 0) return null; return voiceChannels[Math.floor(Math.random() * voiceChannels.length)].id; } async function moveUser(userId: string, guildId: string, channelId: string) { try { await RestAPI.patch({ url: `/guilds/${guildId}/members/${userId}`, body: { channel_id: channelId }, }); } catch (e) { console.error("[autoMover] Failed to move user:", e); } } async function disconnectUser(userId: string, guildId: string) { try { await RestAPI.patch({ url: `/guilds/${guildId}/members/${userId}`, body: { channel_id: null }, }); } catch (e) { console.error("[autoMover] Failed to disconnect user:", e); } } async function serverMuteUser(userId: string, guildId: string) { try { await RestAPI.patch({ url: `/guilds/${guildId}/members/${userId}`, body: { mute: true }, }); } catch (e) { console.error("[autoMover] Failed to server-mute user:", e); } } async function serverDeafenUser(userId: string, guildId: string) { try { await RestAPI.patch({ url: `/guilds/${guildId}/members/${userId}`, body: { deaf: true }, }); } catch (e) { console.error("[autoMover] Failed to server-deafen user:", e); } } async function sendDmToUser(item: MoveItem) { const message = item.dmMessage; if (!message) return; try { let dmChannelId = ChannelStore.getDMFromUserId(item.userId); if (!dmChannelId) { await ChannelActionCreators.openPrivateChannel(item.userId); await new Promise((r) => setTimeout(r, 500)); dmChannelId = ChannelStore.getDMFromUserId(item.userId); } if (dmChannelId) { await sendMessage(dmChannelId, { content: message }); } } catch (e) { console.error("[autoMover] Failed to send DM:", e); } } function scheduleMove(item: MoveItem) { const { userId, guildId } = item; // Check if item is still in the list and enabled const list = getMoveList(); const current = list.find((i) => i.id === item.id); if (!current || !current.enabled || current.disconnectOnJoin) { stopMoving(userId); return; } // Check if user is still in a voice channel in this guild const voiceState = VoiceStateStore.getVoiceStateForUser(userId); if ( !voiceState || !voiceState.channelId || (voiceState.guildId && voiceState.guildId !== guildId) ) { stopMoving(userId); return; } // Pick a random voice channel different from current const nextChannelId = getRandomVoiceChannel(guildId, voiceState.channelId); if (!nextChannelId) { stopMoving(userId); return; } moveUser(userId, guildId, nextChannelId); // Schedule next move using the configured delay const state = activeMoves.get(userId); if (state) { state.timer = setTimeout(() => scheduleMove(item), current.delay); } } async function startMoving(item: MoveItem) { // Stop any existing move for this user stopMoving(item.userId); // 1. Send DM if enabled if (item.sendDm) { await sendDmToUser(item); await wait(ACTION_DELAY_MS); } // 2. Server mute if enabled if (item.serverMute) { await serverMuteUser(item.userId, item.guildId); await wait(ACTION_DELAY_MS); } // 3. Server deafen if enabled if (item.serverDeafen) { await serverDeafenUser(item.userId, item.guildId); await wait(ACTION_DELAY_MS); } // 4. Disconnect or start move loop if (item.disconnectOnJoin) { await disconnectUser(item.userId, item.guildId); } else if (item.enabled) { const state: MoveState = { timer: null, guildId: item.guildId }; activeMoves.set(item.userId, state); scheduleMove(item); } } // --------------------------------------------------------------------------- // Context Menu (right-click user → toggle autoMover / autoDisconnect) // --------------------------------------------------------------------------- interface UserContextProps { channel: any; guildId?: string; user: { id: string }; } /** * Toggle an entry for the given user. * @param disconnectMode false = auto-move, true = auto-disconnect. */ function toggleUserInList( userId: string, guildId: string | undefined, disconnectMode: boolean, ) { const list = getMoveList(); const gid = guildId ?? "*"; const existing = list.find((i) => i.userId === userId && i.guildId === gid); if (existing) { const wasActive = existing.enabled; // If clicking the already-active mode, toggle off if (existing.enabled && existing.disconnectOnJoin === disconnectMode) { existing.enabled = !existing.enabled; } else { // Switch mode (or enable from off) existing.enabled = true; existing.disconnectOnJoin = disconnectMode; } setMoveList(list); // Always stop any running loop before potentially restarting if (wasActive) stopMoving(userId); if (!existing.enabled) { stopMoving(userId); } else { // If user is already in a VC, start immediately tryStartIfInVoice(existing); } } else { // Create new entry const newItem: MoveItem = { id: Date.now().toString(), userId, guildId: gid, enabled: true, disconnectOnJoin: disconnectMode, delay: 2000, serverMute: false, serverDeafen: false, sendDm: false, dmMessage: "", }; setMoveList([...list, newItem]); // If user is already in a VC, start immediately tryStartIfInVoice(newItem); } } /** * Toggle serverMute or serverDeafen for a user. * Creates an entry if none exists yet (with move/disconnect disabled). */ function toggleServerAction( userId: string, guildId: string | undefined, key: "serverMute" | "serverDeafen", ) { const list = getMoveList(); const gid = guildId ?? "*"; const existing = list.find((i) => i.userId === userId && i.guildId === gid); if (existing) { (existing as any)[key] = !(existing as any)[key]; setMoveList(list); // If toggling on and user is in VC, apply immediately if ((existing as any)[key]) { const voiceState = VoiceStateStore.getVoiceStateForUser(userId); if ( voiceState?.channelId && (existing.guildId === "*" || voiceState.guildId === existing.guildId) ) { if (key === "serverMute") { serverMuteUser( userId, voiceState.guildId || existing.guildId, ); } else { serverDeafenUser( userId, voiceState.guildId || existing.guildId, ); } } } } else { const newItem: MoveItem = { id: Date.now().toString(), userId, guildId: gid, enabled: false, disconnectOnJoin: false, delay: 2000, serverMute: key === "serverMute", serverDeafen: key === "serverDeafen", sendDm: false, dmMessage: "", }; setMoveList([...list, newItem]); // Apply immediately if user is in VC const voiceState = VoiceStateStore.getVoiceStateForUser(userId); if ( voiceState?.channelId && (newItem.guildId === "*" || voiceState.guildId === newItem.guildId) ) { if (key === "serverMute") { serverMuteUser(userId, voiceState.guildId || newItem.guildId); } else { serverDeafenUser(userId, voiceState.guildId || newItem.guildId); } } } } /** If the item's user is currently in a matching voice channel, start */ function tryStartIfInVoice(item: MoveItem) { if ( (!item.enabled && !item.serverMute && !item.serverDeafen) || activeMoves.has(item.userId) ) return; const voiceState = VoiceStateStore.getVoiceStateForUser(item.userId); if (!voiceState?.channelId) return; // guildId is optional on VoiceState — only block on explicit mismatch if ( item.guildId !== "*" && voiceState.guildId && voiceState.guildId !== item.guildId ) return; startMoving(item); } const UserContextMenuPatch: NavContextMenuPatchCallback = ( children, { user, guildId }: UserContextProps, ) => { if (!user) return; const list = getMoveList(); const existing = list.find( (i) => i.userId === user.id && i.guildId === (guildId ?? "*"), ); const moveChecked = existing ? existing.enabled && !existing.disconnectOnJoin : false; const disconnectChecked = existing ? existing.enabled && existing.disconnectOnJoin : false; const muteChecked = existing ? existing.serverMute : false; const deafenChecked = existing ? existing.serverDeafen : false; children.push( toggleUserInList(user.id, guildId, false)} />, toggleUserInList(user.id, guildId, true)} />, toggleServerAction(user.id, guildId, "serverMute")} />, toggleServerAction(user.id, guildId, "serverDeafen")} />, ); }; // --------------------------------------------------------------------------- // Settings UI Component // --------------------------------------------------------------------------- function MoveListManager() { const [moveList, setMoveListState] = React.useState(getMoveList()); const [newUserId, setNewUserId] = React.useState(""); const [newGuildId, setNewGuildId] = React.useState("*"); const [newDisconnect, setNewDisconnect] = React.useState(false); const [newDelay, setNewDelay] = React.useState("2000"); const [newServerMute, setNewServerMute] = React.useState(false); const [newServerDeafen, setNewServerDeafen] = React.useState(false); const [newSendDm, setNewSendDm] = React.useState(false); const [newDmMessage, setNewDmMessage] = React.useState(""); const guilds = Object.values(GuildStore.getGuilds()) as any[]; const addEntry = () => { if (!newUserId.trim()) return; const newItem: MoveItem = { id: Date.now().toString(), userId: newUserId.trim(), guildId: newGuildId, enabled: true, disconnectOnJoin: newDisconnect, delay: parseInt(newDelay, 10) || 2000, serverMute: newServerMute, serverDeafen: newServerDeafen, sendDm: newSendDm, dmMessage: newDmMessage, }; const newList = [...moveList, newItem]; setMoveListState(newList); setMoveList(newList); setNewUserId(""); setNewDmMessage(""); setNewSendDm(false); setNewDisconnect(false); setNewServerMute(false); setNewServerDeafen(false); setNewDelay("2000"); }; const removeEntry = (id: string) => { const entry = moveList.find((i) => i.id === id); if (entry) stopMoving(entry.userId); const newList = moveList.filter((i) => i.id !== id); setMoveListState(newList); setMoveList(newList); }; const toggleEnabled = (id: string) => { const newList = moveList.map((i) => { if (i.id === id) { const updated = { ...i, enabled: !i.enabled }; if (!updated.enabled) { stopMoving(i.userId); } else { // Restart if user is already in VC tryStartIfInVoice(updated); } return updated; } return i; }); setMoveListState(newList); setMoveList(newList); }; const toggleDisconnect = (id: string) => { const newList = moveList.map((i) => { if (i.id === id) { // If toggling disconnect on, also enable and stop any move loop if (!i.disconnectOnJoin) { stopMoving(i.userId); const updated = { ...i, enabled: true, disconnectOnJoin: true, }; // Disconnect immediately if user is in VC tryStartIfInVoice(updated); return updated; } // Toggling disconnect off → switch to move mode stopMoving(i.userId); const updated = { ...i, disconnectOnJoin: false }; // Restart move loop if user is in VC tryStartIfInVoice(updated); return updated; } return i; }); setMoveListState(newList); setMoveList(newList); }; const toggleSendDm = (id: string) => { const newList = moveList.map((i) => i.id === id ? { ...i, sendDm: !i.sendDm } : i, ); setMoveListState(newList); setMoveList(newList); }; const toggleServerMute = (id: string) => { const newList = moveList.map((i) => { if (i.id === id) { const updated = { ...i, serverMute: !i.serverMute }; // Apply immediately if user is in VC if (updated.serverMute) { const voiceState = VoiceStateStore.getVoiceStateForUser( i.userId, ); if ( voiceState?.channelId && (i.guildId === "*" || voiceState.guildId === i.guildId) ) { serverMuteUser( i.userId, voiceState.guildId || i.guildId, ); } } return updated; } return i; }); setMoveListState(newList); setMoveList(newList); }; const toggleServerDeafen = (id: string) => { const newList = moveList.map((i) => { if (i.id === id) { const updated = { ...i, serverDeafen: !i.serverDeafen }; // Apply immediately if user is in VC if (updated.serverDeafen) { const voiceState = VoiceStateStore.getVoiceStateForUser( i.userId, ); if ( voiceState?.channelId && (i.guildId === "*" || voiceState.guildId === i.guildId) ) { serverDeafenUser( i.userId, voiceState.guildId || i.guildId, ); } } return updated; } return i; }); setMoveListState(newList); setMoveList(newList); }; const updateDmMessage = (id: string, message: string) => { const newList = moveList.map((i) => i.id === id ? { ...i, dmMessage: message } : i, ); setMoveListState(newList); setMoveList(newList); }; const updateDelay = (id: string, delayStr: string) => { const delay = parseInt(delayStr, 10) || 2000; const newList = moveList.map((i) => i.id === id ? { ...i, delay } : i, ); setMoveListState(newList); setMoveList(newList); }; const getUserDisplay = (userId: string) => { const user = UserStore.getUser(userId); return user ? `${user.username} (${userId})` : userId; }; const getGuildDisplay = (guildId: string) => { if (guildId === "*") return "All Servers"; const guild = GuildStore.getGuild(guildId); return guild ? guild.name : guildId; }; return (

Add New Entry

User ID
Server
setNewDisconnect(e.target.checked)} /> Auto Disconnect (instead of auto-move)
{!newDisconnect && (
Delay between moves (ms)
)}
{newSendDm && ( )}

Move List ({moveList.length})

{moveList.length === 0 ? (
No entries configured
) : ( moveList.map((item) => (
User:{" "} {getUserDisplay(item.userId)}
Server:{" "} {getGuildDisplay(item.guildId)}
Mode:{" "} {item.disconnectOnJoin ? "Disconnect" : "Move"} {!item.disconnectOnJoin && ` (${item.delay}ms)`} {item.serverMute && " 🔇Mute"} {item.serverDeafen && " 🔇Deafen"}
{!item.disconnectOnJoin && (
Delay (ms): updateDelay(item.id, v) } />
)} {item.sendDm && (
updateDmMessage(item.id, v) } />
)}
)) )} ); } // --------------------------------------------------------------------------- // Toolbar Button // --------------------------------------------------------------------------- function AutoMoverButton() { return ( SettingsRouter.open("VencordPlugins")} tooltip="autoMover Settings" icon={() => ( )} /> ); } // --------------------------------------------------------------------------- // Plugin definition // --------------------------------------------------------------------------- export default definePlugin({ name: "autoMover", description: "Automatically moves target users randomly between voice channels when they connect, or disconnects them", authors: [{ name: "You", id: 0n }], settings, patches: [ { // Inject button into the toolbar next to inbox find: "toolbar:function", replacement: { match: /(function \i\(\i\){var \i=\i\.className.*?children):\[(\i\.useMemo[^}]+"div",{className:\i\(\).*?}\),)/, replace: "$1:[$self.ToolbarButton(),$2", }, }, ], ToolbarButton: ErrorBoundary.wrap(AutoMoverButton, { noop: true }), contextMenus: { "user-context": UserContextMenuPatch, }, flux: { VOICE_STATE_UPDATES({ voiceStates }: { voiceStates: any[] }) { const moveList = getMoveList(); if (moveList.length === 0) return; for (const state of voiceStates) { const { userId, channelId, guildId } = state; if (!channelId) continue; for (const item of moveList) { if (!item.enabled && !item.serverMute && !item.serverDeafen) continue; if (item.userId !== userId) continue; if (item.guildId !== "*" && item.guildId !== guildId) continue; // Already moving this user — skip if (activeMoves.has(userId)) continue; startMoving(item); } } }, }, start() { console.log("autoMover started"); }, stop() { for (const userId of activeMoves.keys()) { stopMoving(userId); } console.log("autoMover stopped"); }, });