0

I was writing a discord bot, and i wanted to make a script where if an used joins a voice channel, the bot would also join. I added this code:

_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
                {
                    ConnectToVoice(state2.VoiceChannel).Start();
                }
                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}");
        }

It builds fine, and when i join the channel, the bot follows, but it soon leaves and the console gives me this error: System.InvalidOperationException: Start may not be called on a promise-style task.

Any ideas as to why?

JEREDEK
  • 87
  • 1
  • 9
  • 1
    i.e. `ConnectToVoice(state2.VoiceChannel).Start();` --> `await ConnectToVoice(state2.VoiceChannel);` – JHBonarius Sep 18 '20 at 18:17
  • Yes, but now, when i connect, the bot connects, but soon disconnects with these 3 errors : ```Connecting to channel *channel id*``` ```22:12:06 Gateway A UserVoiceStateUpdated handler is blocking the gateway task.``` ```System.TimeoutException: The operation has timed out.``` – JEREDEK Sep 18 '20 at 20:13

0 Answers0