Discord.js 当用户加入语音频道时,向特定频道发送消息不起作用
已经查找了stackoverflow上的所有问题,但没有任何效果。 如果有人加入特定的语音频道,我希望我的机器人发送消息Discord.js 当用户加入语音频道时,向特定频道发送消息不起作用,discord.js,Discord.js,已经查找了stackoverflow上的所有问题,但没有任何效果。 如果有人加入特定的语音频道,我希望我的机器人发送消息 client.on('voiceStateUpdate', (oldMember, newMember) => { // Here I'm storing the IDs of their voice channels, if available const oldChannel = oldMember.voiceChannel ? oldMember.voic
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.`);
}
不起作用。没有错误,但它不起作用。请帮助我,我认为这应该是可行的:
client.on('voiceStateUpdate',(旧成员,新成员)=>{
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})已加入频道`)
}else if(oldUserChannel==='VOICE CHANNEL ID'&&newUserChannel!=='VOICE CHANNEL ID'){
textChannel.send(`${newMember.user.username}(${newMember.id})已离开频道`)
}
})
虽然这可能会解决OP的问题,但添加一些解释说明为什么代码有效,为什么OPs代码无效,这将真正提高这个答案的价值。