Play audio problem

Hello, I have a problem with the "@discordjs/voice" package, and the problem is in playing the sound. Well, to understand the problem, let's assume that there are two servers, the first is called Server 1 and the second is called Server 2. When I play songs on Server 1, the same song will play on Server 2. What is the solution to this problem?
49 Replies
d.js toolkit
d.js toolkitā€¢4w ago
- What are your intents? GuildVoiceStates is required to receive voice data! - Show what dependencies you are using -- generateDependencyReport() is exported from @discordjs/voice. - Try looking at common examples: https://github.com/discordjs/voice-examples. - 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 staff
duck
duckā€¢4w ago
please also share your code exactly as it is when it produces this behavior continuing from https://discord.com/channels/222078108977594368/824411059443204127/1302027616110248066 , both snippets should be erroring before even playing any audio
d.js docs
d.js docsā€¢4w ago
To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.
duck
duckā€¢4w ago
side note: it's heavily recommended to consolidate your event listeners into one and delegate from there
John Snos
John SnosOPā€¢4w ago
client.on("voiceStateUpdate", async (oldState, newState) => {
const player = createAudioPlayer();
const folderPath = path.join(__dirname, 'data');
const filePath = path.join(folderPath, `server_${newState.guild.id}.json`);
let data = fs.readFileSync(filePath);
let jsonData = JSON.parse(data);
let checkvoicestaus = jsonData[`voicestsus_${newState.guild.id}`]
if(checkvoicestaus == undefined){
if(newState.id == executor ){
jsonData[`voicestsus_${newState.guild.id}`] = { info : {status :true , stage:1} };
fs.writeFileSync(filePath, JSON.stringify(jsonData , null , 2));
await sleep(3000)
const resource = createAudioResource('./voices/voice_1.mp3');
player.play(resource);
connection.subscribe(player);
channel.send(`${executor} hello mr , my name is ToredBotMc and iam here to help you with the bot\nokay the first one type '/startup' command to create the bot\niam waiting for you`)
}


}
else{
return;
}
})
client.on("voiceStateUpdate", async (oldState, newState) => {
const player = createAudioPlayer();
const folderPath = path.join(__dirname, 'data');
const filePath = path.join(folderPath, `server_${newState.guild.id}.json`);
let data = fs.readFileSync(filePath);
let jsonData = JSON.parse(data);
let checkvoicestaus = jsonData[`voicestsus_${newState.guild.id}`]
if(checkvoicestaus == undefined){
if(newState.id == executor ){
jsonData[`voicestsus_${newState.guild.id}`] = { info : {status :true , stage:1} };
fs.writeFileSync(filePath, JSON.stringify(jsonData , null , 2));
await sleep(3000)
const resource = createAudioResource('./voices/voice_1.mp3');
player.play(resource);
connection.subscribe(player);
channel.send(`${executor} hello mr , my name is ToredBotMc and iam here to help you with the bot\nokay the first one type '/startup' command to create the bot\niam waiting for you`)
}


}
else{
return;
}
})
duck
duckā€¢4w ago
going entirely on a hunch, is it possible this event listener is inside a command?
John Snos
John SnosOPā€¢4w ago
It's an old version but I'm also facing the same problem. No It's like If someone join to the voice The music start play
duck
duckā€¢4w ago
ok but I'm curious about the indenting combined with the several variables that aren't defined in this scope where are connection and channel defined? could you share the whole file?
John Snos
John SnosOPā€¢4w ago
But if a bot enters a voice room in multiple servers, a song will be played in all of these servers. I only want it in the server that the person entered.
duck
duckā€¢4w ago
yeah I understand the premise your code just doesn't appear to be the whole picture
John Snos
John SnosOPā€¢4w ago
I think I deleted some definitions for you to try
duck
duckā€¢4w ago
which is why I asked for the code as it is
John Snos
John SnosOPā€¢4w ago
You can bring any song playback code For try The problem is not related to playing the song The problem is related to playing the same song on multiple servers.
duck
duckā€¢4w ago
yes, which is why I'm asking for more of the code, since this code you provided isn't the direct source I am unable to help you with this issue if you're unable to share the code that produces it please share the whole file
John Snos
John SnosOPā€¢4w ago
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');
const path = require('path');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand() || interaction.commandName !== 'play') return;

const voiceChannel = interaction.member.voice.channel;
if (!voiceChannel) {
return interaction.reply('You need to be in a voice channel to play the song!');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();
const filePath = path.join(__dirname, 'Song.mp3');
const resource = createAudioResource(filePath);

player.play(resource);
connection.subscribe(player);

player.on(AudioPlayerStatus.Playing, () => {
console.log('Song is now playing');
interaction.reply('Song started playing!');
});

player.on(AudioPlayerStatus.Idle, () => {
console.log('Song has finished playing');
connection.destroy();
});
});

client.login('YOUR_BOT_TOKEN');
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');
const path = require('path');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand() || interaction.commandName !== 'play') return;

const voiceChannel = interaction.member.voice.channel;
if (!voiceChannel) {
return interaction.reply('You need to be in a voice channel to play the song!');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();
const filePath = path.join(__dirname, 'Song.mp3');
const resource = createAudioResource(filePath);

player.play(resource);
connection.subscribe(player);

player.on(AudioPlayerStatus.Playing, () => {
console.log('Song is now playing');
interaction.reply('Song started playing!');
});

player.on(AudioPlayerStatus.Idle, () => {
console.log('Song has finished playing');
connection.destroy();
});
});

client.login('YOUR_BOT_TOKEN');
This is a demo code from AI.
duck
duckā€¢4w ago
that's great please share your code
John Snos
John SnosOPā€¢4w ago
You can apply the same problem to me My friend, I deleted half of it to try and convert it to a handler.
duck
duckā€¢4w ago
well if you'd just like to me analyze this code, it only plays 1 file to everyone so therefore all servers will receive the same audio then I cannot help you with this issue
John Snos
John SnosOPā€¢4w ago
Why?
duck
duckā€¢4w ago
because I do not have access to the code that produces the issue
John Snos
John SnosOPā€¢4w ago
Consider this the code. This is my code that is giving me trouble.
duck
duckā€¢4w ago
consider my answer
John Snos
John SnosOPā€¢4w ago
Yes this the problem I need a help for this Why does it play the same song on all servers?
duck
duckā€¢4w ago
because you've only told it to do so?
const filePath = path.join(__dirname, 'Song.mp3');
const resource = createAudioResource(filePath);
const filePath = path.join(__dirname, 'Song.mp3');
const resource = createAudioResource(filePath);
this is a single path for a single file it only plays this one file
John Snos
John SnosOPā€¢4w ago
What should I do if Because I did not find a logical solution
duck
duckā€¢4w ago
I'm not entirely sure what you're asking are you just having trouble using a different path? a path other than "./Song.mp3"?
John Snos
John SnosOPā€¢4w ago
The problem is when the bot plays the song, it plays it on all the servers the bot is on, but I want it on the server that I specify.
duck
duckā€¢4w ago
listen if you're expecting me to just imagine code that plays more than 1 song, I'm not going to do that please share your code if your friend has deleted half of it, please write new code, see if it still has the same issue, and if it does, come back and share it
John Snos
John SnosOPā€¢4w ago
@duck You can apply it with this code
duck
duckā€¢4w ago
I cannot because this code does not have more than 1 song
John Snos
John SnosOPā€¢4w ago
Yes you didn't understand me What for one moment? Do you mean that I should make each server have its own song? I mean when these songs are played, for example, on a server His name lol1 The song will play automatically on another server, for example, called lol2 I only want it when the song is playing on a server called lol1 Only works on this server Do you understand me now?
duck
duckā€¢4w ago
I do now understand what you're focused on, but the code provided would not produce this issue each time this command is executed, a new player is created every guild would also have its own VoiceConnection it should not be playing the same audio to every connection without every connection subscribing to the new AudioPlayer I am unable to reproduce the issue you're describing with this code I still believe that it would be easier to diagnose if you shared your code furthermore it's also entirely possible that after finishing your new code, you find that it's been fixed by the new code, so I would still suggest finishing that first
John Snos
John SnosOPā€¢4w ago
I used this code and same problem (:
duck
duckā€¢4w ago
well it shouldn't have the same problem, and having run it just now myself, it does not appear to have the same problem how have you confirmed it does? could you share your logs? I suppose coming back to the multiple audio files idea, it would actually help ensure things are distinct if you were to play different sounds per server, does it still produce the same audio for both?
John Snos
John SnosOPā€¢4w ago
okay I will do I will try it again Same problem
John Snos
John SnosOPā€¢4w ago
No description
John Snos
John SnosOPā€¢4w ago
Do you see this picture?
duck
duckā€¢4w ago
can you share this updated code? I do yes is it possible that this, the green circle, is the only means by which you're checking whether it's playing in other servers?
John Snos
John SnosOPā€¢4w ago
client.on('messageCreate', async (message) => {
console.log('w2')
if(message.content == "play1"){
console.log('w')
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to play the song!');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();
const filePath = path.join(__dirname, 'voice_1.mp3');
const resource = createAudioResource(filePath);

player.play(resource);
connection.subscribe(player);

player.on(AudioPlayerStatus.Playing, () => {
console.log('Song is now playing');
message.reply('Song started playing!');
});

player.on(AudioPlayerStatus.Idle, () => {
console.log('Song has finished playing');
//connection.destroy();

});
}
});
client.on('messageCreate', async (message) => {
console.log('w2')
if(message.content == "play1"){
console.log('w')
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to play the song!');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();
const filePath = path.join(__dirname, 'voice_1.mp3');
const resource = createAudioResource(filePath);

player.play(resource);
connection.subscribe(player);

player.on(AudioPlayerStatus.Playing, () => {
console.log('Song is now playing');
message.reply('Song started playing!');
});

player.on(AudioPlayerStatus.Idle, () => {
console.log('Song has finished playing');
//connection.destroy();

});
}
});
The bot is in a voice room and when I typed the command play He also started talking on another server.
duck
duckā€¢4w ago
could you answer this as well
John Snos
John SnosOPā€¢4w ago
No, I came in and heard the sound on. I have concluded something. I think the problem because connection.destroy();
duck
duckā€¢4w ago
I'm not entirely sure how you've drawn that conclusion, but that should not be the issue once again, this code should not be causing this issue, and if it truly does, it'd be an issue with the library
d.js docs
d.js docsā€¢4w ago
To debug your voice connection and player: - Use debug: true when creating your VoiceConnection and AudioPlayer - Add an event listener to the <VoiceConnection> and the <AudioPlayer>:
// Add one for each class if applicable
<AudioPlayer | VoiceConnection>
.on('debug', console.log)
.on('error', console.error)
// Add one for each class if applicable
<AudioPlayer | VoiceConnection>
.on('debug', console.log)
.on('error', console.error)
- Add an error listener to the stream you are passing to the resource:
<Stream>.on('error', console.error)
<Stream>.on('error', console.error)
Note: The <> represents classes that need to be adapted to their respective name in your code
duck
duckā€¢4w ago
please add these for both the audio player and connection and share the output
John Snos
John SnosOPā€¢4w ago
I solved the problem šŸ„³šŸ„³šŸ„³šŸ„³šŸ„³ The problem was from connection.destroy(); Thanks for the help.
duck
duckā€¢4w ago
could you explain your reasoning? once again, that should not be the source of the issue, and if it is, something still needs to be fixed
John Snos
John SnosOPā€¢4w ago
Because I forgot to define connection.destroy(); to end the music. The reason is kind of stupid. And illogical And trivial It is difficult to explain.
duck
duckā€¢4w ago
once again, especially if you weren't destroying the connection, the connection should not have been playing audio from a different AudioPlayer
John Snos
John SnosOPā€¢4w ago
I really don't understand how the problem was solved (:
Want results from more Discord servers?
Add your server