-
-
Notifications
You must be signed in to change notification settings - Fork 315
Description
Summary
I used a 5.0.0-nightly-02262 build for a long time, but recently I discovered that the bot cannot handle the voice channels (just hanging in the events). I decided to update the library to the latest version which I did already, and there is an issue.
To thansmit the music data to the voice channel the bot is connected to, I just push the FFMPEG data output to the VoiceNext TransmitSink via TransmitSink.WriteAsync method. But now the is noting in the bot audio output. Maybe I am missing something and/or there is more to do with the workaround between FFMPEG and TransmitSink, but I cannot find any similar issues on the issues and discussions tabs.
What version of the library are you using?
v5.0.0-nightly (make sure you are using the latest nightly!)
What .NET version are you using? Make sure to use the latest patch release for your major version.
.NET 9.0
Operating System
Windows 10
Reproduction Steps
Connect the bot to the desired voice channel:
var guild = CommandContext?.Guild;
if (guild is null)
{
return;
}
if (_guild != guild)
{
return;
}
DiscordVoiceState? state = CommandContext?.Member?.VoiceState;
if (state is null)
{
return;
}
ulong? id = state.ChannelId;
if (!id.HasValue)
{
return;
}
DiscordChannel? channel = await guild.GetChannelAsync(id.Value);
if (channel is null)
{
return;
}
Task<VoiceNextConnection> task = channel.ConnectAsync();
_ = task.Wait(2000);
VoiceNextConnection? Connection = task.IsCompletedSuccessfully ? task.Result : null;
if (Connection is null)
{
return;
}
while (Connection.TargetChannel is null)
{
await Task.Delay(1);
}
while (true)
{
try
{
Connection.SendSpeakingAsync(true).Wait();
break;
}
catch
{
await Task.Delay(1);
}
}
Push the data to the transmit sink:
VoiceTransmitSink TransmitSink = Connection.GetTransmitSink();
// The player logic is too complicated, and I tried to use the direct data preparation
const int durationSeconds = 5;
const int sampleRate = 48000;
const short amplitude = 15000;
const double frequency = 440.0;
var totalSamples = sampleRate * durationSeconds;
var pcmData = new byte[totalSamples * 4];
for (int i = 0; i < totalSamples; i++)
{
double time = (double)i / sampleRate;
short sample = (short)(amplitude * Math.Sin(2 * Math.PI * frequency * time));
int byteIndex = i * 4;
pcmData[byteIndex] = (byte)(sample & 0xFF);
pcmData[byteIndex + 1] = (byte)((sample >> 8) & 0xFF);
pcmData[byteIndex + 2] = (byte)(sample & 0xFF);
pcmData[byteIndex + 3] = (byte)((sample >> 8) & 0xFF);
}
using var memoryStream = new MemoryStream(pcmData);
memoryStream.Position = 0;
byte[] buffer = new byte[3840];
int bytesRead;
while ((bytesRead = await memoryStream.ReadAsync(buffer)) > 0)
{
if (bytesRead < buffer.Length)
{
Array.Clear(buffer, bytesRead, buffer.Length - bytesRead);
}
await TransmitSink.WriteAsync(buffer, buffer.Length);
await Task.Delay(20);
}
Trace Logs
On application start:
[2025-10-04 12:49:30 +00:00] [DSharpPlus.DiscordClient] [Info] DSharpPlus; version 5.0.0-nightly-02551+39527ed9f52a75c332df24d3b854e6f42d8751c9
[2025-10-04 12:49:30 +00:00] [DSharpPlus.Net.Gateway.ITransportService] [Debug] Connected to the Discord websocket, using zlib-stream compression.
[2025-10-04 12:49:30 +00:00] [DSharpPlus.Net.Gateway.IGatewayClient] [Debug] Received hello event, starting heartbeating with an interval of 00:00:41.2500000 and identifying.
[2025-10-04 12:49:30 +00:00] [DSharpPlus.Net.Gateway.IGatewayClient] [Debug] Identified with the Discord gateway
[2025-10-04 12:49:31 +00:00] [DSharpPlus.Net.Gateway.IGatewayClient] [Debug] Received READY, the gateway is now operational.
...
On command execution:
[2025-10-04 12:50:09 +00:00] [DSharpPlus.DiscordClient] [Debug] Timer accuracy: 10000000/50000 (high resolution? True)
Exceptions or other error messages
There are no unhandled runtime exceptions thrown during the operation.
The DiscordClient logging level is LogLevel.Debug. No errors printed to the console output and error streams.
Anything else you'd like to share
No response