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

如何用Python开发Discord Bot:特定用户加入指定语音频道时触发提醒

实现特定成员加入指定语音频道的提醒功能

以下是基于Discord.js v14的实现方案,直接整合到你的Bot代码即可:

1. 确认依赖

确保已安装最新版Discord.js:

npm install discord.js@latest

2. 核心代码实现

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildVoiceStates // 必须开启此意图才能监听语音状态变化
  ]
});

// 替换为你实际的ID值
const TARGET_VOICE_CHANNEL_ID = '目标语音频道ID';
const MONITORED_USER_ID = '需要监控的成员ID';
const NOTIFICATION_CHANNEL_ID = '通知用文本频道ID';
const YOUR_USER_ID = '你的用户ID';

client.on('ready', () => {
  console.log(`Bot已登录:${client.user.tag}`);
});

client.on('voiceStateUpdate', async (oldState, newState) => {
  // 判断用户是否从非目标频道(或无频道)加入了指定语音频道,且是监控对象
  if (
    oldState.channelId !== TARGET_VOICE_CHANNEL_ID &&
    newState.channelId === TARGET_VOICE_CHANNEL_ID &&
    newState.member.id === MONITORED_USER_ID
  ) {
    // 方式1:在指定文本频道@你发送提醒
    const notifyChannel = client.channels.cache.get(NOTIFICATION_CHANNEL_ID);
    if (notifyChannel) {
      await notifyChannel.send(`<@${YOUR_USER_ID}> 监控成员 <@${MONITORED_USER_ID}> 加入了语音频道 <#${TARGET_VOICE_CHANNEL_ID}>`);
    }

    // 方式2:给你发送私信(可二选一或同时保留)
    const you = await client.users.fetch(YOUR_USER_ID);
    if (you) {
      await you.send(`监控成员 <@${MONITORED_USER_ID}> 加入了语音频道 <#${TARGET_VOICE_CHANNEL_ID}>`);
    }
  }
});

// 替换为你的Bot令牌
client.login('你的Bot登录令牌');

关键注意事项

  • 必须开启GuildVoiceStates意图,否则Bot无法捕获语音状态变化事件
  • 确保Bot拥有对应权限:
    • 能查看目标语音频道
    • 能在通知文本频道发送消息
    • 能给你发送私信(若使用私信方式)
  • 所有ID需替换为实际字符串,Discord ID为字符串格式,不要用数字类型

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 23:11:14