我试图向我的不和谐机器人添加播放命令,但是

Posted

技术标签:

【中文标题】我试图向我的不和谐机器人添加播放命令,但是【英文标题】:Im trying to add a play command to my discord bot but 【发布时间】:2020-12-06 07:37:17 【问题描述】:

我正在尝试向我的 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.

【问题讨论】:

【参考方案1】:

尝试删除.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);

【讨论】:

以上是关于我试图向我的不和谐机器人添加播放命令,但是的主要内容,如果未能解决你的问题,请参考以下文章

试图制作一个命令页面来列出我的不和谐机器人上的所有命令,但不知道如何[关闭]

如何让我的不和谐机器人等到当前歌曲结束而不是在添加新歌曲时跳过队列中的当前歌曲

如何让我的不和谐机器人播放 YouTube 上的音乐(无链接)?

如何使用 youtubedl 的搜索功能而不是 url 让我的不和谐机器人播放音乐? (Python)

如何在不和谐机器人中显示用户自己对他人推文的转发?

带有 youtube dl 的不和谐机器人音乐不会卡在网页下载中