diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs deleted file mode 100644 index b12d6e2..0000000 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib.Audio; -using Geekbot.net.Lib.ErrorHandling; - -namespace Geekbot.net.Commands.Audio -{ - public class Voice : ModuleBase - { - private readonly IAudioUtils _audioUtils; - private readonly IErrorHandler _errorHandler; - - public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) - { - _errorHandler = errorHandler; - _audioUtils = audioUtils; - } - -// [Command("join")] - public async Task JoinChannel() - { - try - { - // Get the audio channel - var channel = (Context.User as IGuildUser)?.VoiceChannel; - if (channel == null) - { - await Context.Channel.SendMessageAsync("You must be in a voice channel."); - return; - } - - var audioClient = await channel.ConnectAsync(); - _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); - await ReplyAsync($"Connected to {channel.Name}"); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - -// [Command("disconnect")] - public async Task DisconnectChannel() - { - try - { - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); - return; - } - - await audioClient.StopAsync(); - await ReplyAsync("Disconnected from channel!"); - _audioUtils.Cleanup(Context.Guild.Id); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - -// [Command("ytplay")] - public async Task Ytplay(string url) - { - try - { - if (!url.Contains("youtube")) - { - await ReplyAsync("I can only play youtube videos"); - return; - } - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - 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) - { - await _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - } - -} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/AudioUtils.cs b/Geekbot.net/Lib/Audio/AudioUtils.cs deleted file mode 100644 index 024019d..0000000 --- a/Geekbot.net/Lib/Audio/AudioUtils.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Net; -using Discord.Audio; - -namespace Geekbot.net.Lib.Audio -{ - public class AudioUtils : IAudioUtils - { - private string _tempFolderPath; - private Dictionary _audioClients; - - public AudioUtils() - { - _audioClients = new Dictionary(); - _tempFolderPath = Path.GetFullPath("./tmp/"); - if (Directory.Exists(_tempFolderPath)) - { - Directory.Delete(_tempFolderPath, true); - } - Directory.CreateDirectory(_tempFolderPath); - } - - public IAudioClient GetAudioClient(ulong guildId) - { - return _audioClients[guildId]; - } - - public void StoreAudioClient(ulong guildId, IAudioClient client) - { - _audioClients[guildId] = client; - } - - public Process CreateStreamFromFile(string path) - { - var ffmpeg = new ProcessStartInfo - { - FileName = "ffmpeg", - Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 pipe:1", - UseShellExecute = false, - RedirectStandardOutput = true, - }; - return Process.Start(ffmpeg); - } - - public Process CreateStreamFromYoutube(string url, ulong guildId) - { - var ytdlMediaUrl = GetYoutubeMediaUrl(url); - DownloadMediaUrl(ytdlMediaUrl, guildId); - return CreateStreamFromFile($"{_tempFolderPath}{guildId}"); - } - - public void Cleanup(ulong guildId) - { - File.Delete($"{_tempFolderPath}{guildId}"); - } - - private string GetYoutubeMediaUrl(string url) - { - var ytdl = new ProcessStartInfo() - { - FileName = "youtube-dl", - Arguments = $"-f bestaudio -g {url}", - UseShellExecute = false, - RedirectStandardOutput = true - }; - var output = Process.Start(ytdl).StandardOutput.ReadToEnd(); - if (string.IsNullOrWhiteSpace(output)) - { - throw new Exception("Could not get Youtube Media URL"); - } - return output; - } - - private void DownloadMediaUrl(string url, ulong guildId) - { - using (var web = new WebClient()) - { - 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; - } - - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/IAudioUtils.cs b/Geekbot.net/Lib/Audio/IAudioUtils.cs deleted file mode 100644 index 4af1293..0000000 --- a/Geekbot.net/Lib/Audio/IAudioUtils.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Diagnostics; -using Discord.Audio; - -namespace Geekbot.net.Lib.Audio -{ - public interface IAudioUtils - { - IAudioClient GetAudioClient(ulong guildId); - 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 diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 43c16d1..27274d2 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -9,7 +9,6 @@ using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.AlmostRedis; -using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; @@ -124,7 +123,6 @@ namespace Geekbot.net var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); - var audioUtils = new AudioUtils(); var randomNumberGenerator = new RandomNumberGenerator(); _highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); @@ -138,7 +136,6 @@ namespace Geekbot.net _services.AddSingleton(malClient); _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); - _services.AddSingleton(audioUtils); _services.AddSingleton(randomNumberGenerator); _services.AddSingleton(_highscoreManager); _services.AddSingleton(_globalSettings);