multiserver music almost done

This commit is contained in:
Master Kwoth 2016-01-22 06:35:44 +01:00
parent 7cc1a50de9
commit 0b8caf568c
3 changed files with 89 additions and 41 deletions

View File

@ -7,10 +7,10 @@
public string BotMention; public string BotMention;
public string GoogleAPIKey; public string GoogleAPIKey;
public ulong OwnerID; public ulong OwnerID;
public bool Crawl;
public string ParseID; public string ParseID;
public string ParseKey; public string ParseKey;
public string TrelloAppKey; public string TrelloAppKey;
public bool? ForwardMessages;
} }
public class AnimeResult public class AnimeResult
{ {

View File

@ -15,24 +15,23 @@ using System.Diagnostics;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Net; using System.Net;
using System.Globalization; using System.Globalization;
using System.Collections.Concurrent;
namespace NadekoBot.Modules { namespace NadekoBot.Modules {
class Music : DiscordModule { class Music : DiscordModule {
private static bool exit = true;
public static bool NextSong = false; public class MusicControls {
public static IAudioClient Voice; public bool NextSong = false;
public static Channel VoiceChannel; public IAudioClient Voice;
public static bool Pause = false; public Channel VoiceChannel;
public static List<StreamRequest> SongQueue = new List<StreamRequest>(); public bool Pause = false;
public List<StreamRequest> SongQueue = new List<StreamRequest>();
public static StreamRequest CurrentSong; public StreamRequest CurrentSong;
public static bool Exit {
get { return exit; }
set { exit = value; } // if i set this to true, break the song and exit the main loop
} }
public static ConcurrentDictionary<Server, MusicControls> musicPlayers = new ConcurrentDictionary<Server,MusicControls>();
public Music() : base() { public Music() : base() {
//commands.Add(new PlayMusic()); //commands.Add(new PlayMusic());
} }
@ -49,11 +48,19 @@ namespace NadekoBot.Modules {
var client = NadekoBot.client; var client = NadekoBot.client;
Task.Run(async () => { Task.Run(async () => {
while (true) { while (true) {
if (CurrentSong == null || CurrentSong.State == StreamTaskState.Completed) { try {
await LoadNextSong(); foreach (var kvp in musicPlayers) {
} else var player = kvp.Value;
await Task.Delay(200); if (player.CurrentSong == null || player.CurrentSong.State == StreamTaskState.Completed) {
LoadNextSong(player);
await Task.Delay(200);
}
}
} catch (Exception e) {
Console.WriteLine(e);
}
} }
}); });
@ -65,9 +72,10 @@ namespace NadekoBot.Modules {
.Alias("next") .Alias("next")
.Description("Goes to the next song in the queue.") .Description("Goes to the next song in the queue.")
.Do(e => { .Do(e => {
if (CurrentSong == null) return; if (musicPlayers.ContainsKey(e.Server) == false || (musicPlayers[e.Server]?.CurrentSong) == null) return;
var CurrentSong = musicPlayers[e.Server].CurrentSong;
CurrentSong.Cancel(); CurrentSong.Cancel();
CurrentSong = SongQueue.Take(1).FirstOrDefault(); CurrentSong = musicPlayers[e.Server].SongQueue.Take(1).FirstOrDefault();
if (CurrentSong != null) { if (CurrentSong != null) {
CurrentSong.Start(); CurrentSong.Start();
} }
@ -77,60 +85,79 @@ namespace NadekoBot.Modules {
.Alias("stop") .Alias("stop")
.Description("Completely stops the music and unbinds the bot from the channel and cleanes up files.") .Description("Completely stops the music and unbinds the bot from the channel and cleanes up files.")
.Do(e => { .Do(e => {
SongQueue.Clear(); if (musicPlayers.ContainsKey(e.Server) == false) return;
if (CurrentSong != null) { var player = musicPlayers[e.Server];
CurrentSong.Cancel(); player.SongQueue.Clear();
CurrentSong = null; if (player.CurrentSong != null) {
player.CurrentSong.Cancel();
player.CurrentSong = null;
} }
Directory.Delete("StreamBuffers", true);
}); });
cgb.CreateCommand("p") cgb.CreateCommand("p")
.Alias("pause") .Alias("pause")
.Description("Pauses the song") .Description("Pauses the song")
.Do(async e => { .Do(async e => {
if (musicPlayers.ContainsKey(e.Server) == false) return;
await e.Send("Not yet implemented."); await e.Send("Not yet implemented.");
}); });
cgb.CreateCommand("q") cgb.CreateCommand("q")
.Alias("yq") .Alias("yq")
.Description("Queue a song using a multi/single word name.\n**Usage**: `!m q Dream Of Venice`") .Description("Queue a song using keywords or link. **You must be in a voice channel**.\n**Usage**: `!m q Dream Of Venice`")
.Parameter("Query", ParameterType.Unparsed) .Parameter("Query", ParameterType.Unparsed)
.Do(e => { .Do(e => {
SongQueue.Add(new StreamRequest(NadekoBot.client, e, e.GetArg("Query"))); if (musicPlayers.ContainsKey(e.Server) == false)
musicPlayers.TryAdd(e.Server, new MusicControls());
var player = musicPlayers[e.Server];
player.SongQueue.Add(new StreamRequest(NadekoBot.client, e, e.GetArg("Query")));
}); });
cgb.CreateCommand("lq") cgb.CreateCommand("lq")
.Alias("ls").Alias("lp") .Alias("ls").Alias("lp")
.Description("Lists up to 10 currently queued songs.") .Description("Lists up to 10 currently queued songs.")
.Do(async e => { .Do(async e => {
await e.Send(":musical_note: " + SongQueue.Count + " videos currently queued."); if (musicPlayers.ContainsKey(e.Server) == false) await e.Send(":musical_note: No active music player.");
await e.Send(string.Join("\n", SongQueue.Select(v => v.Title).Take(10))); var player = musicPlayers[e.Server];
await e.Send(":musical_note: " + player.SongQueue.Count + " videos currently queued.");
await e.Send(string.Join("\n", player.SongQueue.Select(v => v.Title).Take(10)));
});
cgb.CreateCommand("clrbfr")
.Alias("clearbuffers")
.Description("Clears the music buffer across all servers. **Owner only.**")
.Do(e => {
if (NadekoBot.OwnerID != e.User.Id) return;
Directory.Delete("StreamBuffers", true);
}); });
cgb.CreateCommand("sh") cgb.CreateCommand("sh")
.Description("Shuffles the current playlist.") .Description("Shuffles the current playlist.")
.Do(async e => { .Do(async e => {
if (SongQueue.Count < 2) { if (musicPlayers.ContainsKey(e.Server) == false) return;
var player = musicPlayers[e.Server];
if (player.SongQueue.Count < 2) {
await e.Send("Not enough songs in order to perform the shuffle."); await e.Send("Not enough songs in order to perform the shuffle.");
return; return;
} }
SongQueue.Shuffle(); player.SongQueue.Shuffle();
await e.Send(":musical_note: Songs shuffled!"); await e.Send(":musical_note: Songs shuffled!");
}); });
}); });
} }
private async Task LoadNextSong() { private void LoadNextSong(MusicControls player) {
if (SongQueue.Count == 0 || !SongQueue[0].LinkResolved) { if (player.SongQueue.Count == 0 || !player.SongQueue[0].LinkResolved) {
if (CurrentSong != null) if (player.CurrentSong != null)
CurrentSong.Cancel(); player.CurrentSong.Cancel();
CurrentSong = null; player.CurrentSong = null;
await Task.Delay(200);
return; return;
} }
CurrentSong = SongQueue[0]; player.CurrentSong = player.SongQueue[0];
SongQueue.RemoveAt(0); player.SongQueue.RemoveAt(0);
CurrentSong.Start(); player.CurrentSong.Start();
return; return;
} }
} }

View File

@ -20,8 +20,10 @@ namespace NadekoBot
public static string botMention; public static string botMention;
public static string GoogleAPIKey = null; public static string GoogleAPIKey = null;
public static ulong OwnerID; public static ulong OwnerID;
public static User OwnerUser = null;
public static string password; public static string password;
public static string TrelloAppKey; public static string TrelloAppKey;
public static bool ForwardMessages = false;
static void Main() static void Main()
{ {
@ -45,6 +47,13 @@ namespace NadekoBot
TrelloAppKey = c.TrelloAppKey; TrelloAppKey = c.TrelloAppKey;
trelloLoaded = true; trelloLoaded = true;
} }
if (c.ForwardMessages != true)
Console.WriteLine("Not forwarding messages.");
else {
ForwardMessages = true;
Console.WriteLine("Forwarding messages.");
}
OwnerID = c.OwnerID; OwnerID = c.OwnerID;
password = c.Password; password = c.Password;
} }
@ -75,7 +84,7 @@ namespace NadekoBot
Console.WriteLine("Parse key and/or ID not found. Logging disabled."); Console.WriteLine("Parse key and/or ID not found. Logging disabled.");
} }
//reply to personal messages //reply to personal messages and forward if enabled.
client.MessageReceived += Client_MessageReceived; client.MessageReceived += Client_MessageReceived;
//add command service //add command service
@ -87,7 +96,9 @@ namespace NadekoBot
//add audio service //add audio service
var audio = client.Services.Add<AudioService>(new AudioService(new AudioServiceConfig() { var audio = client.Services.Add<AudioService>(new AudioService(new AudioServiceConfig() {
Channels = 2, Channels = 2,
EnableEncryption = false EnableEncryption = false,
EnableMultiserver = true,
Mode = AudioMode.Outgoing
})); }));
//install modules //install modules
@ -116,6 +127,12 @@ namespace NadekoBot
Console.WriteLine("Heap: "+ Math.Round(GC.GetTotalMemory(true) / (1024.0 * 1024.0), 2).ToString() + "MB"); Console.WriteLine("Heap: "+ Math.Round(GC.GetTotalMemory(true) / (1024.0 * 1024.0), 2).ToString() + "MB");
Console.WriteLine("-------------------------"); Console.WriteLine("-------------------------");
foreach (var serv in client.Servers) {
if ((OwnerUser = serv.GetUser(OwnerID)) != null)
return;
}
}); });
Console.WriteLine("Exiting..."); Console.WriteLine("Exiting...");
Console.ReadKey(); Console.ReadKey();
@ -126,8 +143,12 @@ namespace NadekoBot
if (e.Server != null) return; if (e.Server != null) return;
try { try {
(await client.GetInvite(e.Message.Text))?.Accept(); (await client.GetInvite(e.Message.Text))?.Accept();
await e.User.Send("I got in, thanks. <3");
} catch (Exception) { } } catch (Exception) { }
if (NadekoBot.ForwardMessages && OwnerUser != null)
await OwnerUser.Send(e.Message.Text);
if (repliedRecently = !repliedRecently) { if (repliedRecently = !repliedRecently) {
await e.Send("You can type `-h` or `-help` or `@MyName help` in any of the channels I am in and I will send you a message with my commands.\n Or you can find out what i do here: https://github.com/Kwoth/NadekoBot\nYou can also just send me an invite link to a server and I will join it.\nIf you don't want me on your server, you can simply ban me ;("); await e.Send("You can type `-h` or `-help` or `@MyName help` in any of the channels I am in and I will send you a message with my commands.\n Or you can find out what i do here: https://github.com/Kwoth/NadekoBot\nYou can also just send me an invite link to a server and I will join it.\nIf you don't want me on your server, you can simply ban me ;(");
Timer t = new Timer(); Timer t = new Timer();