最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Play audio infinitely with discord.js - Stack Overflow

programmeradmin3浏览0评论

I'm making a discord bot that joins the channel while it's active and plays an audio clip on repeat.

I tried doing this by calling the function recursively:

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
let vc = client.channels.get(voiceChannel);

vc.join().then(connection => {

    function play (connection) {
        const stream = ytdl('', { filter: 'audioonly' });
        const dispatcher = connection.playStream(stream, streamOptions)
        dispatcher.on('end', play(connection));
    }

    play(connection)
})
});

but when I run it, I get this error:

Error: spawn ffmpeg EAGAIN
at _errnoException (util.js:999:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)
at onErrorNT (internal/child_process.js:389:16)
at process._tickCallback (internal/process/next_tick.js:152:19)

Can anyone tell me how I can put an audio loop on repeat forever with discord.js? Thanks!

I'm making a discord bot that joins the channel while it's active and plays an audio clip on repeat.

I tried doing this by calling the function recursively:

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
let vc = client.channels.get(voiceChannel);

vc.join().then(connection => {

    function play (connection) {
        const stream = ytdl('https://www.youtube./watch?v=fh0_sjZGJSc', { filter: 'audioonly' });
        const dispatcher = connection.playStream(stream, streamOptions)
        dispatcher.on('end', play(connection));
    }

    play(connection)
})
});

but when I run it, I get this error:

Error: spawn ffmpeg EAGAIN
at _errnoException (util.js:999:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)
at onErrorNT (internal/child_process.js:389:16)
at process._tickCallback (internal/process/next_tick.js:152:19)

Can anyone tell me how I can put an audio loop on repeat forever with discord.js? Thanks!

Share Improve this question asked Jul 11, 2018 at 9:19 Joshua WoodJoshua Wood 5356 silver badges20 bronze badges 1
  • Does it play once then fail to repeat, or a number of times, or not play at all? Can you try playing a stream that you already have locally rather than downloading it from YouTube every time? You could well be hitting a YouTube throttling limit as well as a Discord one. And it's worth working out whether it's ytdl that's calling ffmpeg or discord.js if you don't already know: ideally you'd get the audio into a format that Discord supports without any more conversion before you start playing. And finally what are your streamOptions set to? – Rup Commented Jul 11, 2018 at 9:35
Add a ment  | 

1 Answer 1

Reset to default 6

As soon as you join the voice channel you set the following listener:

dispatcher.on('end', play(connection));

Note that you are not giving a callback function here; instead you are calling play immediatly again. The error you are seeing is caused because you recursively spawned too many instances of FFMPEG (The library ytldl uses to transcode the video). To solve this you simply have to give an actual callback function like this:

dispatcher.on('end', () => { 
    play(connection);
});

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论