如何在discord.js中检测用户加入/离开舞台频道?
Discord.js监听舞台频道用户进出的解决方案
- 可以通过
voiceStateUpdate事件实现监听,舞台频道属于语音频道的特殊类型,只需在事件中判断频道类型即可区分普通语音频道与舞台频道。 guildAuditLogEntryCreate事件不适用此场景,该事件仅触发服务器管理类操作(如频道创建、权限修改),不会响应用户进出频道的行为。
代码示例(Discord.js v13/v14)
client.on('voiceStateUpdate', (oldState, newState) => { // 用户加入舞台频道 if (!oldState.channel && newState.channel?.type === 'GUILD_STAGE_VOICE') { console.log(`${newState.member.user.tag} 加入舞台频道:${newState.channel.name}`); } // 用户离开舞台频道 if (oldState.channel?.type === 'GUILD_STAGE_VOICE' && !newState.channel) { console.log(`${oldState.member.user.tag} 离开舞台频道:${oldState.channel.name}`); } // 用户从其他频道切换至舞台频道 if (oldState.channel && newState.channel?.type === 'GUILD_STAGE_VOICE' && oldState.channel.id !== newState.channel.id) { console.log(`${newState.member.user.tag} 切换到舞台频道:${newState.channel.name}`); } });
注意事项
- 需使用Discord.js v13及以上版本,旧版本(如v12)需通过
channel.isStage()方法判断是否为舞台频道。 - 机器人需拥有
View Channel和Voice States相关权限,且必须处于目标服务器内。
内容的提问来源于stack exchange,提问作者Fathi Helmi
相关产品推荐
相关产品推荐

