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?