[英]How to detect when a user leaves a voice channel in Discord with discord.js V12
我正在 v12 上制作一个 Discord.js Bot,其中包含一个静音命令,可以将您所在的整个语音频道静音。问题是当有人离开频道时,他们会保持静音。 我试图用一个简单的事件来解决这个问题来取消静音,但我不明白VoiceStateUpdate
和OldState
和NewState
。 我找了很多遍,但我只能找到一个加入VC,而不是离开。 这是我到目前为止所得到的:
静音命令:
else if (command === 'mute') {
message.delete()
if (!message.member.roles.cache.has('') && !message.member.roles.cache.has('')) {
message.reply('You don\'t have permission to use this commmand!')
.then(message => {
message.delete({ timeout: 5000 })
}).catch();
return;
}
if (message.member.voice.channel) {
let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
for (const [memberID, member] of channel.members) {
member.voice.setMute(true);
}
} else {
message.reply('You need to join a voice channel first!')
.then(message => {
message.delete({ timeout: 5000 })
}).catch();
}
}
取消静音事件:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.member.user.bot) return;
if (oldState.member.user !== newState.member.user) member.voice.setMute(false);
});
感谢您抽出宝贵时间帮助我! :)
好吧,你已经在正确的轨道上。 您应该做的是检查某人在离开语音频道时是否被静音,然后取消静音。
所以让我们开始吧。
首先,我们检查此人是否正在离开语音通道。 这很重要,因为每次有人对他们的声音做任何事情时都会触发voiceStateUpdate
事件,例如静音或加入。 我们通过检查oldState.channelID
是否为null
或undefined
来做到这一点,因为这表示加入。
if (oldState.channelID === null || typeof oldState.channelID == 'undefined') return;
接下来我们需要检查离开的人是否被静音,如果没有则返回。
if (!oldState.member.voice.mute) return;
最后我们移除静音。
oldState.member.voice.setMute(false);
所以你的整个voiceStateUpdate
应该看起来像这样。
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelID === null || typeof oldState.channelID == 'undefined') return;
if (!oldState.member.voice.mute) return;
oldState.member.voice.setMute(false);
});
根据这个链接(我还没有测试过)你需要这样的东西:
const Discord = require("discord.js")
const bot = new Discord.Client()
bot.login('*token*')
bot.on('voiceStateUpdate', (oldState, newState) => {
let newUserChannel = newState.voiceChannel
let oldUserChannel = oldState.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
} else if(newUserChannel === undefined){
// User leaves a voice channel
}
})
client.on('voiceStateUpdate', (oldMember, newMember) => {
const oldUserChannel = oldMember.voice.channelID
const newUserChannel = newMember.voice.channelID
if (oldUserChannel === 'MUTED CHANNEL ID' && newUserChannel !== 'MUTED CHANNEL ID') {
newMember.voice.setMute(false);
}
});
您可以在 json 文件中写入静音频道的 id 并从那里获取它,我想不出其他方法,抱歉 :(
您无法操纵不在语音通道上的人的语音状态,这会导致DiscordAPIError: Target user is not connected to voice.
,即使你可以,你可能仍然不想,因为即使你没有得到某种未处理的承诺错误,你可能仍然会通过检查新状态的通道是否为空来获得无限循环,因为一旦您取消静音该人等,它就会用空通道触发另一个事件。
因此,我现在看到的方式是解决您的问题的可能方法是在用户加入频道时取消静音。 如果有人能找到一种更好的方法来检查它是否是一个正在发生的通道,而不仅仅是使用newState.channel === null
,那么可能没有必要采用这种方式。 无论如何,如果您可以在加入时取消静音,您可以这样做:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channel === null && newState.channel !== null) {
newState.setMute(false);
}
});
请注意,如果有人在离开后恰好加入频道,这可能会出错,否则,您应该没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.