gpt4 book ai didi

javascript - 我正在尝试向我的不和谐机器人添加播放命令,但是

转载 作者:行者123 更新时间:2023-12-01 16:16:40 26 4
gpt4 key购买 nike

我正在尝试向我的 discord 机器人添加一个播放命令,以便它以后可以播放来自 youtube 和其他地方的音乐,但现在我只是尝试让它播放我电脑上的音频文件以测试我是否可以制作它在进入更复杂的东西之前加入并播放一些东西,比如弄清楚如何让它从 youtube url 播放歌曲/音频

现在,这就是我拥有的

Crashbot.on('message', async message => {

let args = message.content.substring(PREFIX.length).split(" ");

switch (args[0]) {
case 'play':
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join().then(() => {

const dispatcher = connection.play('audio.mp3');

dispatcher.on('start', () => {

});

dispatcher.on('error', console.error);

})
}
}

})

每次运行都会报错

(node:18884) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'connection' before initialization
at C:\Users\Michael\Desktop\Crashbot\index.js:213:40
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Client.<anonymous> (C:\Users\Michael\Desktop\Crashbot\index.js:211:36)
(node:18884) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:18884) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

最佳答案

尝试删除 .then(...)。因为有await,所以不用.then()也能正常运行。在您的代码中,connection 是 promise 的返回值。所以它会抛出一个错误。

试试这段代码:

const connection = await message.member.voice.channel.join();

const dispatcher = connection.play('audio.mp3');

dispatcher.on('start', () => {});

dispatcher.on('error', console.error);

关于javascript - 我正在尝试向我的不和谐机器人添加播放命令,但是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63446241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com