From 99d8e09ec9a063477c2dbf28cac77a10bd96fc41 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 25 Feb 2016 08:30:22 +0100 Subject: [PATCH] Last commit was sarcasm. Added local file support. closes #61 --- NadekoBot/Classes/Music/MusicControls.cs | 24 +++++++++++--- NadekoBot/Classes/Music/StreamRequest.cs | 15 ++++++--- NadekoBot/Modules/Music.cs | 41 ++++++++++++++++++++---- NadekoBot/NadekoBot.cs | 2 +- 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/NadekoBot/Classes/Music/MusicControls.cs b/NadekoBot/Classes/Music/MusicControls.cs index b9857243..3ece3323 100644 --- a/NadekoBot/Classes/Music/MusicControls.cs +++ b/NadekoBot/Classes/Music/MusicControls.cs @@ -7,6 +7,13 @@ using Discord.Commands; using MusicModule = NadekoBot.Modules.Music; namespace NadekoBot.Classes.Music { + + public enum MusicType { + Radio, + Normal, + Local + } + public class MusicControls { private CommandEventArgs _e; public bool NextSong { get; set; } = false; @@ -48,8 +55,10 @@ namespace NadekoBot.Classes.Music { } internal void AddSong(StreamRequest streamRequest) { - Stopped = false; - this.SongQueue.Add(streamRequest); + lock (_voiceLock) { + Stopped = false; + this.SongQueue.Add(streamRequest); + } } public MusicControls(Channel voiceChannel, CommandEventArgs e, float? vol) : this() { @@ -65,8 +74,10 @@ namespace NadekoBot.Classes.Music { CurrentSong?.Stop(); CurrentSong = null; if (SongQueue.Count != 0) { - CurrentSong = SongQueue[0]; - SongQueue.RemoveAt(0); + lock (_voiceLock) { + CurrentSong = SongQueue[0]; + SongQueue.RemoveAt(0); + } } else { Stop(); @@ -92,7 +103,10 @@ namespace NadekoBot.Classes.Music { internal void Stop(bool leave = false) { Stopped = true; SongQueue.Clear(); - CurrentSong?.Stop(); + try { + CurrentSong?.Stop(); + } + catch { } CurrentSong = null; if (leave) { VoiceClient?.Disconnect(); diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs index bbb0e503..9c90a41e 100644 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ b/NadekoBot/Classes/Music/StreamRequest.cs @@ -36,11 +36,11 @@ namespace NadekoBot.Classes.Music { public float Volume => MusicControls?.Volume ?? 1.0f; - public bool RadioLink { get; } + public MusicType LinkType { get; } public MusicControls MusicControls; - public StreamRequest(CommandEventArgs e, string query, MusicControls mc, bool radio = false) { + public StreamRequest(CommandEventArgs e, string query, MusicControls mc, MusicType musicType = MusicType.Normal) { if (e == null) throw new ArgumentNullException(nameof(e)); if (query == null) @@ -48,14 +48,19 @@ namespace NadekoBot.Classes.Music { this.MusicControls = mc; this.Server = e.Server; this.Query = query; - this.RadioLink = radio; + this.LinkType = musicType; mc.AddSong(this); } public async Task Resolve() { string uri = null; try { - if (RadioLink) { + if (this.LinkType == MusicType.Local) { + uri = "\"" + Path.GetFullPath(Query) + "\""; + Title = Path.GetFileNameWithoutExtension(Query); + Provider = "Local File"; + } + else if (this.LinkType == MusicType.Radio) { uri = Query; Title = $"{Query}"; Provider = "Radio Stream"; @@ -259,7 +264,7 @@ namespace NadekoBot.Classes.Music { // prebuffering wait stuff start int bufferAttempts = 0; int waitPerAttempt = 500; - int toAttemptTimes = parent.RadioLink ? 4 : 8; + int toAttemptTimes = parent.LinkType != MusicType.Normal ? 4 : 8; while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes) { await Task.Delay(waitPerAttempt); } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 76c0ecff..5773dc03 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -203,6 +203,23 @@ namespace NadekoBot.Modules { msg?.Edit("šŸŽµ `Playlist queue complete.`"); }); + cgb.CreateCommand("lopl") + .Description("Queues up to 50 songs from a directory.") + .Parameter("directory", ParameterType.Unparsed) + .Do(async e => { + var arg = e.GetArg("directory"); + if(string.IsNullOrWhiteSpace(e.GetArg("directory"))) + return; + try { + var fileEnum = System.IO.Directory.EnumerateFiles(e.GetArg("directory")).Take(50); + foreach (var file in fileEnum) { + await Task.Run(async() => await QueueSong(e, file, true, MusicType.Local)).ConfigureAwait(false); + } + await e.Channel.SendMessage("šŸŽµ `Directory queue complete.`"); + } + catch { } + }); + cgb.CreateCommand("radio").Alias("ra") .Description("Queues a direct radio stream from a link.") .Parameter("radio_link", ParameterType.Required) @@ -211,9 +228,19 @@ namespace NadekoBot.Modules { await e.Channel.SendMessage("šŸ’¢ You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining it."); return; } - await QueueSong(e, e.GetArg("radio_link"), radio: true); + await QueueSong(e, e.GetArg("radio_link"), musicType: MusicType.Radio); }); + cgb.CreateCommand("lo") + .Description("Queues a local file by specifying a full path. BOT OWNER ONLY.") + .Parameter("path", ParameterType.Unparsed) + .Do(async e => { + var arg = e.GetArg("path"); + if (string.IsNullOrWhiteSpace(arg)) + return; + await QueueSong(e, e.GetArg("path"), musicType: MusicType.Local); + }); + cgb.CreateCommand("mv") .Description("Moves the bot to your voice channel. (works only if music is already playing)") .Do(async e => { @@ -261,9 +288,10 @@ namespace NadekoBot.Modules { }); } - private async Task QueueSong(CommandEventArgs e, string query, bool silent = false, bool radio = false) { + private async Task QueueSong(CommandEventArgs e, string query, bool silent = false, MusicType musicType = MusicType.Normal) { if (e.User.VoiceChannel?.Server != e.Server) { - await e.Channel.SendMessage("šŸ’¢ You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining."); + if(!silent) + await e.Channel.SendMessage("šŸ’¢ You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining."); return; } @@ -271,9 +299,8 @@ namespace NadekoBot.Modules { return; query = query.Trim(); - - if (IsRadioLink(query)) { - radio = true; + if (musicType != MusicType.Local && IsRadioLink(query)) { + musicType = MusicType.Radio; query = await HandleStreamContainers(query) ?? query; } @@ -294,7 +321,7 @@ namespace NadekoBot.Modules { if (player.SongQueue.Count >= 50) return; try { - var sr = new StreamRequest(e, query, player, radio); + var sr = new StreamRequest(e, query, player, musicType); if (sr == null) throw new NullReferenceException("StreamRequest is null."); diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 88ee5e8f..e080b8dc 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -180,7 +180,7 @@ namespace NadekoBot { if (!repliedRecently) { repliedRecently = true; - await e.Channel.SendMessage("**FULL LIST OF COMMANDS**:\nā¤ ā¤\n\nāš **COMMANDS DO NOT WORK IN PERSONAL MESSAGES**\n\n\n**Bot Creator's server:** "); + await e.Channel.SendMessage("**FULL LIST OF COMMANDS**:\nā¤ ā¤\n\nāš **COMMANDS DO NOT WORK IN PERSONAL MESSAGES**\n\n\n**Bot Creator's server:** "); Timer t = new Timer(); t.Interval = 2000; t.Start();