我正在开发一个不和谐的机器人,它需要检测用户何时加入&离开一个通道。我在第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,但是它不适用于我当前的设置。任何帮助都会很好!
发布于 2020-11-19 16:01:06
我找到了!经过一些测试后,我发现当一个通道被更新时,会发生四种情况之一。
如果用户加入newState.channelID
oldState.channelID
等于oldState.channelID
,用户移动通道oldState.channelID
不等于newState.channelID
我的Discord.Client()
设置为client
,所以如果您的更改不同
client.on('voiceStateUpdate', (oldState, newState) => {
if(oldState.channelID === newState.channelID) {
console.log('a user has not moved!')
}
if(oldState.channelID != null && newState.channelID != null && newState.channelID != oldState.channelID) {
console.log('a user switched channels')
}
if(oldState.channelID === null) {
console.log('a user joined!')
}
if (newState.channelID === null) {
console.log('a user left!')
}
});
发布于 2020-11-19 00:12:19
试试这个片段,请让我知道它是如何为你工作的。
let oldChannel = oldState.voiceChannel;
let newChannel = newState.voiceChannel;
if(oldChannel === undefined && newChannel !== undefined) {
// User has joined a channel
} else if(newChannel === undefined) {
// User has left a channel
}
https://stackoverflow.com/questions/64906261
复制