From f4f927c7a59ea3c16df90800c0ce8a9c7e19ec65 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 30 Jan 2016 16:23:52 +0100 Subject: [PATCH] Using native videolib api. added .mute and .deafen --- NadekoBot/Classes/Music/StreamRequest.cs | 19 ++--- NadekoBot/Classes/Trivia.cs | 10 +-- NadekoBot/Modules/Administration.cs | 88 +++++++++++++++++++++++- NadekoBot/NadekoBot.csproj | 2 + 4 files changed, 104 insertions(+), 15 deletions(-) diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs index 109e9219..d0546ace 100644 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ b/NadekoBot/Classes/Music/StreamRequest.cs @@ -12,7 +12,7 @@ using System.Diagnostics; using NadekoBot.Extensions; using System.Threading; using Timer = System.Timers.Timer; -using YoutubeExtractor; +using VideoLibrary; namespace NadekoBot.Classes.Music { public enum StreamState { @@ -51,18 +51,20 @@ namespace NadekoBot.Classes.Music { this.VoiceClient = mc.VoiceClient; this.Server = e.Server; this.Query = query; - Task.Run(() => ResolveStreamLink()); + Task.Run(async () => await ResolveStreamLink()); } - private void ResolveStreamLink() { - VideoInfo video = null; + private async Task ResolveStreamLink() { + VideoLibrary.YouTubeVideo video = null; try { if (OnResolving != null) OnResolving(); Console.WriteLine("Resolving video link"); - video = DownloadUrlResolver.GetDownloadUrls(Searches.FindYoutubeUrlByKeywords(Query)) - .Where(v => v.AdaptiveType == AdaptiveType.Audio) - .OrderByDescending(v => v.AudioBitrate).FirstOrDefault(); + + video = (await YouTube.Default.GetAllVideosAsync(Searches.FindYoutubeUrlByKeywords(Query))) + .Where(v => v.AdaptiveKind == AdaptiveKind.Audio) + .OrderByDescending(v => v.AudioBitrate) + .FirstOrDefault(); if (video == null) // do something with this error throw new Exception("Could not load any video elements based on the query."); @@ -76,9 +78,10 @@ namespace NadekoBot.Classes.Music { return; } - musicStreamer = new MusicStreamer(this, video.DownloadUrl, Channel); + musicStreamer = new MusicStreamer(this, video.Uri, Channel); if (OnQueued != null) OnQueued(); + return; } internal string PrintStats() => musicStreamer?.Stats(); diff --git a/NadekoBot/Classes/Trivia.cs b/NadekoBot/Classes/Trivia.cs index 647bf444..056ce11d 100644 --- a/NadekoBot/Classes/Trivia.cs +++ b/NadekoBot/Classes/Trivia.cs @@ -11,16 +11,17 @@ using System.Threading.Tasks; using System.Timers; using NadekoBot.Extensions; using System.Collections; +using System.Collections.Concurrent; //github.com/micmorris contributed quite a bit to making trivia better! namespace NadekoBot { public class Trivia : DiscordCommand { public static float HINT_TIME_SECONDS = 6; - public static Dictionary runningTrivias; + public static ConcurrentDictionary runningTrivias; public Trivia() : base() { - runningTrivias = new Dictionary(); + runningTrivias = new ConcurrentDictionary(); } public static TriviaGame StartNewGame(CommandEventArgs e) { @@ -28,7 +29,7 @@ namespace NadekoBot { return null; var tg = new TriviaGame(e, NadekoBot.client); - runningTrivias.Add(e.Server.Id, tg); + runningTrivias.TryAdd(e.Server.Id, tg); return tg; } @@ -86,7 +87,8 @@ namespace NadekoBot { }; internal static void FinishGame(TriviaGame triviaGame) { - runningTrivias.Remove(runningTrivias.Where(kvp => kvp.Value == triviaGame).First().Key); + TriviaGame throwaway; + runningTrivias.TryRemove(runningTrivias.Where(kvp => kvp.Value == triviaGame).First().Key,out throwaway); } } diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 2caa1677..0462e925 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -184,6 +184,87 @@ namespace NadekoBot.Modules { await e.Send("No sufficient permissions."); } }); + cgb.CreateCommand(".mute") + .Description("Mutes mentioned user or users") + .Parameter("throwaway", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.MuteMembers) { + await e.Send("You do not have permission to do that."); + return; + } + if (e.Message.MentionedUsers.Count() == 0) + return; + try { + foreach (var u in e.Message.MentionedUsers) { + await u.Edit(isMuted: true); + } + await e.Send("Mute successful"); + } catch (Exception) { + await e.Send("I do not have permission to do that most likely."); + } + }); + + cgb.CreateCommand(".unmute") + .Description("Unmutes mentioned user or users") + .Parameter("throwaway", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.MuteMembers) { + await e.Send("You do not have permission to do that."); + return; + } + if (e.Message.MentionedUsers.Count() == 0) + return; + try { + foreach (var u in e.Message.MentionedUsers) { + await u.Edit(isMuted: false); + } + await e.Send("Unmute successful"); + } catch (Exception) { + await e.Send("I do not have permission to do that most likely."); + } + }); + + cgb.CreateCommand(".deafen") + .Alias(".deaf") + .Description("Deafens mentioned user or users") + .Parameter("throwaway", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.DeafenMembers) { + await e.Send("You do not have permission to do that."); + return; + } + if (e.Message.MentionedUsers.Count() == 0) + return; + try { + foreach (var u in e.Message.MentionedUsers) { + await u.Edit(isDeafened: true); + } + await e.Send("Deafen successful"); + } catch (Exception) { + await e.Send("I do not have permission to do that most likely."); + } + }); + + cgb.CreateCommand(".undeafen") + .Alias(".undeaf") + .Description("Undeafens mentioned user or users") + .Parameter("throwaway", ParameterType.Unparsed) + .Do(async e => { + if (!e.User.ServerPermissions.DeafenMembers) { + await e.Send("You do not have permission to do that."); + return; + } + if (e.Message.MentionedUsers.Count() == 0) + return; + try { + foreach (var u in e.Message.MentionedUsers) { + await u.Edit(isDeafened: false); + } + await e.Send("Undeafen successful"); + } catch (Exception) { + await e.Send("I do not have permission to do that most likely."); + } + }); cgb.CreateCommand(".rvch") .Description("Removes a voice channel with a given name.") @@ -302,7 +383,7 @@ namespace NadekoBot.Modules { try { var msgs = await e.Channel.DownloadMessages(100); var lastmessage = e.Channel.Messages.LastOrDefault(); - while (num > 0 && lastmessage!=null) { + while (num > 0 && lastmessage != null) { msgs.ForEach(async m => await m.Delete()); num -= 100; lastmessage = msgs.LastOrDefault(); @@ -404,8 +485,9 @@ namespace NadekoBot.Modules { await e.Send("Sending failed."); } }); - - /* + /*cgb.CreateCommand(".voicetext") + .Description("Enabled or disabled voice to text channel connection. Only people in a certain voice channel will see ") + cgb.CreateCommand(".jsontype") .Do(async e => { Newtonsoft.Json.Linq.JArray data = Newtonsoft.Json.Linq.JArray.Parse(File.ReadAllText("data.json")); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 6d07d706..95605139 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -124,10 +124,12 @@ + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll True + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll True