Reputation: 271
I am using Discord.Net Api to make a bot the connects to a discord server and I want to bot to play music and audio.
I can make the bot join a channel and it works great, but when I try to get the IAudioClient
for the channel(from the IAudioClient
I can get the audio output stream so I can write the music to it and that's what transfers the music) , I get an exception.
This is my code for joining the channel and then getting the IAudioClient
for the channel:
[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);
}
}
I don't know if it makes the problem here, but I installed only the "ffmpeg" for the audio transmitting, but so far I don't see any problems with that, only with getting the audio channel. I used this tutorial: https://discord.foxbot.me/docs/guides/voice/sending-voice.html but on the top of the tutorial you can see a caption which says that the tutorial is out of date..
the exception I get is 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()
Upvotes: 1
Views: 1896
Reputation: 271
The method await channel.ConnectAsync()
can take some time until it returns a value, so I just noticed that I can write another method inside the brackets and that other method with get the needed IAudioClient
like that:
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();
}
Upvotes: 0
Reputation: 156
If your command is timing out, it's because 'async' isn't exactly async. You need to set the RunMode of your command to Async
[Command("name",RunMode = RunMode.Async)]
Upvotes: 1