簡體   English   中英

setTimeout js 和語音通道 discordjs

[英]setTimeout js and voice channel discordjs

首先,我很抱歉我的英語不好。 我正在開發一個不和諧的音樂機器人,我想添加一個功能,讓機器人在與語音通道斷開連接之前停留 30 秒(空閑時 = 所有音樂都結束了,不再播放)

async function play(guild, song) {
    const serverQueue = queue.get(guild.id);
    if (!song) {
        setTimeout(function() {serverQueue.voiceChannel.leave();}, 30000);
        queue.delete(guild.id);
        return;
    }
    console.log(serverQueue.songs);
    const dispatcher = serverQueue.connection.playOpusStream(await ytdl(song.url))
        .on('end', reason => {
            console.log(reason)
            serverQueue.songs.shift()
            play(guild, serverQueue.songs[0])
        })
        .on('error', error => console.error(error));
    serverQueue.textChannel.send(`\`\`\`Start playing : ${song.title}\`\`\``);
}

但我想讓它立即與語音通道斷開連接並結束一切,當使用“停止”命令時

else if (command === 'play') {

    const voiceChannel = message.member.voiceChannel;
    ...
}
else if (command === 'stop') {
    if (!message.member.voiceChannel) return message.channel.send("you are not in a voice channel")
    if (!serverQueue) return message.channel.send("nothing to stop!")
    serverQueue.songs = []
    serverQueue.connection.dispatcher.end();

    serverQueue.voiceChannel.leave();

    return undefined;
}
else if (command === 'next') {
    if (!message.member.voiceChannel) return message.channel.send("you are not in a voice channel")
    if (!serverQueue) return message.channel.send("nothing!")
    serverQueue.connection.dispatcher.end();
    message.channel.send("```Next music!```");
    return undefined;
}

當它結束播放一段音樂時,它會等待 30 秒,然后斷開連接,Ok! 但問題是:如果有人在等待 30 秒時使用“播放”命令,它會播放他的音樂曲目,但會在 30 秒后剪切所有內容(即使第二首曲目沒有結束)

.

您需要將setTimeout分配給一個變量。 然后再玩的時候用clearTimeout函數。 取消serverQueue.voiceChannel.leave()

文檔在這里解釋了如何使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM