bco2135
bco2135
DIAdiscord.js - Imagine an app
Created by bco2135 on 3/5/2024 in #djs-voice
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");
}
},
6 replies