discord-php / DiscordPHP

An API to interact with the popular messaging app Discord
MIT License
997 stars 237 forks source link

Audio sometimes is not audible for a few seconds #855

Closed SebastianSchoeps closed 2 years ago

SebastianSchoeps commented 2 years ago

We have a bot that plays audio files into a channel. Everything works fine, except sometimes the audio is not audible for the first few seconds. After some seconds it becomes audible (it doesn't start delayed; it's just as it is muted for a few seconds). This happens randomly - the same audio file works several times and then the next time doesn't. It is also the same for all members in the channel.

I am not sure if this is an issue related to the framework or not - we are currently trying to learn Node JS to recreate this issue with DiscordJS...

We stripped down the functionality to a pretty straight-forward version that still has the problems:

$discord->on('ready', function(Discord $discord) {
    // Register command
    $command = new \Discord\Parts\Interactions\Command\Command($discord, [
        'name' => 'a',
        'description' => 'Play random audio', 
    ]);
    $discord->application->commands->save($command);

    $discord->on(Event::INTERACTION_CREATE, function(Interaction $interaction, Discord $discord) {
        if($interaction->data->name == 'a') {
            $file = 'counting';

            if($voiceClient = $discord->getVoiceClient($interaction->guild_id)) {
                if($voiceClient->isSpeaking()) {
                    $voiceClient->stop();
                }

                $resource = fopen($file, 'r');
                $voiceClient->playDCAStream($resource);
            }
            else {
                $voiceChannel = collect($interaction->guild->channels->toArray())->firstWhere('type', Channel::TYPE_VOICE);

                $discord->joinVoiceChannel($voiceChannel)
                    ->then(function(VoiceClient $voiceClient) use ($voiceChannel, $file) {
                        $resource = fopen($file, 'r');
                        $voiceClient->playDCAStream($resource);
                    });
            }

            $builder = MessageBuilder::new();
            $builder->setContent('Playing count from 1 to 10...');
            $interaction->respondWithMessage($builder);
        }
    });
});

The test bot is available at https://discord.com/api/oauth2/authorize?client_id=993817984487403540&permissions=2064&scope=bot%20applications.commands. You just use /a to play an audio file counting from 1 to 10 (the "1" is on very low volume, just ignore this). You don't have to wait until the audio is finished - just use /a again.

Can anyone point us in the direction how to trouble-shoot this?

Thanks in advance! Sebastian

SebastianSchoeps commented 2 years ago

Our DiscordJS bot running on the same server did not show any issue like this. It seems like this has to do with how this framework does the streaming. Is anything known about issues like this?

key2peace commented 2 years ago

which operating system are you using?

SebastianSchoeps commented 2 years ago

We are running on Ubuntu 22.04.

We have now switched to calling DiscordJS to play the audio which works fine.