javascript - 我试图向我的不和谐机器人添加播放命令,但是
问题描述
我正在尝试向我的不和谐机器人添加一个播放命令,以便它可以播放来自 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);
推荐阅读
- microsoft-graph-api - 频繁的 504 网关超时
- javascript - Json 不会通过 ajax 使用 api 对对象进行字符串化
- python - 在一行中创建列表字典的字典
- html - CSS 全高侧边栏
- flutter - Flutter:ScrollController initialScrollOffset 不起作用
- spring - spring kafka 嵌入式代理 - 我的实际听众永远不会被触发
- sql - 如何对 Big Query 表进行去重和修改?
- composer-php - 在 Prestashop 模块 1.7.6 中添加命令(控制台命令)
- excel - 保护 excel 工作表,但允许使用表单输入数据
- python-3.x - 循环通过排序的 dask 数据帧的最快方法是什么?