I’m trying to create a discord bot in javascript that plays a specified sound for each specific person that joins the voice channel. Kind of like an intro sound/song thing.
I’m getting this error when I tried to test it out. It outputs this in the terminal when I join the voice channel and the bot doesn’t join the voice channel to play the sound.
PS C:javacodeDiscord Audio Bot> node Audio.js
On
C:javacodeDiscord Audio BotAudio.js:29
} else if (newState.channel.members.size = 1) {
^
TypeError: Cannot read properties of null (reading 'members')
at Client.<anonymous> (C:javacodeDiscord Audio BotAudio.js:29:31)
at Client.emit (node:events:513:28)
at VoiceStateUpdate.handle (C:javacodeDiscord Audio Botnode_modulesdiscord.jssrcclientactionsVoiceStateUpdate.js:38:14)
at module.exports [as VOICE_STATE_UPDATE] (C:javacodeDiscord Audio Botnode_modulesdiscord.jssrcclientwebsockethandlersVOICE_STATE_UPDATE.js:4:35)
at WebSocketManager.handlePacket (C:javacodeDiscord Audio Botnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:352:31)
at WebSocketShard.onPacket (C:javacodeDiscord Audio Botnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:489:22)
at WebSocketShard.onMessage (C:javacodeDiscord Audio Botnode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:328:10)
at callListener (C:javacodeDiscord Audio Botnode_moduleswslibevent-target.js:290:14)
at WebSocket.onMessage (C:javacodeDiscord Audio Botnode_moduleswslibevent-target.js:209:9)
at WebSocket.emit (node:events:513:28)
This is my code below. Also, how would I go about doing this so that each user has their own different sound?
console.log('On');
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMembers
]
});
client.on('voiceStateUpdate', (_oldState, newState) => {
// Check if the user who joined is the specific user we want
if (newState.member.id === 'STtheGamer') {
// Check if the user joined a voice channel
if (newState.channel) {
// Join the voice channel and play the specific sound
newState.channel.join()
.then(connection => {
// Replace 'song.mp3' with the path to the audio file you want to play
const dispatcher = connection.play('C:UsersstorrMusicbotsoundsswtheme.mp3');
// Leave the voice channel after the sound is played
dispatcher.on('finish', () => {
newState.channel.leave();
});
})
.catch(console.error);
}
} else if (newState.channel.members.size = 1) {
if (newState.channel) {
newState.channel.leave()
.catch(console.error);
}
}
});
client.login('botkey here');