Remove AudioUtils
This commit is contained in:
parent
68a62ab4ec
commit
c6271cbaa0
4 changed files with 0 additions and 215 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ulong, IAudioClient> _audioClients;
|
||||
|
||||
public AudioUtils()
|
||||
{
|
||||
_audioClients = new Dictionary<ulong, IAudioClient>();
|
||||
_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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
|
@ -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<IMalClient>(malClient);
|
||||
_services.AddSingleton<IMtgManaConverter>(mtgManaConverter);
|
||||
_services.AddSingleton<IWikipediaClient>(wikipediaClient);
|
||||
_services.AddSingleton<IAudioUtils>(audioUtils);
|
||||
_services.AddSingleton<IRandomNumberGenerator>(randomNumberGenerator);
|
||||
_services.AddSingleton<IHighscoreManager>(_highscoreManager);
|
||||
_services.AddSingleton<IGlobalSettings>(_globalSettings);
|
||||
|
|
Loading…
Reference in a new issue