K
K
Kirill2020-11-04 21:57:37
Node.js
Kirill, 2020-11-04 21:57:37

Why does it give an error, and how to fix it?

spoiler
const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const QuickYtSearch = require('quick-yt-search'); // Require the package
const YoutubeSearcher = new QuickYtSearch({
    YtApiKey: 'Он есть', // Place your YouTube API key here
});
module.exports = {
  name: 'play1',
  description: 'Plays a song.',
  execute(message, args, queue, serverQueue) {
        const MAU = message.author.displayAvatarURL();
        const voiceChannel = message.member.voice.channel;
        args = message.content.split(' ');

    args.splice(0, 1);

    args = args.join(' ');
        YoutubeSearcher.getVideo(args).then(video => {
            voiceChannel.join().then(connection => {
                const voiceChannel = message.member.voice.channel;
                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 song = {
                    title: video.title,
                    url: video.url
                };

                if (!serverQueue) {
                    const queueContruct = {
                    textChannel: message.channel,
                    voiceChannel: voiceChannel,
                    connection: null,
                    songs: [],
                    volume: 5,
                    playing: true
                    };

                    queue.set(message.guild.id, queueContruct);

                    let addedMess = new Discord.MessageEmbed()
                        .setTitle('Музыка добавлена в список!')
                        .setDescription(`Добавлено **${song.title}**!`)
                        .setColor('0x00FF00') 
                        .setThumbnail(video.defaultThumbnail)
                        .setFooter(message.author.username, MAU)
                    queueContruct.songs.push(song);
                    message.channel.send(addedMess)
                    

                    try {
                    var connection = voiceChannel.join();
                    queueContruct.connection = connection;
                    play(message.guild, queueContruct.songs[0]);
                    let musicMess = new Discord.MessageEmbed()
                        .setTitle('Проигрывается музыка!')
                        .setDescription(`Сейчас играет **${song.title}**!`)
                        .setColor('0x00FF00') 
                        .setThumbnail(video.defaultThumbnail)
                        .setFooter(message.author.username, MAU)
                    } catch (err) {
                    console.log(err);
                    queue.delete(message.guild.id);
                    return message.channel.send(err);
                    }
                } else {
                    serverQueue.songs.push(song);
                    return message.channel.send(`${song.title} has been added to the queue!`);
                }
                function play(song, guild) {
                    if (!song) {
                    serverQueue = queue.get(guild.id); 
                    serverQueue.voiceChannel.leave();
                    queue.delete(message.guild.id);
                    return;
                    }
                  
                    const dispatcher = serverQueue.connection //Тут ошибка
                      .play(ytdl(song.url))
                      .on("finish", () => {
                        serverQueue.songs.shift();
                        play(guild, serverQueue.songs[0]);
                      })
                      .on("error", error => console.error(error));
                    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
                    serverQueue.textChannel.send(musicMess);
                }
            });
        });
  },
};


Mistake:

TypeError: Cannot read property 'connection' of undefined
    at play (C:\Users\Кирилл\Documents\discord-bots\music-bot\commands\play1.js:86:52)
    at C:\Users\Кирилл\Documents\discord-bots\music-bot\commands\play1.js:62:21       
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:15340) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (C:\Users\Кирилл\Documents\discord-bots\music-bot\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:15340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15340) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


Discord.js v12

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question