Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

javascript - cannot read property 'queue' of undefined

Hey i'm trying to test a music bot with node.js and discord.js. I've put it in 2 servers, in the first one it works fine, in the second one it gives me cannot read property 'queue' of undefined and i don't know why.

const Discord = require("discord.js");
const config = require("./config.json");  
const ytdl = require("ytdl-core");  
const client = new Discord.Client();


const prefix = "..";
var servers = {};

client.on("message", message => {

let args = message.content.substring(prefix.length).split(" ");

function isValidURL(string) {
    var res = string.match(/(http(s)?://.)?(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}([-a-zA-Z0-9@:%_+.~#?&//=]*)/g);
    return (res !== null)
  };

switch(args[0]){
    case "play":

        function play(connection, message){
            var serv = servers[message.guild.id];

            serv.dispatcher = connection.play(ytdl(serv.queue[0], {filter: "audioonly"}));

            serv.queue.shift();

            serv.dispatcher.on("end", function(){
                if(serv.queue[0]){
                    play(connection, message);
                }else{
                    connection.disconnect();
                }

            });

        }


        if(!args[1]){
            message.channel.send(message.author.toString() + " Devi mettere un link dopo il comando.");
            return;
        }else if(isValidURL(args[1]) == false){
            message.channel.send(message.author.toString() + " Link non valido.");
            return;
        }else if(!message.member.voice.channel){
            message.channel.send(message.author.toString() + " Devi essere in un canale vocale.");
            return;
        }else if(!servers[message.guild.id]) servers[message.channel.id] = {
            queue: []
        }

        var server = servers[message.guild.id];

        server.queue.push(args[1]);

        if(!message.member.voice.connection) message.member.voice.channel.join().then(function(connection){
            play(connection, message);
        });




    break;

}





 });

 client.login(config.BOT_TOKEN);

It might be something with servers permissions but they are the same. So i don't know, any idea?

question from:https://stackoverflow.com/questions/65906543/cannot-read-property-queue-of-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The discord.js library may be updated to a new master version which the bot is not compatible with. Try using the version of discord.js which is used in the original source.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...