From c39d65dffd4c89511ab38b7a73b97f8481ffbcea Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 7 Jan 2017 00:00:55 +0100 Subject: [PATCH] Some logging fix --- .../Discord/SocketMessageEventWrapper.cs | 66 +++++++++++++++++++ src/NadekoBot/ShardedDiscordClient.cs | 2 + 2 files changed, 68 insertions(+) create mode 100644 src/NadekoBot/Services/Discord/SocketMessageEventWrapper.cs diff --git a/src/NadekoBot/Services/Discord/SocketMessageEventWrapper.cs b/src/NadekoBot/Services/Discord/SocketMessageEventWrapper.cs new file mode 100644 index 00000000..7c24b564 --- /dev/null +++ b/src/NadekoBot/Services/Discord/SocketMessageEventWrapper.cs @@ -0,0 +1,66 @@ +using Discord; +using Discord.WebSocket; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Discord +{ + public class ReactionEventWrapper : IDisposable + { + public SocketMessage Message { get; } + public event Action OnReactionAdded = delegate { }; + public event Action OnReactionRemoved = delegate { }; + public event Action OnReactionsCleared = delegate { }; + + public ReactionEventWrapper(SocketMessage msg) + { + if (msg == null) + throw new ArgumentNullException(nameof(msg)); + Message = msg; + + msg.Discord.ReactionAdded += Discord_ReactionAdded; + msg.Discord.ReactionRemoved += Discord_ReactionRemoved; + msg.Discord.ReactionsCleared += Discord_ReactionsCleared; + } + + private Task Discord_ReactionsCleared(ulong messageId, Optional reaction) + { + if (messageId == Message.Id) + OnReactionsCleared?.Invoke(); + return Task.CompletedTask; + } + + private Task Discord_ReactionRemoved(ulong messageId, Optional arg2, SocketReaction reaction) + { + if (messageId == Message.Id) + OnReactionRemoved?.Invoke(reaction); + return Task.CompletedTask; + } + + private Task Discord_ReactionAdded(ulong messageId, Optional message, SocketReaction reaction) + { + if(messageId == Message.Id) + OnReactionAdded?.Invoke(reaction); + return Task.CompletedTask; + } + + public void UnsubAll() + { + Message.Discord.ReactionAdded -= Discord_ReactionAdded; + Message.Discord.ReactionRemoved -= Discord_ReactionRemoved; + Message.Discord.ReactionsCleared -= Discord_ReactionsCleared; + } + + private bool disposing = false; + public void Dispose() + { + if (disposing) + return; + disposing = true; + UnsubAll(); + } + } +} diff --git a/src/NadekoBot/ShardedDiscordClient.cs b/src/NadekoBot/ShardedDiscordClient.cs index c2300bd0..580f0e8d 100644 --- a/src/NadekoBot/ShardedDiscordClient.cs +++ b/src/NadekoBot/ShardedDiscordClient.cs @@ -65,7 +65,9 @@ namespace NadekoBot client.ChannelUpdated += (arg1, arg2) => { ChannelUpdated(arg1, arg2); return Task.CompletedTask; }; _log.Info($"Shard #{i} initialized."); +#if GLOBAL_NADEKO client.Log += Client_Log; +#endif var j = i; client.Disconnected += (ex) => {