Discord music bot: prefix not defined

I wanted to make a music discord bot and I’ve been having trouble with it for a while. and I keep getting an error and I got this code from CodeLyon’s video called “Make Your Own Discord Bot | Music Bot (Play, Skip, Stop Commands)”

I keep getting the same error when I try it:

ReferenceError: PREFIX is not defined

    const ytdl = require("ytdl-core");
    var servers = {};
    
    client.on('message', message => {
        
        let args = message.content.substring(PREFIX.length).split(" ");
        switch (args[0]){
            case "play":
                const prefix = '!'; 
            function play(connection, message){
            var server= servers[message.guild.id];
                server.dispatcher = connection.play(ytdl(server.queue[0], {filter: "audioonly"}));
    
                server.queue.shift();
                server.dispatcher.on("end", function(){
                    if(server.queue[0]){
                        play(connection, message);
                    }else {
                        connection.disconnect();
    
                    }
                });
            }
    
           if(!args[1]){
               message.channel.send("you need to provide a link");
               return;
           }
           if(!message.member.VoiceChannel){
               message.channel.send("you must be in a channel to play the bot");
            return;
            }
           
            if(!serveres[message.guild.id]) servers[message.guild.id] = {
    
               queue: [] 
            }
           var server = servers[message.guild.id];
    
           server.queue.push(args[1]);
    
            if(!message.guild.voice) message.member.VoiceChannel.join().then(function(Connection){
                play(connection, message)
    
            })
    
           break;
    
           case 'skip':
               var server = servers[message.guild.id];
               if(server.dispatcher) server.dispatcher.end();
               break;
               case 'stop':
                var server = servers[message.guild.id];
                if(message.guild.voice){
                    for(var i = server.queue.length -1; i >=0;i--){
                    server.queue.splice(i, 1);   
                    }
                    server.dispatcher.end();
                    console.log('stopped the queue')
                }
                if(message.guild.connection) message.guild.voice.disconnect();
                break;
        }
    
    
    });

Answer

The error you are getting basically tells you that you have not defined the PREFIX variable.

At the top of your code you should add the following constant variable:

const PREFIX = '!';

Leave a Reply

Your email address will not be published. Required fields are marked *