You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord语音状态监听代码异常:意图监听Rythm机器人语音行为却对所有用户生效且输出异常

Fixing Your Discord.js Voice State Update Logic

Let's break down the core issues in your code and walk through the corrected version step by step:

Identified Problems

  • Incorrect Equality Check: You used the assignment operator = instead of the strict equality operator === when verifying the bot's ID. The line newMember.id = "235088799074484224" doesn't check if the state change belongs to Rythm—it tries to overwrite the ID (which won't work, since id is read-only) and always returns true, so every voice state update triggers your logic.
  • Hardcoded Channel Values: You directly set newUserChannel and oldUserChannel to your target VC ID instead of fetching the actual channel ID from the voice state object. This means your code never checks where the bot actually moved, so it always logs "Joined VC1" regardless of the real action.
  • Missing State Change Validation: Your original code doesn't distinguish between joining, leaving, or switching channels—you need to compare the old and new voice states to know what action happened.

Corrected Code (Discord.js v13+)

This version targets modern Discord.js versions, uses proper state checks, and only triggers logic for Rythm:

client.on('voiceStateUpdate', (oldState, newState) => {
  // Define constants for easy maintenance
  const RYTHM_BOT_ID = '235088799074484224';
  const TARGET_VC_ID = '844108228353851392';

  // Exit early if the state change isn't from Rythm
  if (newState.id !== RYTHM_BOT_ID) return;

  // Get the actual channel IDs from the state objects
  const oldChannelId = oldState.channelId;
  const newChannelId = newState.channelId;

  // Check if Rythm joined the target VC (from being disconnected)
  if (!oldChannelId && newChannelId === TARGET_VC_ID) {
    console.log("Joined VC1");
  }
  // Check if Rythm left the target VC (to being disconnected)
  else if (oldChannelId === TARGET_VC_ID && !newChannelId) {
    console.log("Left VC1");
  }
  // Optional: Handle switching from another VC to the target
  else if (oldChannelId !== TARGET_VC_ID && newChannelId === TARGET_VC_ID) {
    console.log("Switched to VC1 from another channel");
  }
});

Corrected Code (Discord.js v12)

If you're using an older v12 version, adjust to access the channel ID from the channel object:

client.on('voiceStateUpdate', (oldState, newState) => {
  const RYTHM_BOT_ID = '235088799074484224';
  const TARGET_VC_ID = '844108228353851392';

  // Check if the member is Rythm
  if (newState.member.id !== RYTHM_BOT_ID) return;

  // Safely get channel IDs (handle null if not in a channel)
  const oldChannelId = oldState.channel ? oldState.channel.id : null;
  const newChannelId = newState.channel ? newState.channel.id : null;

  if (!oldChannelId && newChannelId === TARGET_VC_ID) {
    console.log("Joined VC1");
  } else if (oldChannelId === TARGET_VC_ID && !newChannelId) {
    console.log("Left VC1");
  } else if (oldChannelId !== TARGET_VC_ID && newChannelId === TARGET_VC_ID) {
    console.log("Switched to VC1 from another channel");
  }
});

Key Improvements

  • Early Exit: We immediately skip logic if the state change isn't from Rythm, making the code more efficient.
  • Dynamic Channel Checks: We pull the actual channel IDs from the voice state objects instead of hardcoding them.
  • Clear State Comparisons: We explicitly check different scenarios (joining from disconnect, leaving to disconnect, switching channels) to log accurate messages.

内容的提问来源于stack exchange,提问作者Satyam Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:33:10