猿问

如何检测成员何时加入和离开语音频道

我正在开发一个不和谐的机器人,它需要检测用户何时加入和离开频道。我已经在第 4 行尝试过这个,因为如果用户的连接没有改变,它就不会运行。但这是行不通的。这是我的整个代码部分。


client.on('voiceStateUpdate', (oldState, newState) => {

// check for bot

if (oldState.member.user.bot) return;

if (oldState.member.voice === newState.member.voice) return;//<- here

client.channels.cache.get('777782275004825640').send(`${oldState.member} joined`);

});

我也尝试过.connection但它不适用于我当前的设置。任何帮助都会很棒!


幕布斯6054654
浏览 106回答 2
2回答

万千封印

我找到了!经过一些测试,我发现当频道更新时,会发生以下四种情况之一。如果用户加入则oldState.channelID返回null如果用户返回newState.channelIDnull如果用户将自己静音或被震聋,则oldState.channelID等于newState.channelID如果用户移动的频道&nbsp;oldState.channelID不等于newState.channelID我Discord.Client()的设置为client,所以如果你的不同,请更改client.on('voiceStateUpdate', (oldState, newState) => {&nbsp; &nbsp; if(oldState.channelID === newState.channelID) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('a user has not moved!')&nbsp; &nbsp; }&nbsp; &nbsp; if(oldState.channelID != null && newState.channelID != null && newState.channelID != oldState.channelID) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('a user switched channels')&nbsp; &nbsp; }&nbsp; &nbsp; if(oldState.channelID === null) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('a user joined!')&nbsp; &nbsp; }&nbsp; &nbsp; if (newState.channelID === null) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('a user left!')&nbsp; &nbsp; }});

慕慕森

尝试这个片段,请让我知道它对你有什么作用。let oldChannel = oldState.voiceChannel;let newChannel = newState.voiceChannel;if(oldChannel === undefined && newChannel !== undefined) {&nbsp; // User has joined a channel} else if(newChannel === undefined) {&nbsp; // User has left a channel}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答