当我尝试获取频道的 IAudioClient 时程序崩溃

Program crashes when I try to get the IAudioClient for a channel

我正在使用 Discord.Net Api 让机器人连接到 discord 服务器,我想让机器人播放音乐和音频。 我可以让机器人加入频道并且效果很好,但是当我尝试获取频道的 IAudioClient 时(从 IAudioClient 我可以获得音频输出流,这样我就可以将音乐写入它,这就是传输音乐的原因),我得到一个例外。 这是我加入频道然后获取频道的 IAudioClient 的代码:

    [Command("join")]
    public async Task JoinChannel(IVoiceChannel channel = null)
    {
        channel = channel ?? (callingUser as IGuildUser)?.VoiceChannel;
        if(channel == null)
        {
            await callingMsg.Channel.SendMessageAsync("You must be in a voice channel.");
            return;
        }

        Console.WriteLine("Channel: " + channel.Name + " - " + channel.Id);
        try
        {
            audioClient = await channel.ConnectAsync(); //crashes here
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

我不知道这里是否有问题,但我只安装了 "ffmpeg" 用于音频传输,但到目前为止我没有看到任何问题,只有获取音频渠道。我使用了这个教程:https://discord.foxbot.me/docs/guides/voice/sending-voice.html 但是在教程的顶部你可以看到一个标题,上面写着教程已经过时了..

我得到的异常是 The operation has timed out

21:39:18 Discord Discord.Net v1.0.1 (API v6) 21:39:19 Gateway Connecting 21:39:20 Gateway Connected 21:39:20 Gateway Ready Channel: תודה תודה תודה תודה תודה - 334069917047455747 21:39:29 Gateway A MessageReceived handler is blocking the gateway task. at Discord.WebSocket.SocketGuild.d__160.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Discord.WebSocket.SocketGuild.d__160.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Discord.WebSocket.SocketVoiceChannel.d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at ruin_counter.Program.d__9.MoveNext()

如果您的命令超时,那是因为 'async' 不完全是异步的。您需要将命令的 RunMode 设置为 Async

[Command("name",RunMode = RunMode.Async)]

方法 await channel.ConnectAsync() 可能需要一些时间才能 returns 一个值,所以我只是注意到我可以在括号内编写另一个方法,另一个方法可以获取所需的 IAudioClient 像那样: audioClient = await channel.ConnectAsync(SendAsync);

        private void SendAsync(IAudioClient audioClient)
    {
        var ffmpeg = CreateStream("tune.mp3");
        var output = ffmpeg.StandardOutput.BaseStream;
        var discord = audioClient.CreatePCMStream(AudioApplication.Mixed);
        output.CopyToAsync(discord);
        discord.FlushAsync();        
    }