This commit is contained in:
2022-04-09 10:18:39 +03:00
parent 3d4c9ae9ea
commit 1e7387cfa7
11 changed files with 186 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -52,7 +52,7 @@ namespace PluginManager.Commands
DMCommands += cmd.Command + " ";
if (cmd.requireAdmin)
adminCommands += cmd.Command + " ";
else normalCommands += cmd.Command + " ";
else if (cmd.canUseServer) normalCommands += cmd.Command + " ";
}
embedBuilder.AddField("Admin Commands", adminCommands);

View File

@@ -1,11 +1,15 @@
using Discord;
using Discord.Audio;
using MusicCommands;
namespace CMD_Utils.Music
{
internal static class Data
{
internal static IAudioClient audioClient = null;
internal static IVoiceChannel voiceChannel = null;
internal static MusicPlayer CurrentlyRunning = null;
}
}

View File

@@ -29,6 +29,8 @@ namespace CMD_Utils.Music
{
if (Data.audioClient is not null && Data.voiceChannel is not null)
{
Data.CurrentlyRunning.Stop();
Data.CurrentlyRunning = null;
await Data.audioClient.StopAsync();
await Data.voiceChannel.DisconnectAsync();
}

View File

@@ -0,0 +1,26 @@
using Discord.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace MusicCommands
{
public class LinkMusic
{
private string URL;
public LinkMusic(string URL)
{
this.URL = URL;
}
public async Task<Stream> GetMusicStreamAsync()
{
WebClient client = new WebClient();
return await client.OpenReadTaskAsync(this.URL);
}
}
}

View File

@@ -0,0 +1,57 @@
using CMD_Utils.Music;
using PluginManager.Others;
using System;
using System.IO;
using System.Threading.Tasks;
namespace MusicCommands
{
class MusicPlayer
{
public Stream inputStream { get; private set; } // from FFMPEG
public Stream outputStream { get; private set; } // to Voice Channel
public MusicPlayer(Stream input, Stream output)
{
inputStream = input;
outputStream = output;
}
public bool Paused { get; set; }
private bool _stop { get; set; }
public void Stop()
{
_stop = true;
}
public async Task StartSendAudio()
{
Paused = false;
_stop = false;
while (!_stop)
{
if (Paused) continue;
int bsize = 512;
byte[] buffer = new byte[bsize];
var bcount = await inputStream.ReadAsync(buffer, 0, bsize);
if (bcount <= 0)
{
Stop();
Data.CurrentlyRunning = null;
break;
}
try
{
await outputStream.WriteAsync(buffer, 0, bcount);
}
catch (Exception ex)
{
await outputStream.FlushAsync();
Functions.WriteLogFile(ex.ToString());
}
}
}
}
}

View File

@@ -3,12 +3,6 @@ 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
@@ -25,9 +19,9 @@ namespace CMD_Utils.Music
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
// to be implemented
Data.CurrentlyRunning.Paused = true;
}
}
}

View File

@@ -3,6 +3,8 @@ using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using MusicCommands;
using PluginManager.Interfaces;
using PluginManager.Others;
@@ -37,22 +39,20 @@ namespace CMD_Utils.Music
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(); }
if (Data.CurrentlyRunning != null)
Data.CurrentlyRunning.Stop();
Data.CurrentlyRunning = new MusicPlayer(output, discord);
await Data.CurrentlyRunning.StartSendAudio();
}
}

35
MusicCommands/Unpause.cs Normal file
View File

@@ -0,0 +1,35 @@
using CMD_Utils.Music;
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 MusicCommands
{
class Unpause : DBCommand
{
public string Command => "unpause";
public string Description => "Unpause the music";
public string Usage => "unpause";
public bool canUseDM => false;
public bool canUseServer => true;
public bool requireAdmin => false;
public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
Data.CurrentlyRunning.Paused = false;
}
}
}

53
MusicCommands/lplay.cs Normal file
View File

@@ -0,0 +1,53 @@
using CMD_Utils.Music;
using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using Discord;
using PluginManager.Interfaces;
using PluginManager.Others;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MusicCommands
{
class lplay : DBCommand
{
public string Command => "lplay";
public string Description => "Play music from a link";
public string Usage => "lplay [name]";
public bool canUseDM => false;
public bool canUseServer => false;
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
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; }
Data.audioClient = await Data.voiceChannel.ConnectAsync();
using (var discord = Data.audioClient.CreatePCMStream(AudioApplication.Mixed))
{
if (Data.CurrentlyRunning != null)
Data.CurrentlyRunning.Stop();
LinkMusic music = new LinkMusic(Functions.GetArguments(message)[0]);
Data.CurrentlyRunning = new MusicPlayer(await music.GetMusicStreamAsync(), discord);
await Data.CurrentlyRunning.StartSendAudio();
}
}
}
}