oh yeah woo yeah
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kehyeedra/Kehyeedra3/AudioService.cs

66 lines
2.2 KiB

using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Discord;
using Discord.Audio;
namespace Kehyeedra3
{
public class AudioService
{
private readonly ConcurrentDictionary<ulong, IAudioClient> ConnectedChannels = new ConcurrentDictionary<ulong, IAudioClient>();
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))
{
}
}
public async Task LeaveAudio(IGuild guild)
{
IAudioClient client;
if (ConnectedChannels.TryRemove(guild.Id, out client))
{
await client.StopAsync();
}
}
public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path)
{
if (!File.Exists(path))
{
await channel.SendMessageAsync("File does not exist.");
return;
}
IAudioClient client;
if (ConnectedChannels.TryGetValue(guild.Id, out client))
{
using (var output = CreateStream(path).StandardOutput.BaseStream)
using (var stream = client.CreatePCMStream(AudioApplication.Music))
{
try { await output.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
});
}
}
}