This commit is contained in:
2022-04-08 21:23:36 +03:00
parent d8d92ea56e
commit faab2ddc8a
6 changed files with 161 additions and 0 deletions

17
CMD_Utils/Music/Data.cs Normal file
View File

@@ -0,0 +1,17 @@
using Discord;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMD_Utils.Music
{
internal static class Data
{
internal static IAudioClient audioClient;
internal static IVoiceChannel voiceChannel;
}
}

34
CMD_Utils/Music/Leave.cs Normal file
View File

@@ -0,0 +1,34 @@
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMD_Utils.Music
{
class Leave : DBCommand
{
public string Command => "leave";
public string Description => "Leave the voice channel";
public string Usage => "leave";
public bool canUseDM => false;
public bool canUseServer => true;
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
await Data.audioClient.StopAsync();
await Data.voiceChannel.DisconnectAsync();
}
}
}

33
CMD_Utils/Music/Pause.cs Normal file
View File

@@ -0,0 +1,33 @@
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMD_Utils.Music
{
class Pause : DBCommand
{
public string Command => "pause";
public string Description => "Pause the music";
public string Usage => "pause";
public bool canUseDM => false;
public bool canUseServer => true;
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
// to be implemented
}
}
}

77
CMD_Utils/Music/Play.cs Normal file
View File

@@ -0,0 +1,77 @@
using Discord;
using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.VisualBasic;
using PluginManager.Interfaces;
using PluginManager.Others;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace CMD_Utils.Music
{
class Play : DBCommand
{
public string Command => "fplay";
public string Description => "Play music from a file";
public string Usage => "fplay [name]";
public bool canUseDM => false;
public bool canUseServer => true;
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
string path = "./Music";
string FileName = Functions.GetArguments(message).ToArray().MergeStrings(0);
path += "/" + FileName + ".mp3";
if (!File.Exists(path))
{
Console.WriteLine("Unknown path " + path);
return;
}
// Get the audio channel
Data.voiceChannel = (context.User as IGuildUser)?.VoiceChannel;
if (Data.voiceChannel == null) { await context.Channel.SendMessageAsync("User must be in a voice channel, or a voice channel must be passed as an argument."); return; }
// For the next step with transmitting audio, you would want to pass this Audio Client in to a service.
Data.audioClient = await Data.voiceChannel.ConnectAsync();
// Create FFmpeg using the previous example
using (var ffmpeg = CreateStream(path))
using (var output = ffmpeg.StandardOutput.BaseStream)
using (var discord = Data.audioClient.CreatePCMStream(AudioApplication.Mixed))
{
try { await output.CopyToAsync(discord); }
finally { await discord.FlushAsync(); }
}
}
private Process CreateStream(string path)
{
return Process.Start(new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
UseShellExecute = false,
RedirectStandardOutput = true,
});
}
}
}