G
G
gohellp2020-08-11 16:04:04
Node.js
gohellp, 2020-08-11 16:04:04

Why doesn't the bot play audio after connecting and throws an error?

When you start the bot, everything is fine, but as soon as you order a song, it gives an error

The "file" argument must be of type string. Received an instance of Object
and completes its work. FFMPEG reinstalled, downloaded the standard, binaries - nothing helped. I'm sure in the code, and I tried converting the type of the variable to ctring - the same result. I looked at the branches on the stack, github, etc. - I did not find anything sensible. I also tried the obviously working code from the tutorials on creating music bots.

const Discord = require('discord.js');
const {
    prefix,
    token,
} = require('./config.json');
const ytdl = require('ytdl-core');
const client = new Discord.Client();
const queue = new Map();

client.once('ready', () => {
    console.log('Ready!');
});
client.once('reconnecting', () => {
    console.log('Reconnecting!');
});
client.once('disconnect', () => {
    console.log('Disconnect!');
});
client.on('message', async message => {
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;
    const serverQueue = queue.get(message.guild.id);
    if (message.content.startsWith(`${prefix}play`)) {
        execute(message, serverQueue);
        return;
    } else if (message.content.startsWith(`${prefix}skip`)) {
        skip(message, serverQueue);
        return;
    } else if (message.content.startsWith(`${prefix}stop`)) {
        stop(message, serverQueue);
        return;
    } else {
        message.channel.send('You need to enter a valid command!')
    }
});
async function execute(message, serverQueue) {
    const args = message.content.split(' ');
    const voiceChannel = message.member.voiceChannel;
    if (!voiceChannel) return message.channel.send('You need to be in a voice channel to play music!');
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
        return message.channel.send('I need the permissions to join and speak in your voice channel!');
    }
    const songInfo = await ytdl.getInfo(args[1]);
    const song = {
        title: songInfo.title,
        url: songInfo.video_url,
    };
    if (!serverQueue) {
        const queueContruct = {
            textChannel: message.channel,
            voiceChannel: voiceChannel,
            connection: null,
            songs: [],
            volume: 5,
            playing: true,
        };
        queue.set(message.guild.id, queueContruct);
        queueContruct.songs.push(song);
        try {
            var connection = await voiceChannel.join();
            queueContruct.connection = connection;
            play(message.guild, queueContruct.songs[0]);
        } catch (err) {
            console.log(err);
            queue.delete(message.guild.id);
            return message.channel.send(err);
        }
    } else {
        serverQueue.songs.push(song);
        console.log(serverQueue.songs);
        return message.channel.send(`${song.title} has been added to the queue!`);
    }
}
function skip(message, serverQueue) {
    if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
    if (!serverQueue) return message.channel.send('There is no song that I could skip!');
    serverQueue.connection.dispatcher.end();
}
function stop(message, serverQueue) {
    if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
    serverQueue.songs = [];
    serverQueue.connection.dispatcher.end();
}
function play(guild, song) {
    const serverQueue = queue.get(guild.id);
    if (!song) {
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    }
    const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
        .on('end', () => {
            console.log('Music ended!');
            serverQueue.songs.shift();
            play(guild, serverQueue.songs[0]);
        })
        .on('error', error => {
            console.error(error);
        });
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}
client.login(token);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-11
@gohellp

At first glance, it's hard to tell what exactly the problem is.
you copied the code from here , right?
As far as I understand, this code is for an old version of discord.js, and I don't advise you to use it.
it has a lot of confusing moments, in general it is written quite badly.
try to do a simple connection and play a tune, and if everything goes smoothly - the problem was in that code (otherwise, it's FFMPEG'e).

bot.on("message", async message => {
const dispatcher = connection.play('audio.mp3');

dispatcher.on('start', () => {
  message.channel.send('audio.mp3 проигрывается!');
});

dispatcher.on('finish', () => {
  message.channel.send('audio.mp3 остановлено!');
});

dispatcher.on('error', console.error);
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question