chebro / discord-voice-recorder

A nifty script that can pipe out audio from a discord voice channel to your local storage. A discord voice recorder!
MIT License
111 stars 37 forks source link

voiceChannel.leave() : TypeError: Cannot read property 'leave' of null #20

Open astariul opened 3 years ago

astariul commented 3 years ago

When leaving a channel, I'm meeting the following error :

C:\Users\remon\Documents\workspace\discord-voice-recorder\bin\commands.js:48 voiceChannel.leave(); ^

TypeError: Cannot read property 'leave' of null at Object.exports.exit (C:\Users\remon\Documents\workspace\discord-voice-recorder\bin\commands.js:48:18) at Client. (C:\Users\remon\Documents\workspace\discord-voice-recorder\index.js:22:51) at Client.emit (events.js:376:20) at MessageCreateAction.handle (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31) at WebSocketShard.onPacket (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10) at WebSocket.onMessage (C:\Users\remon\Documents\workspace\discord-voice-recorder\node_modules\ws\lib\event-target.js:132:16) at WebSocket.emit (events.js:376:20)

Any idea where it comes from and how to fix it ?

astariul commented 3 years ago

Note : it happens only when the use leave the channel first.

When the user stay in the channel and send the command, it work properly.

To reproduce :

-> Cmd "enter" -> Connect to discord voice channel -> Say something -> Disconnect from voice channel -> Cmd "exit"

astariul commented 3 years ago

Ok I changed :

const { channel: voiceChannel, connection: conn } = msg.guild.voiceStates.cache.last();

into

    var { channel: voiceChannel, connection: conn } = msg.guild.voiceStates.cache.last();
    if (voiceChannel == null){
        var { channel: voiceChannel, connection: conn } = msg.guild.voiceStates.cache.first();
    }

and it seems to work. I don't know enough javascript to find the actual solution, but this dirty work-around is fine for my usage.

astariul commented 3 years ago

Turned out my fix does not work all the time...