Maintaining connection after restarting node

I have a bot that would connect to a channel and play sounds. I have a slash command that would play one of the few sound files I have, it works fine but when I restart node it would stay in the channel but if I try doing anything it would say that the bot isn't in a voice channel and I would have to reconnect the bot to the voice channel. I get the connection using
const connection = getVoiceConnection(interaction.guild.id);
const connection = getVoiceConnection(interaction.guild.id);
though it would still return undefined even if the bot is in the channel (This is if I restarted node when the bot was already in a voice channel) Is there a way to keep the connection even if you restart node, because I sometimes have problems when the bot is on multiple servers and I restart node. Here's the full code of the command execute function
async execute(interaction) {
try {
const SoundPath = interaction.options.getString("sound-effect");
const SoundResource = createAudioResource(SoundPath);

const connection = getVoiceConnection(interaction.guild.id);

console.log(`${connection} + ${interaction.guild.id}`);

if (!connection) {
console.log("Played Sound: Bot is not in a Voice Chanel");
interaction.reply("Could not play sound effect");
return;
}

const audioPlayer = createAudioPlayer();

const subscription = connection.subscribe(audioPlayer);

audioPlayer.play(SoundResource);

if (subscription) {
setTimeout(() => {
audioPlayer.stop();
subscription.unsubscribe(audioPlayer);
}, 60_000);
}

interaction.reply({
content: "Played sound effect",
empheral: true,
});
} catch (e) {
console.log(`Error, while playing sound effect`);
interaction.reply("A error occured");
}
},
async execute(interaction) {
try {
const SoundPath = interaction.options.getString("sound-effect");
const SoundResource = createAudioResource(SoundPath);

const connection = getVoiceConnection(interaction.guild.id);

console.log(`${connection} + ${interaction.guild.id}`);

if (!connection) {
console.log("Played Sound: Bot is not in a Voice Chanel");
interaction.reply("Could not play sound effect");
return;
}

const audioPlayer = createAudioPlayer();

const subscription = connection.subscribe(audioPlayer);

audioPlayer.play(SoundResource);

if (subscription) {
setTimeout(() => {
audioPlayer.stop();
subscription.unsubscribe(audioPlayer);
}, 60_000);
}

interaction.reply({
content: "Played sound effect",
empheral: true,
});
} catch (e) {
console.log(`Error, while playing sound effect`);
interaction.reply("A error occured");
}
},
3 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
bco2135
bco2135OP9mo ago
Node is version v21.6.0
ThePedroo
ThePedroo9mo ago
No, there's not Unless you host it somewhere else It's impossible, as TCP of the other side will detect the connection is dead
Want results from more Discord servers?
Add your server