From 00f09b66bde1e1535d9076ae93cb24b6a559f167 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 30 Jan 2016 05:24:32 +0100 Subject: [PATCH] Fixed memory leak, trivia hints are after 6 sec, etc --- NadekoBot/Classes/Extensions.cs | 20 +-- NadekoBot/Classes/Music/MusicControls.cs | 31 +++-- NadekoBot/Classes/Music/StreamRequest.cs | 55 ++++---- NadekoBot/Classes/Trivia.cs | 155 +++++++++-------------- NadekoBot/Commands/HelpCommand.cs | 2 +- NadekoBot/Modules/Administration.cs | 72 +++-------- NadekoBot/Modules/Conversations.cs | 29 +++-- NadekoBot/Modules/Music.cs | 4 +- NadekoBot/NadekoBot.cs | 4 +- NadekoBot/NadekoBot.csproj | 12 +- NadekoBot/packages.config | 3 +- 11 files changed, 178 insertions(+), 209 deletions(-) diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index e9e41e2b..29b48ab3 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -5,27 +5,31 @@ using System.Threading.Tasks; using System.Security.Cryptography; using Discord.Commands; using Discord; -using Discord.Legacy; using NadekoBot.Modules; using System.IO; using System.Drawing; -namespace NadekoBot.Extensions -{ +namespace NadekoBot.Extensions { public static class Extensions { public static string Scramble(this string word) { var letters = word.ToArray(); - for (int i = 0; i < letters.Length; i++) - { - if (i % 3 == 0) - { + int count = 0; + for (int i = 0; i < letters.Length; i++) { + if (letters[i] == ' ') + continue; + + count++; + if (count <= letters.Length / 5) + continue; + + if (count % 3 == 0) continue; - } if (letters[i] != ' ') letters[i] = '_'; + } return "`"+string.Join(" ", letters)+"`"; } diff --git a/NadekoBot/Classes/Music/MusicControls.cs b/NadekoBot/Classes/Music/MusicControls.cs index 155da1fb..0bb46677 100644 --- a/NadekoBot/Classes/Music/MusicControls.cs +++ b/NadekoBot/Classes/Music/MusicControls.cs @@ -15,13 +15,15 @@ namespace NadekoBot.Classes.Music { public StreamRequest CurrentSong; public bool IsPaused { get; internal set; } = false; + public bool Stopped { get; private set; } + public IAudioClient VoiceClient; private readonly object _voiceLock = new object(); public MusicControls() { Task.Run(async () => { - while (true) { + while (!Stopped) { try { lock (_voiceLock) { if (CurrentSong == null) { @@ -36,7 +38,7 @@ namespace NadekoBot.Classes.Music { } catch (Exception e) { Console.WriteLine("Bug in music task run. " + e); } - await Task.Delay(200); + await Task.Delay(500); } }); } @@ -52,7 +54,11 @@ namespace NadekoBot.Classes.Music { if (SongQueue.Count != 0) { CurrentSong = SongQueue[0]; SongQueue.RemoveAt(0); - } else return; + } else { + VoiceClient?.Disconnect(); + VoiceClient = null; + return; + } } try { @@ -60,32 +66,35 @@ namespace NadekoBot.Classes.Music { } catch (Exception ex) { Console.WriteLine($"Starting failed: {ex}"); CurrentSong?.Stop(); - CurrentSong = null; } } internal void Stop() { lock (_voiceLock) { + Stopped = true; foreach (var kvp in SongQueue) { if(kvp != null) kvp.Cancel(); } - SongQueue?.Clear(); - LoadNextSong(); + SongQueue.Clear(); + CurrentSong?.Stop(); + CurrentSong = null; VoiceClient?.Disconnect(); VoiceClient = null; } } - internal StreamRequest CreateStreamRequest(CommandEventArgs e, string query, Channel voiceChannel) { + internal async Task CreateStreamRequest(CommandEventArgs e, string query, Channel voiceChannel) { if (VoiceChannel == null) throw new ArgumentNullException("Please join a voicechannel."); StreamRequest sr = null; + if (VoiceClient == null) { + VoiceChannel = voiceChannel; + VoiceClient = await NadekoBot.client.Audio().Join(VoiceChannel); + } + sr = new StreamRequest(e, query, this); + lock (_voiceLock) { - if (VoiceClient == null) { - VoiceClient = NadekoBot.client.Audio().Join(VoiceChannel).Result; - } - sr = new StreamRequest(e, query, this); SongQueue.Add(sr); } return sr; diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs index 4facc250..c3bb8a56 100644 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ b/NadekoBot/Classes/Music/StreamRequest.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.Audio; -using YoutubeExtractor; using NadekoBot.Modules; using System.IO; using System.Diagnostics; using NadekoBot.Extensions; using System.Threading; using Timer = System.Timers.Timer; +using YoutubeExtractor; namespace NadekoBot.Classes.Music { public enum StreamState { @@ -94,12 +94,12 @@ namespace NadekoBot.Classes.Music { musicStreamer?.Stop(); } - internal Task Start() => - Task.Run(async () => { - Console.WriteLine("Start called."); + internal async Task Start() { + Console.WriteLine("Start called."); - int attemptsLeft = 4; - //wait for up to 4 seconds to resolve a link + int attemptsLeft = 4; + //wait for up to 4 seconds to resolve a link + try { while (State == StreamState.Resolving) { await Task.Delay(1000); Console.WriteLine("Resolving..."); @@ -107,13 +107,15 @@ namespace NadekoBot.Classes.Music { throw new TimeoutException("Resolving timed out."); } } - try { - await musicStreamer.StartPlayback(); - } catch (Exception ex) { - Console.WriteLine("Error in start playback." + ex.Message); - privateState = StreamState.Completed; - } - }); + await musicStreamer.StartPlayback(); + } catch (TimeoutException) { + Console.WriteLine("Resolving timed out."); + privateState = StreamState.Completed; + } catch (Exception ex) { + Console.WriteLine("Error in start playback." + ex.Message); + privateState = StreamState.Completed; + } + } } public class MusicStreamer { @@ -127,7 +129,7 @@ namespace NadekoBot.Classes.Music { StreamRequest parent; private readonly object _bufferLock = new object(); - private CancellationTokenSource bufferCancelSource; + private bool prebufferingComplete = false; public MusicStreamer(StreamRequest parent, string directUrl, Channel channel) { this.parent = parent; @@ -136,7 +138,6 @@ namespace NadekoBot.Classes.Music { this.Url = directUrl; Console.WriteLine("Created new streamer"); State = StreamState.Queued; - bufferCancelSource = new CancellationTokenSource(); } public string Stats() => @@ -162,12 +163,9 @@ namespace NadekoBot.Classes.Music { int attempt = 0; while (true) { + //wait for the read pos to catch up with write pos while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) { - if (!bufferCancelSource.IsCancellationRequested) { - Console.WriteLine("Canceling buffer token"); - Task.Run(() => bufferCancelSource.Cancel()); - } - + prebufferingComplete = true; await Task.Delay(500); } @@ -228,11 +226,18 @@ namespace NadekoBot.Classes.Music { if (parent.OnBuffering != null) parent.OnBuffering(); BufferSong(); - try { - await Task.Delay(5000, bufferCancelSource.Token); - } catch (Exception) { - Console.WriteLine("Buffered enough in less than 5 seconds!"); + + // prebuffering wait stuff start + int bufferAttempts = 0; + int waitPerAttempt = 500; + while (!prebufferingComplete && bufferAttempts++ < 10) { + await Task.Delay(waitPerAttempt); } + if (prebufferingComplete) { + Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}"); + } + // prebuffering wait stuff end + //Task.Run(async () => { while (true) { Console.WriteLine($"Title: {parent.Title} State:{State}"); await Task.Delay(200); } }); if (parent.OnStarted != null) parent.OnStarted(); @@ -265,8 +270,6 @@ namespace NadekoBot.Classes.Music { } else attempt = 0; - - if (State == StreamState.Completed) { Console.WriteLine("Canceled"); break; diff --git a/NadekoBot/Classes/Trivia.cs b/NadekoBot/Classes/Trivia.cs index f14a702c..647bf444 100644 --- a/NadekoBot/Classes/Trivia.cs +++ b/NadekoBot/Classes/Trivia.cs @@ -12,10 +12,11 @@ using System.Timers; using NadekoBot.Extensions; using System.Collections; -namespace NadekoBot -{ - public class Trivia : DiscordCommand - { +//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 Trivia() : base() { @@ -28,47 +29,37 @@ namespace NadekoBot var tg = new TriviaGame(e, NadekoBot.client); runningTrivias.Add(e.Server.Id, tg); - + return tg; } public TriviaQuestion GetCurrentQuestion(ulong serverId) => runningTrivias[serverId].currentQuestion; - public override Func DoFunc() => async e => - { + public override Func DoFunc() => async e => { TriviaGame tg; - if ((tg = StartNewGame(e)) != null) - { - await e.Send("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command [tq] if game was started by accident.\nTyping [idfk] 15 seconds after the question has started will give you a hint."); - } - else + if ((tg = StartNewGame(e)) != null) { + await e.Send("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command `tq` if game was started by accident.**"); + } else await e.Send("Trivia game is already running on this server. The question is:\n**" + GetCurrentQuestion(e.Server.Id).Question + "**"); }; - private Func LbFunc() => async e => - { - if (runningTrivias.ContainsKey(e.Server.Id)) - { + private Func LbFunc() => async e => { + if (runningTrivias.ContainsKey(e.Server.Id)) { var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); await e.Send(lb); - } - else + } else await e.Send("Trivia game is not running on this server."); }; - private Func RepeatFunc() => async e => - { - if (runningTrivias.ContainsKey(e.Server.Id)) - { + private Func RepeatFunc() => async e => { + if (runningTrivias.ContainsKey(e.Server.Id)) { var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); await e.Send(lb); - } - else + } else await e.Send("Trivia game is not running on this server."); }; - public override void Init(CommandGroupBuilder cgb) - { + public override void Init(CommandGroupBuilder cgb) { cgb.CreateCommand("t") .Description("Starts a game of trivia.") .Alias("-t") @@ -87,18 +78,14 @@ namespace NadekoBot .Do(QuitFunc()); } - private Func QuitFunc() => async e => - { - if (runningTrivias.ContainsKey(e.Server.Id) && runningTrivias[e.Server.Id].ChannelId == e.Channel.Id) - { - await e.Send("Trivia will stop after this question. Run [**@NadekoBot clr**] to remove this bot's messages from the channel."); + private Func QuitFunc() => async e => { + if (runningTrivias.ContainsKey(e.Server.Id) && runningTrivias[e.Server.Id].ChannelId == e.Channel.Id) { + await e.Send("Trivia will stop after this question."); runningTrivias[e.Server.Id].StopGame(); - } - else await e.Send("No trivias are running on this channel."); + } else await e.Send("No trivias are running on this channel."); }; - internal static void FinishGame(TriviaGame triviaGame) - { + internal static void FinishGame(TriviaGame triviaGame) { runningTrivias.Remove(runningTrivias.Where(kvp => kvp.Value == triviaGame).First().Key); } } @@ -127,11 +114,13 @@ namespace NadekoBot private Stopwatch stopwatch; private bool isQuit = false; + private System.Threading.CancellationTokenSource hintCancelSource; + public TriviaGame(CommandEventArgs starter, DiscordClient client) { this.users = new Dictionary(); this.client = client; this._serverId = starter.Server.Id; - this._channellId= starter.Channel.Id; + this._channellId = starter.Channel.Id; oldQuestions = new List(); client.MessageReceived += PotentialGuess; @@ -149,12 +138,12 @@ namespace NadekoBot stopwatch = new Stopwatch(); timeout.Elapsed += (s, e) => { TimeUp(); }; + hintCancelSource = new System.Threading.CancellationTokenSource(); TriviaQuestionsPool.Instance.Reload(); LoadNextRound(); } - private async void PotentialGuess(object sender, MessageEventArgs e) - { + private async void PotentialGuess(object sender, MessageEventArgs e) { if (e.Server == null || e.Channel == null) return; if (e.Server.Id != _serverId || !active) return; @@ -163,26 +152,24 @@ namespace NadekoBot return; if (e.Message.Text.ToLower().Equals("idfk")) { - GetHint(e); + GetHint(e.Channel); return; } - if (IsAnswerCorrect(e.Message.Text.ToLower(), currentQuestion.Answer.ToLower())) - { + if (IsAnswerCorrect(e.Message.Text.ToLower(), currentQuestion.Answer.ToLower())) { active = false; //start pause between rounds timeout.Enabled = false; stopwatch.Stop(); if (!users.ContainsKey(e.User.Id)) users.Add(e.User.Id, 1); - else - { + else { users[e.User.Id]++; } - await e.Send( e.User.Mention + " Guessed it!\n The answer was: **" + currentQuestion.Answer + "**"); + await e.Send(e.User.Mention + " Guessed it!\n The answer was: **" + currentQuestion.Answer + "**"); if (users[e.User.Id] >= 10) { - await e.Send( " We have a winner! It's " + e.User.Mention+"\n"+GetLeaderboard()+"\n To start a new game type '@NadekoBot t'"); + await e.Send(" We have a winner! It's " + e.User.Mention + "\n" + GetLeaderboard() + "\n To start a new game type '@NadekoBot t'"); FinishGame(); return; } @@ -192,16 +179,13 @@ namespace NadekoBot } } - private bool IsAnswerCorrect(string guess, string answer) - { - if(guess.Equals(answer)) - { + private bool IsAnswerCorrect(string guess, string answer) { + if (guess.Equals(answer)) { return true; } guess = CleanString(guess); answer = CleanString(answer); - if (guess.Equals(answer)) - { + if (guess.Equals(answer)) { return true; } @@ -209,12 +193,9 @@ namespace NadekoBot return Judge(guess.Length, answer.Length, levDistance); } - private bool Judge(int guessLength, int answerLength, int levDistance) - { - foreach(Tuple level in strictness) - { - if(guessLength <= level.Item1 || answerLength <= level.Item1) - { + private bool Judge(int guessLength, int answerLength, int levDistance) { + foreach (Tuple level in strictness) { + if (guessLength <= level.Item1 || answerLength <= level.Item1) { if (levDistance <= level.Item2) return true; else @@ -224,8 +205,7 @@ namespace NadekoBot return false; } - private string CleanString(string str) - { + private string CleanString(string str) { str = " " + str + " "; str = Regex.Replace(str, "\\s+", " "); str = Regex.Replace(str, "[^\\w\\d\\s]", ""); @@ -239,30 +219,24 @@ namespace NadekoBot return str; } - public async void GetHint(MessageEventArgs e) { - if (timeout != null && !isQuit && stopwatch.ElapsedMilliseconds > 10000) - await e.Send( currentQuestion.Answer.Scramble()); - else { - await e.Send( $"You have to wait {10-stopwatch.ElapsedMilliseconds/1000} more seconds in order to get a hint."); - } + public async void GetHint(Channel ch) { + await ch.Send(currentQuestion.Answer.Scramble()); } public void StopGame() { isQuit = true; } - private void LoadNextRound() - { + private void LoadNextRound() { Channel ch = client.GetChannel(_channellId); - - if(currentQuestion!=null) + + if (currentQuestion != null) oldQuestions.Add(currentQuestion.Question); currentQuestion = TriviaQuestionsPool.Instance.GetRandomQuestion(oldQuestions); - if (currentQuestion == null || isQuit) - { + if (currentQuestion == null || isQuit) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed ch.Send("Trivia bot stopping. :\\\n" + GetLeaderboard()); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed @@ -279,12 +253,16 @@ namespace NadekoBot timeout.Enabled = true;//starting countdown of the next question stopwatch.Reset(); stopwatch.Start(); + try { + await Task.Delay((int)Trivia.HINT_TIME_SECONDS * 1000); + GetHint(ch); + } catch (Exception) { } }; return; } private async void TimeUp() { - await client.GetChannel(_channellId)?.Send("**Time's up.**\nCorrect answer was: **" + currentQuestion.Answer+"**\n\n*[tq quits trivia][tl shows leaderboard]["+NadekoBot.botMention+" clr clears my messages]*"); + await client.GetChannel(_channellId)?.Send("**Time's up.**\nCorrect answer was: **" + currentQuestion.Answer + "**\n\n*[tq quits trivia][tl shows leaderboard][" + NadekoBot.botMention + " clr clears my messages]*"); LoadNextRound(); } @@ -302,29 +280,26 @@ namespace NadekoBot public string GetLeaderboard() { if (users.Count == 0) return ""; - + string str = "**Leaderboard:**\n-----------\n"; - if(users.Count>1) + if (users.Count > 1) users.OrderBy(kvp => kvp.Value); - - foreach (var KeyValuePair in users) - { - str += "**" + client.GetServer(_serverId).GetUser(KeyValuePair.Key).Name + "** has " +KeyValuePair.Value + (KeyValuePair.Value == 1 ? "point." : "points.") + Environment.NewLine; + + foreach (var KeyValuePair in users) { + str += "**" + client.GetServer(_serverId).GetUser(KeyValuePair.Key).Name + "** has " + KeyValuePair.Value + (KeyValuePair.Value == 1 ? "point." : "points.") + Environment.NewLine; } - + return str; } } - public class TriviaQuestion - { + public class TriviaQuestion { public string Category; public string Question; public string Answer; - public TriviaQuestion(string q, string a) - { + public TriviaQuestion(string q, string a) { this.Question = q; this.Answer = a; } @@ -338,8 +313,7 @@ namespace NadekoBot public class TriviaQuestionsPool { private static TriviaQuestionsPool instance = null; - public static TriviaQuestionsPool Instance - { + public static TriviaQuestionsPool Instance { get { if (instance == null) instance = new TriviaQuestionsPool(); @@ -352,21 +326,18 @@ namespace NadekoBot private Random _r; - public TriviaQuestionsPool() - { + public TriviaQuestionsPool() { Reload(); } - + public TriviaQuestion GetRandomQuestion(List exclude) { if (pool.Count == 0) return null; TriviaQuestion tq = pool[_r.Next(0, pool.Count)]; - if (exclude.Count > 0 && exclude.Count < pool.Count) - { - while (exclude.Contains(tq.Question)) - { + if (exclude.Count > 0 && exclude.Count < pool.Count) { + while (exclude.Contains(tq.Question)) { tq = pool[_r.Next(0, pool.Count)]; } } diff --git a/NadekoBot/Commands/HelpCommand.cs b/NadekoBot/Commands/HelpCommand.cs index 65836170..f21a736c 100644 --- a/NadekoBot/Commands/HelpCommand.cs +++ b/NadekoBot/Commands/HelpCommand.cs @@ -10,7 +10,7 @@ namespace NadekoBot { public override Func DoFunc() => async e => { - string helpstr = "Official repo: **github.com/Kwoth/NadekoBot/**"; + string helpstr = "**COMMANDS DO NOT WORK IN PERSONAL MESSAGES**\nOfficial repo: **github.com/Kwoth/NadekoBot/**"; string lastCategory = ""; foreach (var com in client.Commands().AllCommands) diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index d7f4a23a..462d1a05 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -10,17 +10,14 @@ using System.Threading.Tasks; using NadekoBot.Commands; using System.IO; -namespace NadekoBot.Modules -{ - class Administration : DiscordModule - { +namespace NadekoBot.Modules { + class Administration : DiscordModule { public Administration() : base() { commands.Add(new HelpCommand()); commands.Add(new ServerGreetCommand()); } - public override void Install(ModuleManager manager) - { + public override void Install(ModuleManager manager) { manager.CreateCommands("", cgb => { var client = manager.Client; @@ -133,22 +130,22 @@ namespace NadekoBot.Modules try { bool rgb = args.Count() == 4; - byte red = Convert.ToByte(rgb ? int.Parse(e.Args[1]) : Convert.ToInt32(e.Args[1].Substring(0,2), 16)); + byte red = Convert.ToByte(rgb ? int.Parse(e.Args[1]) : Convert.ToInt32(e.Args[1].Substring(0, 2), 16)); byte green = Convert.ToByte(rgb ? int.Parse(e.Args[2]) : Convert.ToInt32(e.Args[1].Substring(2, 2), 16)); byte blue = Convert.ToByte(rgb ? int.Parse(e.Args[3]) : Convert.ToInt32(e.Args[1].Substring(4, 2), 16)); - + await role.Edit(color: new Color(red, green, blue)); await e.Channel.SendMessage($"Role {role.Name}'s color has been changed."); } catch (Exception ex) { await e.Send(":warning: Unspecified error, please report this."); Console.WriteLine($".rolecolor error: {ex}"); } - + }); - - + + cgb.CreateCommand(".b").Alias(".ban") - .Parameter("everything",ParameterType.Unparsed) + .Parameter("everything", ParameterType.Unparsed) .Description("Bans a mentioned user") .Do(async e => { try { @@ -259,7 +256,7 @@ namespace NadekoBot.Modules .Parameter("user", ParameterType.Optional) .Do(async e => { var usr = e.User; - if(e.GetArg("user") != null) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); + if (e.GetArg("user") != null) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); await e.Send($"Id of the user { usr.Name } is { usr.Id }"); }); @@ -343,15 +340,15 @@ namespace NadekoBot.Modules await client.CurrentUser.Edit(NadekoBot.password, e.GetArg("new_name")); }); - - cgb.CreateCommand(".setgame") - .Description("Sets the bots game.") - .Parameter("set_game", ParameterType.Unparsed) - .Do(e => { - if (e.User.Id != NadekoBot.OwnerID || e.GetArg("set_game") == null) return; - client.SetGame(e.GetArg("set_game")); - }); + cgb.CreateCommand(".setgame") + .Description("Sets the bots game.") + .Parameter("set_game", ParameterType.Unparsed) + .Do(e => { + if (e.User.Id != NadekoBot.OwnerID || e.GetArg("set_game") == null) return; + + client.SetGame(e.GetArg("set_game")); + }); cgb.CreateCommand(".checkmyperms") .Description("Checks your userspecific permissions on this channel.") @@ -363,7 +360,7 @@ namespace NadekoBot.Modules output += "```"; await e.User.SendMessage(output); }); - + Server commsServer = null; User commsUser = null; @@ -393,7 +390,7 @@ namespace NadekoBot.Modules cgb.CreateCommand(".send") .Description("Send a message to someone on a different server through the bot.**Owner only.**\n **Usage**: .send Message text multi word!") - .Parameter("msg",ParameterType.Unparsed) + .Parameter("msg", ParameterType.Unparsed) .Do(async e => { if (e.User.Id != NadekoBot.OwnerID) return; try { @@ -435,34 +432,5 @@ namespace NadekoBot.Modules */ }); } - - bool announcingGreet = false; - Channel announceChannel = null; - Server joinServer = null; - string announceMsg = "Welcome to the server %user%"; - - private void Client_UserJoined(object sender, UserEventArgs e) { - if (e.Server != joinServer) return; - try { - announceChannel?.Send(announceMsg.Replace("%user%", e.User.Mention)); - } catch (Exception) { - Console.WriteLine("Failed sending greet message to the specified channel"); - } - - } - bool announcingLeave = false; - Channel announceLeaveChannel = null; - Server leaveServer = null; - string announceLeaveMsg = "%user% has left the server"; - - private void Client_UserLeft(object sender, UserEventArgs e) { - if (e.Server != leaveServer) return; - try { - announceLeaveChannel?.Send(announceLeaveMsg.Replace("%user%", e.User.Mention)); - } catch (Exception) { - Console.WriteLine("Failed sending leave message to the specified channel."); - } - - } } } diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations.cs index e7265a78..f8c3a175 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations.cs @@ -182,7 +182,13 @@ namespace NadekoBot.Modules .Parameter("mention", ParameterType.Required) .Do(async e => { - List praises = new List { " You are cool.", " You are nice!", " You did a good job.", " You did something nice.", " is awesome!" }; + List praises = new List { " You are cool.", + " You are nice!", + " You did a good job.", + " You did something nice.", + " is awesome!", + " Wow."}; + Random r = new Random(); var u = e.Channel.FindUsers(e.GetArg("mention")).FirstOrDefault(); @@ -320,21 +326,26 @@ namespace NadekoBot.Modules .Description("Shows the message where you were last mentioned in this channel (checks last 10k messages)") .Do(async e => { - Message msg; + Message msg = null; var msgs = e.Channel.Messages .Where(m => m.MentionedUsers.Contains(e.User)) .OrderByDescending(m => m.Timestamp); if (msgs.Count() > 0) - msg = msgs.FirstOrDefault(); + msg = msgs.First(); else { - var msgsarr = await e.Channel.DownloadMessages(10000); - msg = msgsarr - .Where(m => m.MentionedUsers.Contains(e.User)) - .OrderByDescending(m => m.Timestamp) - .FirstOrDefault(); + int attempt = 0; + Message lastMessage = null; + while (msg == null && attempt++ < 5) { + var msgsarr = await e.Channel.DownloadMessages(100, lastMessage?.Id); + msg = msgsarr + .Where(m => m.MentionedUsers.Contains(e.User)) + .OrderByDescending(m => m.Timestamp) + .FirstOrDefault(); + lastMessage = msgsarr.OrderBy(m => m.Timestamp).First(); + } } if (msg != null) - await e.Send("Last message mentioning you was at " + msg.Timestamp + "\n**Message:** " + msg.RawText.Replace("@everyone","@everryone")); + await e.Send($"Last message mentioning you was at {msg.Timestamp}\n**Message from {msg.User.Name}:** {msg.RawText.Replace("@everyone", "@everryone")}"); else await e.Send("I can't find a message mentioning you."); }); diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index cea47b50..fc8bbea1 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -58,9 +58,9 @@ namespace NadekoBot.Modules { .Do(e => { if (musicPlayers.ContainsKey(e.Server) == false) return; var player = musicPlayers[e.Server]; - player.Stop(); MusicControls throwAwayValue; musicPlayers.TryRemove(e.Server, out throwAwayValue); + player.Stop(); }); cgb.CreateCommand("p") @@ -92,7 +92,7 @@ namespace NadekoBot.Modules { } try { - var sr = player.CreateStreamRequest(e, e.GetArg("query"), player.VoiceChannel); + var sr = await player.CreateStreamRequest(e, e.GetArg("query"), player.VoiceChannel); if (sr == null) throw new NullReferenceException("StreamRequest is null."); Message msg = null; diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 8a986fa1..400bf76c 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -150,7 +150,7 @@ namespace NadekoBot { $"\nUptime: {GetUptimeString()}" + $"\nServers: {client.Servers.Count()}" + $"\nChannels: {client.Servers.Sum(s => s.AllChannels.Count())}" + - $"\nUsers: {client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} ({client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Distinct().Count()} unique) ({client.Servers.SelectMany(x => x.Users.Where(y => y.Status != UserStatus.Offline).Select(y => y.Id)).Distinct().Count()} online)" + + $"\nUsers: {client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} (non-unique)" + $"\nHeap: {Math.Round(GC.GetTotalMemory(true) / (1024.0 * 1024.0), 2).ToString()}MB" + $"\nCommands Ran this session: {commandsRan}"; } @@ -183,7 +183,7 @@ namespace NadekoBot { await OwnerUser.SendMessage(e.User + ": ```\n" + e.Message.Text + "\n```"); 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 ;(\nBot Creator's server: https://discord.gg/0ehQwTK2RBhxEi0X"); + await e.Send("**COMMANDS DO NOT WORK IN PERSONAL MESSAGES**\nYou 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 ;(\nBot Creator's server: https://discord.gg/0ehQwTK2RBhxEi0X"); Timer t = new Timer(); t.Interval = 2000; t.Start(); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index f0ff1a00..2d6ce9f5 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -73,6 +73,14 @@ true + + ..\packages\VideoLibrary.1.3.3\lib\portable-net45+win+wpa81+MonoAndroid10+xamarinios10+MonoTouch10\libvideo.dll + True + + + ..\packages\VideoLibrary.Compat.1.3.3\lib\portable-net45+win+wpa81+MonoAndroid10+xamarinios10+MonoTouch10\libvideo.compat.dll + True + ..\packages\Manatee.Json.3.2.1\lib\net45\Manatee.Json.dll True @@ -134,10 +142,6 @@ ..\packages\taglib.2.1.0.0\lib\taglib-sharp.dll True - - ..\packages\YoutubeExtractor.0.10.10\lib\net35\YoutubeExtractor.dll - True - diff --git a/NadekoBot/packages.config b/NadekoBot/packages.config index f3f8c5bd..994e1628 100644 --- a/NadekoBot/packages.config +++ b/NadekoBot/packages.config @@ -11,7 +11,6 @@ - + - \ No newline at end of file