From 3aff5d0a12bb024c886da911cfc786eee7487684 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 31 Jul 2016 20:06:09 +0200 Subject: [PATCH] User presence updates are now sent every 5 seconds. #468 --- .../Administration/Commands/LogCommand.cs | 51 ++++++++++++++++--- .../Modules/Help/Commands/HelpCommand.cs | 3 +- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/NadekoBot/Modules/Administration/Commands/LogCommand.cs b/NadekoBot/Modules/Administration/Commands/LogCommand.cs index 08212b62..db693ae3 100644 --- a/NadekoBot/Modules/Administration/Commands/LogCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/LogCommand.cs @@ -4,7 +4,10 @@ using NadekoBot.Classes; using NadekoBot.Extensions; using NadekoBot.Modules.Permissions.Classes; using System; +using System.Collections.Concurrent; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace NadekoBot.Modules.Administration.Commands { @@ -12,6 +15,8 @@ namespace NadekoBot.Modules.Administration.Commands { private string prettyCurrentTime => $"【{DateTime.Now:HH:mm:ss}】"; + private ConcurrentBag> voicePresenceUpdates = new ConcurrentBag>(); + public LogCommand(DiscordModule module) : base(module) { NadekoBot.Client.MessageReceived += MsgRecivd; @@ -45,6 +50,36 @@ namespace NadekoBot.Modules.Administration.Commands } catch { } }; + + // start the userpresence queue + + NadekoBot.OnReady += () => Task.Run(async () => + { + while (true) + { + var toSend = new Dictionary(); + //take everything from the queue and merge the messages which are going to the same channel + KeyValuePair item; + while (voicePresenceUpdates.TryTake(out item)) + { + if (toSend.ContainsKey(item.Key)) + { + toSend[item.Key] = toSend[item.Key] + Environment.NewLine + item.Value; + } + else + { + toSend.Add(item.Key, item.Value); + } + } + //send merged messages to each channel + foreach (var k in toSend) + { + try { await k.Key.SendMessage(Environment.NewLine + k.Value).ConfigureAwait(false); } catch { } + } + + await Task.Delay(5000); + } + }); } private async void ChannelUpdated(object sender, ChannelUpdatedEventArgs e) @@ -177,13 +212,13 @@ namespace NadekoBot.Modules.Administration.Commands if (!string.IsNullOrWhiteSpace(e.Message.Text)) { await ch.SendMessage( - $@"🕔`{prettyCurrentTime}` **New Message** `#{e.Channel.Name}` + $@"🕔`{prettyCurrentTime}` **New Message** `#{e.Channel.Name}` 👤`{e.User?.ToString() ?? ("NULL")}` {e.Message.Text.Unmention()}").ConfigureAwait(false); } else { await ch.SendMessage( - $@"🕔`{prettyCurrentTime}` **File Uploaded** `#{e.Channel.Name}` + $@"🕔`{prettyCurrentTime}` **File Uploaded** `#{e.Channel.Name}` 👤`{e.User?.ToString() ?? ("NULL")}` {e.Message.Attachments.FirstOrDefault()?.ProxyUrl}").ConfigureAwait(false); } @@ -206,13 +241,13 @@ namespace NadekoBot.Modules.Administration.Commands if (!string.IsNullOrWhiteSpace(e.Message.Text)) { await ch.SendMessage( - $@"🕔`{prettyCurrentTime}` **Message** 🚮 `#{e.Channel.Name}` + $@"🕔`{prettyCurrentTime}` **Message** 🚮 `#{e.Channel.Name}` 👤`{e.User?.ToString() ?? ("NULL")}` {e.Message.Text.Unmention()}").ConfigureAwait(false); } else { await ch.SendMessage( - $@"🕔`{prettyCurrentTime}` **File Deleted** `#{e.Channel.Name}` + $@"🕔`{prettyCurrentTime}` **File Deleted** `#{e.Channel.Name}` 👤`{e.User?.ToString() ?? ("NULL")}` {e.Message.Attachments.FirstOrDefault()?.ProxyUrl}").ConfigureAwait(false); } } @@ -232,7 +267,7 @@ namespace NadekoBot.Modules.Administration.Commands if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null) return; await ch.SendMessage( -$@"🕔`{prettyCurrentTime}` **Message** 📝 `#{e.Channel.Name}` + $@"🕔`{prettyCurrentTime}` **Message** 📝 `#{e.Channel.Name}` 👤`{e.User?.ToString() ?? ("NULL")}` `Old:` {e.Before.Text.Unmention()} `New:` {e.After.Text.Unmention()}").ConfigureAwait(false); @@ -252,7 +287,7 @@ $@"🕔`{prettyCurrentTime}` **Message** 📝 `#{e.Channel.Name}` { if (e.Before.Status != e.After.Status) { - await ch.SendMessage($"`{prettyCurrentTime}`**{e.Before.Name}** is now **{e.After.Status}**.").ConfigureAwait(false); + voicePresenceUpdates.Add(new KeyValuePair(ch, $"`{prettyCurrentTime}`**{e.Before.Name}** is now **{e.After.Status}**.")); } } } @@ -371,8 +406,8 @@ $@"🕔`{prettyCurrentTime}` **Message** 📝 `#{e.Channel.Name}` Channel ch; if ((ch = e.Server.TextChannels.Where(tc => tc.Id == chId).FirstOrDefault()) == null) return; - - SpecificConfigurations.Default.Of (e.Server.Id).LogServerChannel = null; + + SpecificConfigurations.Default.Of(e.Server.Id).LogServerChannel = null; await e.Channel.SendMessage($"❗**NO LONGER LOGGING IN {ch.Mention} CHANNEL**❗").ConfigureAwait(false); }); diff --git a/NadekoBot/Modules/Help/Commands/HelpCommand.cs b/NadekoBot/Modules/Help/Commands/HelpCommand.cs index 1ed5f73c..0adce45e 100644 --- a/NadekoBot/Modules/Help/Commands/HelpCommand.cs +++ b/NadekoBot/Modules/Help/Commands/HelpCommand.cs @@ -52,8 +52,7 @@ $@"######For more information and how to setup your own NadekoBot, go to: ######or paypal: `nadekodiscordbot@gmail.com` -#NadekoBot List Of Commands -Version: `{NadekoStats.Instance.BotVersion}`"; +#NadekoBot List Of Commands "; string lastCategory = "";