Discord.js 音乐机器人不响应第一个音乐请求
Discord.js music bot does not respond to first music request
我最近一直在为我的 discord 服务器编写一个通用机器人,运行 遇到了一些音乐请求问题。目前没有队列系统,我不确定我是否打算实施一个,所以目前,如果正在播放一首歌曲并请求一首新歌曲,则跳过当前歌曲并开始播放新歌曲。然而,我的问题是,当正在播放一首歌曲并请求一首新歌曲时,它只会播放最后请求的歌曲,也就是当前歌曲。这实质上意味着歌曲总是落后于一个请求。我的音乐部分代码如下
var channel;
var dispatcher;
var contentID;
var playing = false;
var queue = [] bot.on("message", message => {
let args = message.content.substring(musicPrefix.length).split(" ");
//GOOGLE API AREA!
channel = message.member.voice.channel
if (!message.guild && args[0] === "play") return message.reply('Please send music requests in a Sleepybot© supported server!');
switch (args[0].toLowerCase()) {
case ("play"):
google.youtube("v3").search.list({
key: youtubeToken,
part: "snippet",
q: message.content.slice(6),
maxResults: 1,
}).then((response) => {
const { data } = response data.items.forEach((item) => {
console.log(item.id.videoId);
contentID = item.id.videoId;
});
}).catch((err) => console.log(err)); //if (message.member.voice.channel) {
if (!channel) {
message.reply("you must be in a voice channel to play music!");
return;
} else if (!args[1]) return message.reply("please specify a song title or link");
channel.join().then(connection => {
var sound = "https://www.youtube.com/watch?v=" + contentID; // const stream = ytdl(sound, { filter: 'audioonly' })
// if(playing===false){
const stream = ytdl(sound, { filter: 'audioonly' });
dispatcher = connection.play(stream);
console.log('playing something');
playing = true;
return;
});
break;
case ("disconnect"):
message.reply("disconnecting...") dispatcher.end();
if (!channel) {
message.reply("there was an error leaving the channel");
} else {
channel.leave() message.reply("disconnected");
}
playing = false; //you may not just "leave all channels" you need to leave a specific one, so make sure you're leaving the one it's in
break;
case ("pause"):
dispatcher.pause() message.reply("Paused"); console.log("paused");
break;
case ("resume"):
dispatcher.resume() message.reply("resumed!"); console.log("resumed");
break;
}
}
);
在 javascript 中使用承诺时,任何依赖于承诺结果的东西都需要在提供给 .then()
的 lambda 函数中。例子
promise.then((response) => {
// after the promise calls the lambda function provided
});
// code runs immediately right after the promise is **created**, not when done running
在您的情况下,您是 运行宁代码 在 lambda 之外。这意味着它将 运行 "outside" 在创建承诺后立即编码,无论它是否调用 .then()
...解决此问题:移动播放音频的部分
if (!channel) {
//.........
channel.join().then(connection => {
//........
return;
});
进入这里
google.youtube("v3").search.list({
key: youtubeToken,
part: "snippet",
q: message.content.slice(6),
maxResults: 1,
}).then((response) => { // START OF LAMBDA FUNCTION
const { data } = response data.items.forEach((item) => {
console.log(item.id.videoId);
contentID = item.id.videoId;
});
// ----------------
// INSERT CODE HERE
// ----------------
// END OF LAMBDA FUNCTION
}).catch((err) => console.log(err));
希望这能解决您的问题。 :)
我最近一直在为我的 discord 服务器编写一个通用机器人,运行 遇到了一些音乐请求问题。目前没有队列系统,我不确定我是否打算实施一个,所以目前,如果正在播放一首歌曲并请求一首新歌曲,则跳过当前歌曲并开始播放新歌曲。然而,我的问题是,当正在播放一首歌曲并请求一首新歌曲时,它只会播放最后请求的歌曲,也就是当前歌曲。这实质上意味着歌曲总是落后于一个请求。我的音乐部分代码如下
var channel;
var dispatcher;
var contentID;
var playing = false;
var queue = [] bot.on("message", message => {
let args = message.content.substring(musicPrefix.length).split(" ");
//GOOGLE API AREA!
channel = message.member.voice.channel
if (!message.guild && args[0] === "play") return message.reply('Please send music requests in a Sleepybot© supported server!');
switch (args[0].toLowerCase()) {
case ("play"):
google.youtube("v3").search.list({
key: youtubeToken,
part: "snippet",
q: message.content.slice(6),
maxResults: 1,
}).then((response) => {
const { data } = response data.items.forEach((item) => {
console.log(item.id.videoId);
contentID = item.id.videoId;
});
}).catch((err) => console.log(err)); //if (message.member.voice.channel) {
if (!channel) {
message.reply("you must be in a voice channel to play music!");
return;
} else if (!args[1]) return message.reply("please specify a song title or link");
channel.join().then(connection => {
var sound = "https://www.youtube.com/watch?v=" + contentID; // const stream = ytdl(sound, { filter: 'audioonly' })
// if(playing===false){
const stream = ytdl(sound, { filter: 'audioonly' });
dispatcher = connection.play(stream);
console.log('playing something');
playing = true;
return;
});
break;
case ("disconnect"):
message.reply("disconnecting...") dispatcher.end();
if (!channel) {
message.reply("there was an error leaving the channel");
} else {
channel.leave() message.reply("disconnected");
}
playing = false; //you may not just "leave all channels" you need to leave a specific one, so make sure you're leaving the one it's in
break;
case ("pause"):
dispatcher.pause() message.reply("Paused"); console.log("paused");
break;
case ("resume"):
dispatcher.resume() message.reply("resumed!"); console.log("resumed");
break;
}
}
);
在 javascript 中使用承诺时,任何依赖于承诺结果的东西都需要在提供给 .then()
的 lambda 函数中。例子
promise.then((response) => {
// after the promise calls the lambda function provided
});
// code runs immediately right after the promise is **created**, not when done running
在您的情况下,您是 运行宁代码 在 lambda 之外。这意味着它将 运行 "outside" 在创建承诺后立即编码,无论它是否调用 .then()
...解决此问题:移动播放音频的部分
if (!channel) {
//.........
channel.join().then(connection => {
//........
return;
});
进入这里
google.youtube("v3").search.list({
key: youtubeToken,
part: "snippet",
q: message.content.slice(6),
maxResults: 1,
}).then((response) => { // START OF LAMBDA FUNCTION
const { data } = response data.items.forEach((item) => {
console.log(item.id.videoId);
contentID = item.id.videoId;
});
// ----------------
// INSERT CODE HERE
// ----------------
// END OF LAMBDA FUNCTION
}).catch((err) => console.log(err));
希望这能解决您的问题。 :)