Discord.js音乐机器人对第一个音乐请求没有响应。

问题描述 投票:0回答:1

我最近一直在为我的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;
        }
    }
);
node.js youtube-api discord discord.js youtube-data-api
1个回答
0
投票

当在javascript中使用诺言时,任何依赖于诺言结果的东西都需要在lambda函数中提供给 .then(). 例子

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。这意味着它将在创建承诺后立即运行 "外部 "代码,无论它是否调用了 .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));

希望能解决你的问题:)

© www.soinside.com 2019 - 2024. All rights reserved.