Discord.net: voiceChannel.ConnectAsync(true, false, true) returns null

77 Views Asked by At
 var voiceChannel = (command.User as IGuildUser).VoiceChannel != null
     ? (command.User as IGuildUser).VoiceChannel
     : null;

 if (voiceChannel != null)
 {
     var audioClient = await voiceChannel.ConnectAsync(true, false, false);
 }

if the 'external' parameter of the ConnectAsync method is false ConnectAsync(true, false, FALSE) the bot connects to the voice channel and gives an error: Gateway A SlashCommandExecuted handler is blocking the gateway task. It stays in the voice channel for a few more seconds and exits with the following error:

Gateway A SlashCommandExecuted handler has thrown an unhandled exception....: System.TimeoutException: The operation has timed out.
at Discord.WebSocket.SocketGuild.ConnectAudioAsync(UInt64 channelId, Boolean selfDeaf, Boolean selfMute, Boolean external) at Discord.WebSocket.SocketGuild.ConnectAudioAsync(UInt64 channelId, Boolean selfDeaf, Boolean selfMute, Boolean external) at Program.HandlePlayCommand(SocketSlashCommand command) in C:\Users\ol1Hu\Workbench\Files\code\code\c#\TestDSBot\Program.cs:line 210 at Program.SlashCommandHandler(SocketSlashCommand command) in C:\Users\ol1Hu
Desktop\Files\code\code\code\c#\TestDSBot\Program.cs:line 136 at Discord.EventExtensions.InvokeAsync[T](AsyncEvent1 eventHandler, T arg) at Discord.WebSocket.DiscordSocketClient.TimeoutWrap(String name, Func1 action)

If the 'external' parameter of the ConnectAsync method is true ConnectAsync(true, false, TRUE) the ConnectAsync method simply returns null, but the bot connects to the voice channel without any errors.

The original goal was to create a music bot. The code is made almost exactly according to discord.net documentation.

private async Task HandlePlayCommand(SocketSlashCommand command)
{
    var voiceChannel = (command.User as IGuildUser).VoiceChannel != null
        ? (command.User as IGuildUser).VoiceChannel
        : null;

    if (voiceChannel != null)
    {
        await command.RespondAsync(embed: new EmbedBuilder { Description = "Please wait..." }.Build(), ephemeral: true);
        var audioClient = await voiceChannel.ConnectAsync(true, false, false);
        try
        {
            using (var ffmpeg = CreateStream(@"path to file"))
            using (var output = ffmpeg.StandardOutput.BaseStream)
            using (var discord = audioClient.CreatePCMStream(AudioApplication.Mixed))
            {
                try
                {
                    await output.CopyToAsync(discord);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally { await discord.FlushAsync(); }
            }
            await command.RespondAsync(embed: new EmbedBuilder { Description = "Ready!" }.Build(), ephemeral: true);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    else
        await command.RespondAsync(embed: new EmbedBuilder { Description = "You must be in the voice channel!" }.Build(), ephemeral: true);
}

private Process CreateStream(string path)
{
    return Process.Start(new ProcessStartInfo
    {
        FileName = "ffmpeg",
        Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
        UseShellExecute = false,
        RedirectStandardOutput = true,
    });
}
0

There are 0 best solutions below