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

Discord Bot监听用户加入指定频道发消息却误触发静音事件如何解决

问题原因
  • voiceStateUpdate 事件会在用户任意语音状态变更时触发,除了进出语音频道的场景外,用户静音/取消静音麦克风、开关摄像头、切换共享屏幕状态等操作都会触发该事件。
  • 现有代码仅校验了新状态下用户所在频道ID是否匹配目标ID,未排除用户原本就处于目标频道内的场景,所以用户在频道内修改静音状态触发事件时,会命中现有判断逻辑发送重复消息。
修复方案

调整判断逻辑,仅当用户从非目标频道(或未加入任何频道)进入目标语音频道时才发送通知,修改后的代码如下:

client.on('voiceStateUpdate', (oldState, newState) => {
    const targetVoiceChannelId = "这里填目标语音频道ID";
    const notifyChannelId = "这里填发送通知的文字频道ID";

    const oldChannelId = oldState.channelId;
    const newChannelId = newState.channelId;
    const notifyChannel = client.channels.cache.get(notifyChannelId);

    // 仅新加入目标频道时触发
    if(newChannelId === targetVoiceChannelId && oldChannelId !== targetVoiceChannelId) {
        if(notifyChannel) notifyChannel.send("你的通知消息内容");
    }
});
补充说明
  • 若你使用的是 Discord.js v12 及以上版本,原代码中的channelID属性已更名为channelId,注意对应调整避免取值异常。
  • 新增的通知频道存在性校验,可以避免频道缓存未命中时程序抛出报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 10:54:04