我正在尝试将音乐bot的功能实现到我的discord bot中,但是将我的作品保存在atom中后,node报告未定义短语“ serverQueue”。
显然,问题出在以下事实:“ queue”一词未在文件中更早定义,因此它不理解如何使用它,唯一的问题是,我不知道如何添加它。 / p>
client.on("message", async message => {
if (message.author.bot) return;
const severQueue = queue.get(message.guild.id);
if (message.content.includes(`!play`)) {
execute(message, serverQueue);
return;
} else if (message.content.includes(`!skip`)) {
skip(message, serverQueue);
return;
} else if (message.content.includes(`!stop`)) {
stop(message, serverQueue);
return;
} else {
message.channel.send("Necesitas poner un comando valido...");
}
});
async function execute(message, serverQueue) {
const args = message.content.split(" ");
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
"¡Necesitas estar en un canal de voz para pedir musica!"
);
const permissions = voiceChannel.permissionsfor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"¡Necesito permisos para unirme y cantar en ese canal de voz!"
);
}
const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.title,
url: songInfo.video_url,
};
if (!serverQueue) {
const queueContrucut = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true,
};
queue.set(message.guild.id, queueContrucut);
queueContrucut.songs.push(song);
try {
var connection = await voiceChannel.join();
queueContrucut.connection = connection;
play(message.guild, queueContrucut.song[0]);
}catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.song);
return message.channel.send(`¡${song.title} ha sido agregada a la lista de reproducción!`);
}
}
function skip(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"¡Necesitas estar en un canal de voz para skipear la musica!"
);
if (!serverQueue)
return message.channel.send("¡No hay canción que pueda skipear!");
serverQueue.connection.dispatcher.end();
}
function stop (message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"¡Necesitas estar en un canal de voz para detener la musica!"
);
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(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(`Empezó a sonar: **${song.title}**`);
}
它有望启动,并允许我将其连接到语音通道,以便从YouTube之类的网站进行音频播放以收听音乐,但相反,它在启动时崩溃,并且我得到的唯一错误是短语“ ServerQueue”是没有定义。
这是控制台中出现的错误:
!播放despacito
(节点:7644)UnhandledPromiseRejectionWarning:ReferenceError:服务器队列未定义
在客户处。 (C:\ Users \ abrah \ Documents \ alienwalkerbot \ index.js:101:22)
在Client.emit(events.js:326:22)
在MessageCreateAction.handle(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ discord.js \ src \ client \ actions \ MessageCreate.js:31:14)
在Object.module.exports [作为MESSAGE_CREATE](C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ discord.js \ src \ client \ websocket \ handlers \ MESSAGE_CREATE.js:4:32)中
在WebSocketManager.handlePacket(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketManager.js:386:31)
在WebSocketShard.onPacket上(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:436:22)
在WebSocketShard.onMessage(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:293:10)
在WebSocket.onMessage(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ ws \ lib \ event-target.js:125:16)
在WebSocket.emit(events.js:314:20)
在Receiver.receiverOnMessage(C:\ Users \ abrah \ Documents \ alienwalkerbot \ node_modules \ ws \ lib \ websocket.js:797:20)
(节点:7644)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未经.catch()处理的诺言而引起的。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志--unhandled-rejections=strict
(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝ID:2)