2

So My Discord bot will be able to play music for a while then it will stop and i have to restart the bot to fix it. And if it stopped then i use command skip it will crash the bot with the errors below

C:\Users\User\Desktop\Discord bot\Song\play.js:96
    server_queue.connection.dispatcher.end();
                                       ^

TypeError: Cannot read property 'end' of null
    at skip_song (C:\Users\User\Desktop\Discord bot\Song\play.js:96:40)
    at Object.execute (C:\Users\User\Desktop\Discord bot\Song\play.js:65:47)
    at Client.<anonymous> (C:\Users\User\Desktop\Discord bot\bot.js:78:74)
    at Client.emit (node:events:394:28)
    at MessageCreateAction.handle (C:\Users\User\Desktop\Discord bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\User\Desktop\Discord bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\User\Desktop\Discord bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\User\Desktop\Discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\User\Desktop\Discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\User\Desktop\Discord bot\node_modules\ws\lib\event-target.js:132:16)

I guess the problem is something with ffmpeg or i must have some npms more.

npm i already installed

  • discord.js
  • ytdl-core
  • ytdl-search and also i already installed ffmpeg in my computer and set the path

i want to ask that do i have to install any npms more? or anyone know how to fixed this problem?

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

const queue = new Map();

module.exports = {
    name: 'play',
    aliases: ['skip', 'stop', 'queue', 'leave', 'join'],
    description: 'Joins and play',
    async execute(message, args , cmd, client, Discord){
        const voice_channel = message.member.voice.channel;
        if(!voice_channel) return message.channel.send('เข้าไปอยู่ในดิสก่อนดิวะ :angry: ');//you must in voice channel
        
        
        const server_queue = queue.get(message.guild.id);
        if(cmd==='play' || cmd==='p'){
            if(!args.length) return message.channel.send('จะเปิดอะไรล่ะพี่ :triumph:');//you must have argument
            let song = {};

            if(ytdl.validateURL(args[0])){
                const song_info = await ytdl.getInfo(args[0]);
                song = {title: song_info.videoDetails.title, url: song_info.videoDetails.video_url}
            } else {
                const videoFinder = async (query) => {
                    const videoResult = await ytSearch(query);
                    return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
                }

                const video = await videoFinder(args.join(' '));
                if(video){
                    song = {title: video.title, url: video.url}
                
                } else{
                    message.channel.send('หาไม่เจอ :cry: ');//cant find song
                }
            }
            //const connection = await voiceChannel.join(); 
            if(!server_queue){
                const queue_constructor = {
                    voice_channel: voice_channel,
                    text_channel: message.channel,
                    connection: null,
                    songs: []
                }

                queue.set(message.guild.id, queue_constructor);
                queue_constructor.songs.push(song);

                try{
                    const connection = await voice_channel.join();
                    queue_constructor.connection = connection;
                    video_player(message.guild, queue_constructor.songs[0]);
                } catch (err) {
                    queue.delete(message.guild.id);
                    message.channel.send('error');
                    throw err;
                }
            } else{
                server_queue.songs.push(song);
                return message.channel.send(`:regional_indicator_k: :regional_indicator_o: :regional_indicator_n: :regional_indicator_g: :star_struck: **${song.title}** ใส่เข้าคิวแล้ว`)//added to queue
            }
            

        }
        else if(cmd === 'skip' || cmd ==='s') skip_song(message, server_queue);
        else if(cmd === 'clear' || cmd==='c') clear_song(message, server_queue);
        else if(cmd === 'join') join_song(message);
        else if(cmd === 'leave') leave_song(message);
        else if(cmd === 'queue' || cmd ==='q') queue_show(message,server_queue,Discord);


    }
}

const video_player = async (guild, song) => {
    const song_queue = queue.get(guild.id);

    if(!song){
        //song_queue.text_channel.send('ไปละบาย :kissing_heart: ');
        song_queue.voice_channel.leave();
        queue.delete(guild.id);
        return;
    }
    const stream = ytdl(song.url,{filter: 'audioonly'},{ highWaterMark: 1<<25 });
    song_queue.connection.play(stream, { seek: 0, volume: 0.5 })
    .on('finish', () => {
        song_queue.songs.shift();
        video_player(guild, song_queue.songs[0]);
    });
    await song_queue.text_channel.send(`:regional_indicator_k: :regional_indicator_o: :regional_indicator_n: :regional_indicator_g: :sunglasses: กำลังเล่นเด็ก ***${song.title}***`);//playing song
}

const skip_song = (message, server_queue) => {
    if(!message.member.voice.channel) return message.channel.send('เข้าดิสก่อนดิ :angry: ');//you must be in voice channel
    if(!server_queue) return message.channel.send('ไม่มีเพลงในคิวแล้ว :relieved: ');//no song in queue
    server_queue.connection.dispatcher.end();
}

const clear_song = (message, server_queue) => {
    if(!message.member.voice.channel) return message.channel.send('เข้าดิสก่อนดิ :angry: ');//you must be in voice channel
    if(!server_queue) return message.channel.send('ไม่มีเพลงในคิวแล้ว :relieved: ');//no song in queue
    server_queue.songs = [];
    server_queue.connection.dispatcher.end();
}

const join_song = (message) => {
    if(!message.member.voice.channel) return message.channel.send('เข้าดิสก่อนดิ :angry: ');//you must be in voice channel
    message.member.voice.channel.join();
}

const leave_song = (message) => {
    if(!message.member.voice.channel) return message.channel.send('เข้าดิสก่อนดิ :angry: ');//you must be in voice channel
    message.member.voice.channel.leave();
}

const queue_show =  (message,server_queue,Discord) => {
    if(!server_queue) return message.channel.send('ไม่มีเพลงในคิวแล้ว :relieved: ');//no song in queue
    const queueList = server_queue.songs.map((song, i) => `[${++i}] - ${song.title}`);
    const queueEmbed = new Discord.MessageEmbed()
    .setDescription(queueList);
    message.channel.send(queueEmbed);
}```
Konglnwza
  • 21
  • 2
  • 2
    Could you show the corresponding code please? This would help us a lot to identify the cause of this error – Gilles Heinesch Sep 27 '21 at 15:04
  • ok i just added it. When the song stop playing i still can check the queue or add new song to queue but it wont play anyway but if i use skip or clear the bot will crash with the error above. – Konglnwza Sep 27 '21 at 17:22

0 Answers0