Audio not playing despite properly retrieving URL to audio. No errors.

I'm trying to play a song through a URL (not YouTube) and despite the URL ending with .ogg (i even tried with .mp3), audio isn't emitted. There is no errors or anything alike, it just doesn't play. What do I do please? I have installed FFmpeg, @discordjs/voice and @discordjs/opus. (Code will be in the comments because it's too long)
14 Replies
d.js toolkit
d.js toolkit4w 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 OP
althruist
althruistOP4w ago
// Imports
const {Client,Events,GatewayIntentBits} = require('discord.js');
const {joinVoiceChannel,createAudioPlayer,createAudioResource,AudioResource,StreamType,getVoiceConnections} = require('@discordjs/voice');

// Client
const client = new Client({intents: [GatewayIntentBits.Guilds,GatewayIntentBits.GuildMessages,GatewayIntentBits.MessageContent]});

async function fetchAudioResource(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch audio: ${response.statusText}`);
const resource = createAudioResource(response.body, { inlineVolume: true });
resource.volume.setVolume(1);
return resource;
}

// Message handler
client.on('messageCreate', async (message) => {
if (message.author.bot || !message.guild) return;
if (!message.content.startsWith(system[0].prefix)) return;

const args = message.content.slice(system[0].prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();

if (command === "play") {
const voiceConnection = joinVoiceChannel({
channelId: "1318656940884234323",
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

message.reply("I'm in! Hop on with me in <#1318656940884234323>");
}
// Imports
const {Client,Events,GatewayIntentBits} = require('discord.js');
const {joinVoiceChannel,createAudioPlayer,createAudioResource,AudioResource,StreamType,getVoiceConnections} = require('@discordjs/voice');

// Client
const client = new Client({intents: [GatewayIntentBits.Guilds,GatewayIntentBits.GuildMessages,GatewayIntentBits.MessageContent]});

async function fetchAudioResource(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch audio: ${response.statusText}`);
const resource = createAudioResource(response.body, { inlineVolume: true });
resource.volume.setVolume(1);
return resource;
}

// Message handler
client.on('messageCreate', async (message) => {
if (message.author.bot || !message.guild) return;
if (!message.content.startsWith(system[0].prefix)) return;

const args = message.content.slice(system[0].prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();

if (command === "play") {
const voiceConnection = joinVoiceChannel({
channelId: "1318656940884234323",
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});

message.reply("I'm in! Hop on with me in <#1318656940884234323>");
}
if (command === "queue") {
try {
if (args.length < 1) {
return message.reply("Please provide a valid audio URL!");
}
const audioUrl = args[0];
const audioResource = await fetchAudioResource(audioUrl);
const voiceConnection = getVoiceConnections().get(message.guild.id);

if (!voiceConnection) {
return message.reply("The bot isn't connected to a voice channel!");
}

const player = createAudioPlayer();
player.play(audioResource);
voiceConnection.subscribe(player);

player.on('error', (error) => {
console.error('Player error:', error.message);
message.reply("An error occurred during playback.");
});

player.on('idle', () => {
console.log('Finished playing!');
voiceConnection.destroy();
message.reply("Finished playing the audio!");
});

message.reply("Playing your audio!");
} catch (error) {
console.error(error);
message.reply("Failed to play the audio. Please check the URL and try again.");
}
}
});
if (command === "queue") {
try {
if (args.length < 1) {
return message.reply("Please provide a valid audio URL!");
}
const audioUrl = args[0];
const audioResource = await fetchAudioResource(audioUrl);
const voiceConnection = getVoiceConnections().get(message.guild.id);

if (!voiceConnection) {
return message.reply("The bot isn't connected to a voice channel!");
}

const player = createAudioPlayer();
player.play(audioResource);
voiceConnection.subscribe(player);

player.on('error', (error) => {
console.error('Player error:', error.message);
message.reply("An error occurred during playback.");
});

player.on('idle', () => {
console.log('Finished playing!');
voiceConnection.destroy();
message.reply("Finished playing the audio!");
});

message.reply("Playing your audio!");
} catch (error) {
console.error(error);
message.reply("Failed to play the audio. Please check the URL and try again.");
}
}
});
(i did remove the unnecessary lines of code for the purpose of this thread)
althruist
althruistOP4w ago
this is the only feedback i got from the bot
No description
althruist
althruistOP4w ago
(also apologies for sending this on #djs-questions, i didn't notice this channel 😅 ) yeah! let me send it here
althruist
althruistOP4w ago
this is if i log response.body alone
No description
althruist
althruistOP4w ago
and if i do the resource:
althruist
althruistOP4w ago
it was reporting that it was playing an .ogg.. now it's not? i think its because i installed libopus as i was trying to debug it i see so, remove inlinevolume? or libopus? okay!
d.js docs
d.js docs4w 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
althruist
althruistOP4w ago
okay i will try!
althruist
althruistOP4w ago
No description
althruist
althruistOP4w ago
i got this status autopaused??? i haven't, let me try nothing i tried doing some research about it, there was an old workaround for it but it seems that it used to be a bug that got patched. im not sure what's going on
althruist
althruistOP4w ago
i tried adding GatewayIntentBits.GuildVoiceStates and now its printing these
No description
althruist
althruistOP4w ago
uhh... dunno. it did though LOL OH it just- started playing. half way through the song its working now but it's verrrry choppy..?? how do i go about that? i see, ill do my research on that! i went ahead and utilized it
async function fetchAudioResource(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch audio: ${response.statusText}`);

// Convert the response body to an ArrayBuffer
const arrayBuffer = await response.arrayBuffer();

// Create a ReadableStream from the ArrayBuffer
const readableStream = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(arrayBuffer));
controller.close();
}
});

// Create a resource from the ReadableStream
const resource = createAudioResource(readableStream, { inputType: StreamType.Arbitrary });
return resource;
}
async function fetchAudioResource(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch audio: ${response.statusText}`);

// Convert the response body to an ArrayBuffer
const arrayBuffer = await response.arrayBuffer();

// Create a ReadableStream from the ArrayBuffer
const readableStream = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(arrayBuffer));
controller.close();
}
});

// Create a resource from the ReadableStream
const resource = createAudioResource(readableStream, { inputType: StreamType.Arbitrary });
return resource;
}
-# honestly i did use AI for help because it's late and my head can't wrap around it all that well 🥲 it still seems to be a bit choppy but it's better. it might be my internet since for whatever reason my laptop handles my internet poorly thanks for the help! :)

Did you find this page helpful?