【问题标题】:Discord.js bot joining voice channel but wont run the remaining code after joiningDiscord.js 机器人加入语音频道,但加入后不会运行剩余代码
【发布时间】:2021-05-03 16:17:13
【问题描述】:

我有一个不和谐的机器人,我正试图加入一个语音频道并让它重复一个声音文件,到目前为止我已经加入了它,但是在它加入后,箭头函数中的代码都不是运行

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)

这是我正在尝试运行的代码,但它加入了频道,并且在箭头函数运行后什么也没有

这里是repeat()函数,以备不时之需

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
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    VoiceChannel#join 不带参数。你没有正确地形成你的箭头函数,这就是为什么你的代码都没有工作,你需要在 .join() 之后有 .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方法here

    【讨论】:

    • 非常感谢,我不敢相信我忽略了这一点,我什至查看了文档上的 VoiceChannel 加入方法并完全跳过了 .then()
    猜你喜欢
    • 2021-12-06
    • 2023-02-25
    • 1970-01-01
    • 2020-10-24
    • 2020-07-16
    • 2022-01-04
    • 2021-07-13
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多