Here the code for my play.js file:
const { createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection, AudioPlayerStatus } = require('@discordjs/voice');
const play = require('play-dl');
const { Readable } = require('stream');
module.exports = {
name: 'play',
cooldown: 0,
description: 'Riproduce un video da YouTube',
async execute(message, args) {
if (!message.member.voice.channel) {
return message.channel.send('Connettiti ad un canale vocale!');
}
const query = args.join(' ');
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
try {
const yt_info = await play.video_info(query);
console.log('Video info fetched:', yt_info.video_details.title);
const stream = await play.stream_from_info(yt_info);
console.log('Stream fetched:', stream.type);
let readableStream;
if (stream.stream instanceof Readable) {
readableStream = stream.stream;
} else {
readableStream = Readable.from(stream.stream);
}
const resource = createAudioResource(readableStream, {
inputType: stream.type,
inlineVolume: true,
});
const player = createAudioPlayer();
player.on(AudioPlayerStatus.Playing, () => {
console.log('Audio player started playing');
});
player.on(AudioPlayerStatus.Idle, () => {
console.log('Audio player is idle');
});
const voiceConnection = getVoiceConnection(message.guild.id);
if (voiceConnection) {
voiceConnection.subscribe(player);
} else {
connection.subscribe(player);
}
player.play(resource);
console.log('Started playing:', yt_info.video_details.title);
message.channel.send(`Sto riproducendo: ${yt_info.video_details.title}`);
} catch (error) {
console.error('Errore durante la riproduzione:', error);
message.channel.send('Si è verificato un errore durante la riproduzione del video.');
}
},
};
Here are the console logs:
Video info fetched: Prowler Goku meme
Stream fetched: opus
Started playing: Prowler Goku meme
then it prints line 7 of the play-dl library and in the end it types this:
TypeError: Cannot read properties of undefined (reading 'url')
at fe.retry (C:UsersMYUSERDesktopDESKTOPFOLDERPROJECTnode_modulesplay-dldistindex.js:7:15306)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async fe.seek (C:UsersMYUSERDesktopDESKTOPFOLDERPROJECTnode_modulesplay-dldistindex.js:7:14941)
if you need here is the dependencies in the package.json:
{
"dependencies": {
"@discordjs/voice": "^0.17.0",
"discord.js": "^14.15.3",
"opusscript": "^0.1.1",
"play-dl": "^1.9.7"
}
}
pls help me i’ve tried everything and the rest of the commands work fine, i don’t wanna use slash commands
the only things it does:
It connects, say in chat that it’s playing the audio, eternal silence and after a while it crashes.
New contributor
Zap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.