当用户加入语音频道时,向特定频道发送消息是不起作用的



已经在stackoverflow上查找了所有问题,但都没有成功。如果有人加入特定的语音频道,我希望我的机器人发送消息。

client.on('voiceStateUpdate', (oldMember, newMember) => {
// Here I'm storing the IDs of their voice channels, if available
const oldChannel = oldMember.voiceChannel ? oldMember.voiceChannel.id : null;
const newChannel = newMember.voiceChannel ? newMember.voiceChannel.id : null;
if (oldChannel == newChannel) return; // If there has been no change, exit
// Here I'm getting the client's channel (client.voiceChannel does not exist)
const clientMember = oldMember.guild.member(client.user),
clientChannel = clientMember ? clientMember.voiceChannel.id : null;
// Here I'm getting the channel, just replace VVV this VVV with the channel's ID
const textChannel = oldMember.guild.channels.get('765520462439645191');
if (!textChannel) throw new Error("That channel does not exist.");
// Here I don't need to check if they're the same, since it would've exit before
if (newChannel == clientChannel) {
// console.log("A user joined.");
textChannel.send(`${newMember} has joined the voice channel.`);
} else if (oldChannel == clientChannel) {
// console.log("A user left.");
textChannel.send(`${newMember} has left the voice channel.`);
}

不起作用。没有错误,但它不起作用。请帮助ty

我认为这应该有效:

client.on('voiceStateUpdate', (oldMember, newMember) => {
const newUserChannel = newMember.voice.channelID
const oldUserChannel = oldMember.voice.channelID
const textChannel = message.guild.channels.cache.get('TEXT CHANNEL ID')
if(newUserChannel === 'VOICE CHANNEL ID') {
textChannel.send(`${newMember.user.username} (${newMember.id}) has joined the channel`)
} else if (oldUserChannel === 'VOICE CHANNEL ID' && newUserChannel !== 'VOICE CHANNEL ID') {
textChannel.send(`${newMember.user.username} (${newMember.id}) has left the channel`)
}
})

最新更新