Skip to content

Instantly share code, notes, and snippets.

@ItemME
Forked from Joe4evr/AudioModule.cs
Created October 4, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ItemME/ddc3a4c67fb9710485d43e662d7a99e5 to your computer and use it in GitHub Desktop.
Save ItemME/ddc3a4c67fb9710485d43e662d7a99e5 to your computer and use it in GitHub Desktop.
D.Net 1.0 audio example
using System.Threading.Tasks;
using Discord.Commands;
public class AudioModule : ModuleBase<ICommandContext>
{
// Scroll down further for the AudioService.
// Like, way down
private readonly AudioService _service;
// Remember to add an instance of the AudioService
// to your IServiceCollection when you initialize your bot
public AudioModule(AudioService service)
{
_service = service;
}
// You *MUST* mark these commands with 'RunMode.Async'
// otherwise the bot will not respond until the Task times out.
[Command("join", RunMode = RunMode.Async)]
public async Task JoinCmd()
{
await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
}
// Remember to add preconditions to your commands,
// this is merely the minimal amount necessary.
// Adding more commands of your own is also encouraged.
[Command("leave", RunMode = RunMode.Async)]
public async Task LeaveCmd()
{
await _service.LeaveAudio(Context.Guild);
}
[Command("play", RunMode = RunMode.Async)]
public async Task PlayCmd([Remainder] string song)
{
await _service.SendAudioAsync(Context.Guild, Context.Channel, song);
}
}
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Discord;
using Discord.Audio;
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))
{
// If you add a method to log happenings from this service,
// you can uncomment the following line to make use of that.
//await Log(LogSeverity.Info, $"Connected to voice on {guild.Name}.");
}
}
public async Task LeaveAudio(IGuild guild)
{
IAudioClient client;
if (ConnectedChannels.TryRemove(guild.Id, out client))
{
await client.StopAsync();
//await Log(LogSeverity.Info, $"Disconnected from voice on {guild.Name}.");
}
}
public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path)
{
// Your task: Get a full path to the file if the value of 'path' is only a filename.
if (!File.Exists(path))
{
await channel.SendMessageAsync("File does not exist.");
return;
}
IAudioClient client;
if (ConnectedChannels.TryGetValue(guild.Id, out client))
{
//await Log(LogSeverity.Debug, $"Starting playback of {path} in {guild.Name}");
var output = CreateStream(path).StandardOutput.BaseStream;
// You can change the bitrate of the outgoing stream with an additional argument to CreatePCMStream().
// If not specified, the default bitrate is 96*1024.
var stream = client.CreatePCMStream(AudioApplication.Music);
await output.CopyToAsync(stream);
await stream.FlushAsync().ConfigureAwait(false);
}
}
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
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment