using System.Collections.Concurrent; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Discord; using Discord.Audio; namespace Kehyeedra3 { public class AudioService { private readonly ConcurrentDictionary ConnectedChannels = new ConcurrentDictionary(); public async Task JoinAudio(IGuild guild, IVoiceChannel target) { IAudioClient client; if (ConnectedChannels.TryGetValue(guild.Id, out client)) { return; } if (target.Guild.Id != guild.Id) { return; } var audioClient = await target.ConnectAsync(); if (ConnectedChannels.TryAdd(guild.Id, audioClient)) { } } public async Task LeaveAudio(IGuild guild) { IAudioClient client; if (ConnectedChannels.TryRemove(guild.Id, out client)) { await client.StopAsync(); } } public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path) { if (!File.Exists(path)) { await channel.SendMessageAsync("File does not exist."); return; } IAudioClient client; if (ConnectedChannels.TryGetValue(guild.Id, out client)) { using (var output = CreateStream(path).StandardOutput.BaseStream) using (var stream = client.CreatePCMStream(AudioApplication.Music)) { try { await output.CopyToAsync(stream); } finally { await stream.FlushAsync(); } } } } private Process CreateStream(string path) { return Process.Start(new ProcessStartInfo { FileName = "ffmpeg.exe", Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1", UseShellExecute = false, RedirectStandardOutput = true }); } } }