簡體   English   中英

Discord.js bot 加入語音通道但加入后不會運行剩余代碼

[英]Discord.js bot joining voice channel but wont run the remaining code after joining

我有一個 discord 機器人,我正試圖加入一個語音頻道並讓它重復一個聲音文件,到目前為止我已經加入但在它加入后,箭頭 function 中的代碼都沒有運行

let channel = client.channels.cache.get('723620872572895243')

channel.join(connection => {
    console.log("Starting")
    mp3("speech.mp3", function (err, duration) {
        if (err) return console.log(err);
        console.log("File duration:" + duration * 1000 + "ms")
        repeat(connection, duration)
    })
}).catch(console.error)

這是我試圖運行的代碼,但它加入了通道,並且在箭頭 function 運行后什么也沒有

這是 repeat() function 以防萬一

function repeat(connection, duration) {
const dispatcher = connection.play("speech.mp3")
let play = setInterval(function () {
    const dispatcher = connection.play("speech.mp3")
    console.log("Playing")
}, duration * 1000 + 2000)
module.exports.interval = play
}

VoiceChannel#join不帶任何參數。 您還沒有正確形成箭頭 function ,這就是為什么您的代碼都不起作用的原因,您需要在.join() .then()之后使用 .then() ,如下所示:

let channel = client.channels.cache.get('723620872572895243')

channel.join().then(connection => {
    console.log("Starting")
    mp3("speech.mp3", function (err, duration) {
        if (err) return console.log(err);
        console.log("File duration:" + duration * 1000 + "ms")
        repeat(connection, duration)
    });
}).catch(console.error)

您可以在此處查看有關VoiceChannel#join方法的更多信息

暫無
暫無

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

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