定制 discord-php-helpers/voice 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

discord-php-helpers/voice

Composer 安装命令:

composer require discord-php-helpers/voice

包简介

A voice library for DiscordPHP

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require codecov

PHP Discorders

Getting Started

Before you start using this Library, you need to know how PHP works, you need to know how Event Loops and Promises work. This is a fundamental requirement before you start. Without this knowledge, you will only suffer.

Requirements

DAVE (Discord Audio/Video End-to-End Encryption) runtime support

  • 📊 Visual DAVE Protocol Guide → — Mermaid diagrams covering architecture, MLS lifecycle, media encryption pipeline, and all DAVE opcodes.
  • 🎵 Audio Pipeline Guide → — Mermaid diagrams covering outbound/inbound audio flow, playback state machine, and format chain.
  • 📡 Protocol Reference → — Voice gateway opcodes, DAVE opcodes, and close codes reference table.
  • DAVE protocol negotiation is now supported at the voice gateway layer.
  • Binary DAVE voice opcodes are parsed and routed.
  • Required: ext-ffi must be enabled and libdave must be available — the runtime loads libdave session and media APIs and detects the maximum supported DAVE protocol version.
  • Use ./scripts/setup-libdave.sh to fetch the published discord/libdave release asset into .cache/libdave without vendoring the binary into git. The script auto-detects your OS and architecture (Linux, macOS, Windows — x64 and ARM64).
  • Export DISCORDPHP_DAVE_LIBRARY pointing to the platform library (e.g. .cache/libdave/lib/libdave.so on Linux) to force the runtime to use the repo-local shared library path.
  • CI uses the same setup script and enables ext-ffi so native DAVE coverage stays runnable.
  • libdave is required for voice connections. Manager::__construct() and the voice WebSocket throw LibDaveNotFoundException immediately when ext-ffi or a working libdave library cannot be loaded — there is currently no automatic fallback to protocol version 0. Discord has required the DAVE E2EE protocol for all voice and video connections since March 1st, 2026.

Local setup

COMPOSER_ROOT_VERSION=dev-main composer install --no-interaction --prefer-dist
./scripts/setup-libdave.sh

# Linux
export DISCORDPHP_DAVE_LIBRARY="$PWD/.cache/libdave/lib/libdave.so"
# macOS
# export DISCORDPHP_DAVE_LIBRARY="$PWD/.cache/libdave/lib/libdave.dylib"
# Windows (Git Bash / PowerShell)
# export DISCORDPHP_DAVE_LIBRARY="$PWD/.cache/libdave/bin/libdave.dll"

./vendor/bin/pest tests/Unit/Dave/RuntimeTest.php

The script auto-detects your OS and architecture (Linux, macOS, Windows — x64 and ARM64). Set DISCORDPHP_DAVE_LIBRARY to the path printed by the script. Use the bundled Pest runner for local validation. composer unit runs the default suite with TestDox output, while composer pest keeps the parallel full-suite command.

Composer Scripts

Script Description
composer unit Run the test suite (pest --testdox)
composer lint Run PHPLint syntax check
composer check Run CS-Fixer dry-run + PHPLint + tests (CI-safe, no rewrites)
composer cs Auto-format with PHP-CS-Fixer (rewrites files)
composer cs:check Check style without rewriting files
composer phpstan Run static analysis (level 5)
composer infection Run mutation testing (slow — local only)
composer coverage Run tests with Xdebug coverage report

Basic Example

$discord->on('init', function (Discord $discord) {
    $channel = $discord->getChannel('YOUR_CHANNEL_ID');

    $discord->voice->joinChannel($channel)->then(function (VoiceClient $vc) {
        $vc->on('ready', function () use ($vc) {
            $vc->playFile('/path/to/audio.mp3');
        });

        $vc->on('end', function () use ($vc) {
            $vc->disconnect();
        });
    });
});

Recording

// Raw PCM events — handle audio yourself
$discord->voice->joinChannel($channel)->then(function (VoiceClient $vc) use ($discord) {
    $vc->on('ready', function () use ($vc, $discord) {
        $vc->record();

        // Fires with raw 16-bit stereo 48 kHz PCM for each decoded Opus frame.
        $vc->on('channel-pcm', function (string $pcm) {
            // write $pcm to a file, pipe to an encoder, etc.
        });

        $discord->getLoop()->addTimer(10, function () use ($vc) {
            $vc->stopRecording();
            $vc->disconnect();
        });
    });
});
use Discord\Voice\Recording\RecordingFormat;

// Automatic per-user WAV files — no manual file handling needed
$discord->voice->joinChannel($channel)->then(function (VoiceClient $vc) use ($discord) {
    $vc->on('ready', function () use ($vc, $discord) {
        $vc->record(
            RecordingFormat::WAV,
            fn(string $userId) => "/tmp/recording_{$userId}.wav"
        );

        $discord->getLoop()->addTimer(10, function () use ($vc) {
            $vc->stopRecording(); // finalizes and closes all WAV files
            $vc->disconnect();
        });
    });
});

Recording formats: RecordingFormat::PCM emits raw PCM events (default). RecordingFormat::WAV writes self-contained WAV files per user (pure PHP). RecordingFormat::OGG writes OGG Opus files per user (requires ffmpeg).

See examples folder for full runnable scripts.

Documentation

Documentation for the latest version can be found here. Community contributed tutorials can be found on the wiki.

Troubleshooting

Having issues? See docs/TROUBLESHOOTING.md for solutions to common problems including missing libraries (libdave, ffmpeg, opus, libsodium), permission errors, and playback state issues.

Exceptions

All library exceptions implement the VoiceException marker interface, so you can catch any voice error with a single handler:

use Discord\Voice\Exceptions\VoiceException;

try {
    $manager->joinChannel($channel);
} catch (VoiceException $e) {
    echo "Voice error: " . $e->getMessage();
}

Common exceptions include LibDaveNotFoundException, EnterChannelDeniedException, CantSpeakInChannelException, FFmpegNotFoundException, and LibSodiumNotFoundException.

Contributing

We are open to contributions. However, please make sure you follow our coding standards (PSR-4 autoloading and custom styling). Please run php-cs-fixer before opening a pull request by running composer cs.

See CONTRIBUTING.md for setup, coding standards, and PR guidelines.

See CHANGELOG.md for version history.

License

MIT License, © David Cole and other contributors 2016-present.

discord-php-helpers/voice 适用场景与选型建议

discord-php-helpers/voice 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.45k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 discord-php-helpers/voice 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 discord-php-helpers/voice 我们能提供哪些服务?
定制开发 / 二次开发

基于 discord-php-helpers/voice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-24