我制作了一个 Discord 机器人,用于在有人加入特定频道时发送消息,但它也会在用户将麦克风静音时发送消息。怎么了?
您可以在下面找到我当前的代码:
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.channelID;
let oldUserChannel = oldMember.channelID;
const channel2 = client.channels.cache.get('channelid here');
if(newUserChannel === "channelid here")
{
// User Joins a voice channel
channel2.send("message");
}
});
答案 0 :(得分:2)
您还应该检查频道是否发生了变化,因此请检查 newMember.channelID
是否与 oldMember.channelID
不同:
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.channelID;
let oldUserChannel = oldMember.channelID;
const channel2 = client.channels.cache.get('channelid here');
if (
newUserChannel !== oldUserChannel &&
newUserChannel === 'channelid here'
) {
// User Joins a voice channel
channel2.send('message');
}
});