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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…