In short: I have been trying to make some UI in electron (electron-react-boilerplate) for my discord bot. The bot should connect to voice channel and on command, play some mp3 files into the mic. I've made it work in development, but not in production. Whenether i run npm run package, it packages the app, and exactly when the connection is established (bot actually connects and is in the channel), I cannot attach the ".on('event', fn)" to this connection.
The original error states: Cannot read properties of undefined (reading 'handle') at Object.t [as VOICE_STATE_UPDATE]
https://i.stack.imgur.com/BLZl3.jpg
Here's the snipper of connection function:
public static async joinVoice(guildId: string, id: string) {
if (this.currentVoiceConnection) {
this.currentVoiceConnection.destroy();
}
constguild = this.client.guilds.cache.get(guildId);
if (!guild) {
throw new Error('This channel does not exist.');
}
const channel = guild.channels.cache?.get(id);
if (!channel) {
throw new Error('This channel does not exist.');
}
// connection is created here.
// bot actually connects and is in the channel!
const connection = joinVoiceChannel({
channelId: channel.id.toString(),
guildId: guild.id.toString(),
adapterCreator: channel.guild.voiceAdapterCreator,
});
// does get here after initializing connection
connection.once(VoiceConnectionStatus.Ready, () => {
// DOES NOT ENTER HERE
console.log('Connected to voice! ');
connection.subscribe(this.audioPlayer);
this.audioPlayer.on('error', (err) => {
console.log(err);
});
});
connection.on('stateChange', (prev, next) => {
// DOES NOT ENTER HERE
console.log(prev, next);
});
connection.on(VoiceConnectionStatus.Disconnected, async () => {
// DOES NOT ENTER HERE
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
connection.removeAllListeners();
connection.destroy();
}
});
this.currentVoiceConnection = connection;
return true;
}
This is what the connection varible is in the production debug console:
VoiceConnection {
_events: [Object: null prototype] {}, // <<< EMPTY??
_eventsCount: 0,
_maxListeners: undefined,
rejoinAttempts: 0,
_state: {
status: 'signalling',
adapter: {
sendPayload: [Function: sendPayload],
destroy: [Function: destroy]
}
},
joinConfig: {
selfDeaf: true,
selfMute: false,
group: 'default',
channelId: '946026122380972076',
guildId: '946026122380972072',
adapterCreator: [Function (anonymous)]
},
packets: { server: undefined, state: undefined },
receiver: VoiceReceiver {
voiceConnection: [Circular *1],
ssrcMap: SSRCMap {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
map: Map(0) {},
[Symbol(kCapture)]: false
},
subscriptions: Map(0) {},
connectionData: {},
speaking: SpeakingMap {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
users: Map(0) {},
speakingTimeouts: Map(0) {},
[Symbol(kCapture)]: false
},
onWsPacket: [Function: bound onWsPacket],
onUdpMessage: [Function: bound onUdpMessage]
},
debug: null,
onNetworkingClose: [Function: bound onNetworkingClose],
onNetworkingStateChange: [Function: bound onNetworkingStateChange],
onNetworkingError: [Function: bound onNetworkingError],
onNetworkingDebug: [Function: bound onNetworkingDebug],
[Symbol(kCapture)]: false
I've been banging my head about this problem for a couple of days now, never have I ever wrote a question on stackoverflow, and this is my last option. What am i missing here?
I've tried different recommended packages for @discord/voice.
I've tried migrating to a fresh electron-react-boilerplate project, with all of my files.
I've did install ffmpeg for discord audio.
I've tried getting voice connection with getVoiceConnection.