简体   繁体   English

Discord.js bot 加入语音通道但加入后不会运行剩余代码

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

I have a discord bot that I'm trying to get to join a voice channel and have it repeat a sound file, so far I have got it to join but after it joins, none of the code in the arrow function is run我有一个 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)

This is the code I'm trying to run but it joins the channel and nothing after the arrow function is run这是我试图运行的代码,但它加入了通道,并且在箭头 function 运行后什么也没有

Here is the repeat() function in case it is needed这是 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 takes no parameters. VoiceChannel#join不带任何参数。 You haven't formed your arrow function correctly, which is why none of your code is working, you need to have the .then() after the .join() like this:您还没有正确形成箭头 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)

You can see more about the VoiceChannel#join method here您可以在此处查看有关VoiceChannel#join方法的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM