From ceff1cc3b929541b2f4a93acbf67d577f246f475 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Thu, 11 Jan 2018 01:16:30 +0100 Subject: [PATCH] Finish experimental version of voice utils --- .gitignore | 1 + Geekbot.net/Commands/Voice.cs | 7 ++++++ Geekbot.net/Lib/AudioClientCache.cs | 37 +++++++++++++++++++---------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 2e43789..c99d9e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ Geekbot.net/bin Geekbot.net/obj +Geekbot.net/tmp/ Tests/bin Tests/obj Backup/ diff --git a/Geekbot.net/Commands/Voice.cs b/Geekbot.net/Commands/Voice.cs index 6fbcaa9..cda1f8b 100644 --- a/Geekbot.net/Commands/Voice.cs +++ b/Geekbot.net/Commands/Voice.cs @@ -57,10 +57,12 @@ namespace Geekbot.net.Commands await audioClient.StopAsync(); await ReplyAsync("Disconnected from channel!"); + _audioUtils.Cleanup(Context.Guild.Id); } catch (Exception e) { _errorHandler.HandleCommandException(e, Context); + _audioUtils.Cleanup(Context.Guild.Id); } } @@ -80,15 +82,20 @@ namespace Geekbot.net.Commands await ReplyAsync("I'm not in a voice channel at the moment"); return; } + + var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); var output = ffmpeg.StandardOutput.BaseStream; + await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); await output.CopyToAsync(discord); await discord.FlushAsync(); + _audioUtils.Cleanup(Context.Guild.Id); } catch (Exception e) { _errorHandler.HandleCommandException(e, Context); + _audioUtils.Cleanup(Context.Guild.Id); } } } diff --git a/Geekbot.net/Lib/AudioClientCache.cs b/Geekbot.net/Lib/AudioClientCache.cs index 0869525..d43b98a 100644 --- a/Geekbot.net/Lib/AudioClientCache.cs +++ b/Geekbot.net/Lib/AudioClientCache.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Net; using System.Security.Cryptography; using Discord.Audio; using Discord.Net; @@ -16,11 +17,12 @@ namespace Geekbot.net.Lib public AudioUtils() { _audioClients = new Dictionary(); - _tempFolderPath = Path.GetFullPath("./temp/"); - if (!Directory.Exists(_tempFolderPath)) + _tempFolderPath = Path.GetFullPath("./tmp/"); + if (Directory.Exists(_tempFolderPath)) { - Directory.CreateDirectory(_tempFolderPath); + Directory.Delete(_tempFolderPath, true); } + Directory.CreateDirectory(_tempFolderPath); } public IAudioClient GetAudioClient(ulong guildId) @@ -49,7 +51,12 @@ namespace Geekbot.net.Lib { var ytdlMediaUrl = GetYoutubeMediaUrl(url); DownloadMediaUrl(ytdlMediaUrl, guildId); - return CreateStreamFromFile($"{_tempFolderPath}\\{guildId}.mp3"); + return CreateStreamFromFile($"{_tempFolderPath}{guildId}"); + } + + public void Cleanup(ulong guildId) + { + File.Delete($"{_tempFolderPath}{guildId}"); } private string GetYoutubeMediaUrl(string url) @@ -64,21 +71,26 @@ namespace Geekbot.net.Lib var output = Process.Start(ytdl).StandardOutput.ReadToEnd(); if (string.IsNullOrWhiteSpace(output)) { - throw new System.Exception("Could not get Youtube Media URL"); + throw new Exception("Could not get Youtube Media URL"); } return output; } private void DownloadMediaUrl(string url, ulong guildId) { - var ffmpeg = new ProcessStartInfo + using (var web = new WebClient()) { - FileName = "ffmpeg", - Arguments = $"-re -i \"${url}\" -c:a mp3 -b:a 256k {_tempFolderPath}\\{guildId}.mp3", - UseShellExecute = false, - RedirectStandardOutput = true, - }; - Process.Start(ffmpeg).WaitForExit(); + web.DownloadFile(url, $"{_tempFolderPath}{guildId}"); + } +// var ffmpeg = new ProcessStartInfo +// { +// FileName = "ffmpeg", +// Arguments = $"-i \"{_tempFolderPath}{guildId}\" -c:a mp3 -b:a 256k {_tempFolderPath}{guildId}.mp3", +// UseShellExecute = false, +// RedirectStandardOutput = true, +// }; +// Process.Start(ffmpeg).WaitForExit(); +// File.Delete($"{_tempFolderPath}{guildId}"); return; } } @@ -89,5 +101,6 @@ namespace Geekbot.net.Lib void StoreAudioClient(ulong guildId, IAudioClient client); Process CreateStreamFromFile(string path); Process CreateStreamFromYoutube(string url, ulong guildId); + void Cleanup(ulong guildId); } } \ No newline at end of file