From 82f07c959dc7a87251b4617e462003471e3cc071 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 9 Apr 2018 00:54:19 +0200 Subject: Refactoring and Cleanup --- DiscoBot/AudioService.cs | 88 ------------------------------------------------ 1 file changed, 88 deletions(-) delete mode 100644 DiscoBot/AudioService.cs (limited to 'DiscoBot/AudioService.cs') diff --git a/DiscoBot/AudioService.cs b/DiscoBot/AudioService.cs deleted file mode 100644 index bb4d21e..0000000 --- a/DiscoBot/AudioService.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System.Collections.Concurrent; -using System.Diagnostics; -using System.IO; -using System.Threading.Tasks; -using Discord; -using Discord.Audio; - -namespace DiscoBot -{ - 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)) - { - // If you add a method to log happenings from this service, - // you can uncomment these commented lines to make use of that. - //await Log(LogSeverity.Info, $"Connected to voice on {guild.Name}."); - } - } - - public async Task LeaveAudio(IGuild guild) - { - IAudioClient client; - if (ConnectedChannels.TryRemove(guild.Id, out client)) - { - await client.StopAsync(); - //await Log(LogSeverity.Info, $"Disconnected from voice on {guild.Name}."); - } - } - - public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path) - { - // Your task: Get a full path to the file if the value of 'path' is only a filename. - if (!File.Exists(path)) - { - await channel.SendMessageAsync("File does not exist."); - return; - } - - IAudioClient client; - if (ConnectedChannels.TryGetValue(guild.Id, out client)) - { - //await Log(LogSeverity.Debug, $"Starting playback of {path} in {guild.Name}"); - using (var ffmpeg = Process.Start(path)) - using (var stream = client.CreatePCMStream(AudioApplication.Music)) - { - try - { - await ffmpeg.StandardOutput.BaseStream.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 - }); - } - } -} -- cgit v1.2.3-70-g09d2