geekbot/Geekbot.net/Commands/Audio/Voice.cs

101 lines
3.5 KiB
C#
Raw Normal View History

2018-05-04 00:54:01 +02:00
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;
}
2018-05-04 23:34:47 +02:00
// [Command("join")]
2018-05-04 00:54:01 +02:00
public async Task JoinChannel()
{
try
{
// Get the audio channel
var channel = (Context.User as IGuildUser)?.VoiceChannel;
if (channel == null)
{
2018-05-04 23:34:47 +02:00
await Context.Channel.SendMessageAsync("You must be in a voice channel.");
2018-05-04 00:54:01 +02:00
return;
}
var audioClient = await channel.ConnectAsync();
_audioUtils.StoreAudioClient(Context.Guild.Id, audioClient);
await ReplyAsync($"Connected to {channel.Name}");
}
catch (Exception e)
{
2018-07-28 16:31:18 +02:00
await _errorHandler.HandleCommandException(e, Context);
2018-05-04 00:54:01 +02:00
}
}
2018-05-04 23:34:47 +02:00
// [Command("disconnect")]
2018-05-04 00:54:01 +02:00
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);
2018-05-04 00:54:01 +02:00
_audioUtils.Cleanup(Context.Guild.Id);
}
}
2018-05-04 23:34:47 +02:00
// [Command("ytplay")]
public async Task Ytplay(string url)
2018-05-04 00:54:01 +02:00
{
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);
2018-05-04 00:54:01 +02:00
_audioUtils.Cleanup(Context.Guild.Id);
}
}
}
}