即使其他人加入了频道,也继续播放歌曲

Posted

技术标签:

【中文标题】即使其他人加入了频道,也继续播放歌曲【英文标题】:keep playing the song even if someone else joined the channel 【发布时间】:2022-01-21 13:30:40 【问题描述】:

您好,我正在尝试让机器人播放特定歌曲,然后我希望机器人离开频道并且我做到了,但问题是当机器人播放时其他人加入频道时重新开始播放歌曲

client.on('voiceStateUpdate', async (oldState, newState) => 
    const channel = client.channels.cache.get('921118985876017217')
    if (oldState.channel == null && newState.channel == channel) 
  await new Promise(function (resolve , reject) 
            resolve(
                joinVoiceChannel(
                    channelId: channel.id,
                    guildId: newState.guild.id,
                    adapterCreator: newState.guild.voiceAdapterCreator
                ))
            let stream = ytdl('https://www.youtube.com/watch?v=TU1i6I1ms6s', 
                filter: "audioonly",
                fmt: "mp3",
                encoderArgs: ['-af', 'bass=g=10'],
            )

            let secStream = ytdl('https://www.youtube.com/watch?v=wHqKkiHlvJc&list=RDwHqKkiHlvJc&start_radio=1', 
                filter: "audioonly",
                fmt: "mp3",
                encoderArgs: ['-af', 'bass=g=10'],
            )
         
            const resource = createAudioResource(stream, 
                inputType: StreamType.Arbitrary,
                inlineVolume: true,
            );
            // resource.volume.setVolume(0.5);
            const secReasorce = createAudioResource(secStream, 
                inputType: StreamType.Arbitrary, /* StreamType.Arbitrary type of the song like mpe */
                inlineVolume: true,
            );
            const player = createAudioPlayer();
            player.play(resource)
            const connection = getVoiceConnection(oldState.guild.id)
            connection.subscribe(player)
            player.on(AudioPlayerStatus.Idle, () =>setTimeout(() => 
                try
                    player.play(secReasorce)
                catch
                    connection.destroy()
                
            , 3000))
            player.on( AudioPlayerStatus.Playing , ()=>
                //I think there will be the solution but I don't know any functions does what I want
             )
        )

【问题讨论】:

【参考方案1】:

您必须跟踪它是否已经在播放音频。由于您是根据 ID 获取频道的;我假设您只在一个语音通道中使用它。所以你可以在 lambda 表达式的范围之外有一个布尔值来跟踪它是否正在播放。

如果你实现它,你会有这样的东西:

let isPlaying = false; // Create a variable to keep track of whether it's playing or not.

client.on('voiceStateUpdate', async (oldState, newState) => 

    // If it's already playing, return and do nothing.
    if (isPlaying) return;

    const channel = client.channels.cache.get('921118985876017217')
    if (oldState.channel == null && newState.channel == channel) 
            await new Promise(function (resolve , reject) 
            resolve(
                joinVoiceChannel(
                    channelId: channel.id,
                    guildId: newState.guild.id,
                    adapterCreator: newState.guild.voiceAdapterCreator
                ))

            isPlaying = true; // Bot successfully joined vc, it now begins to play audio.

            // Removed code unrelated to the answer so you can more easily see the changes.

            player.on(AudioPlayerStatus.Idle, () =>setTimeout(() => 
                try
                    player.play(secReasorce)
                catch
                    // I assume this is where you make it disconnect? 
                    // If so, retain the integrity of `isPlaying` by setting it back to false.
                    isPlaying = false;
                    connection.destroy()
                
            , 3000))
        )```

【讨论】:

以上是关于即使其他人加入了频道,也继续播放歌曲的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法让我的不和谐机器人在播放完歌曲后断开与语音频道的连接?

机器人加入公会的所有语音频道并播放文件

如何使用 Discord.py Cogs 使 Discord Bot 加入语音频道并在成员加入频道时播放音频文件

Discord.py music_cog,机器人加入频道但不播放声音

运行播放命令时未定义的歌曲

单击后退按钮iOS 6时如何继续播放音频