加入语音通道后,C#Discord.net Bot断开连接

时间:2020-09-18 20:27:18

标签: c# discord.net

我写了一个discord bot,我希望它在用户加入后加入语音通道,但是它的作用是加入它,等待一秒钟,然后断开连接,控制台中弹出以下错误:< / p>

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 Discord.WebSocket.SocketVoiceChannel.ConnectAsync(Boolean selfDeaf, Boolean selfMute, Boolean external)
   at DiscordBot2.Program.ConnectToVoice(SocketVoiceChannel voiceChannel)
   at DiscordBot2.Program.OnVoiceStateUpdated(SocketUser user, SocketVoiceState state1, SocketVoiceState state2)

这是我使用的代码:

// called before calling StartAsync
_client.UserVoiceStateUpdated += OnVoiceStateUpdated;

private async Task OnVoiceStateUpdated(SocketUser user, SocketVoiceState state1, SocketVoiceState state2)
        {
            // Check if this was a non-bot user joining a voice channel
            if (user.IsBot)
                return;

            if (state1.VoiceChannel == null && state2.VoiceChannel != null)
            {
                try
                {
                    await ConnectToVoice(state2.VoiceChannel);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }

        private async Task ConnectToVoice(SocketVoiceChannel voiceChannel)
        {
            if (voiceChannel == null)
                return;

            Console.WriteLine($"Connecting to channel {voiceChannel.Id}");
            var connection = await voiceChannel.ConnectAsync();
            Console.WriteLine($"Connected to channel {voiceChannel.Id}");
        }

0 个答案:

没有答案