如何解决我试图向我的不和谐机器人添加播放命令,但
我正在尝试将播放命令添加到我的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
是承诺的返回值。因此会引发错误。
尝试以下代码:
const connection = await message.member.voice.channel.join();
const dispatcher = connection.play('audio.mp3');
dispatcher.on('start',() => {});
dispatcher.on('error',console.error);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。