From 06c94e370ab84937ce8cd66531fc18e9f68d8148 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Fri, 17 Nov 2017 12:13:47 -0800 Subject: [PATCH] Added DMLogger module --- Module/DMLogger/DMLogger.cs | 53 +++++++++++++++++++++++++++++++++++++ RegexBot.cs | 1 + RegexBot.csproj | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 Module/DMLogger/DMLogger.cs diff --git a/Module/DMLogger/DMLogger.cs b/Module/DMLogger/DMLogger.cs new file mode 100644 index 0000000..a63f60c --- /dev/null +++ b/Module/DMLogger/DMLogger.cs @@ -0,0 +1,53 @@ +using Discord; +using Discord.WebSocket; +using Newtonsoft.Json.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Noikoio.RegexBot.Module.DMLogger +{ + /// + /// Listens for and logs direct messages sent to the bot. + /// The function of this module should be transparent to the user, and thus no configuration is needed. + /// + class DMLogger : BotModule + { + public override string Name => nameof(DMLogger); + + public DMLogger(DiscordSocketClient client) : base(client) + { + client.MessageReceived += Client_MessageReceived; + client.MessageUpdated += Client_MessageUpdated; + } + + private async Task Client_MessageReceived(SocketMessage arg) + { + if (!(arg.Channel is IDMChannel)) return; + + await ProcessMessage(arg, false); + } + + private async Task Client_MessageUpdated(Cacheable arg1, SocketMessage arg2, ISocketMessageChannel arg3) + { + if (!(arg2.Channel is IDMChannel)) return; + + await ProcessMessage(arg2, true); + } + + public override Task ProcessConfiguration(JToken configSection) => Task.FromResult(null); + + private async Task ProcessMessage(SocketMessage arg, bool edited) + { + var result = new StringBuilder(); + result.Append(arg.Author.ToString() + (edited ? "(edit) " : "") + ": "); + if (!string.IsNullOrWhiteSpace(arg.Content)) + { + if (arg.Content.Contains("\n")) result.AppendLine(); // If multi-line, show sender on separate line + result.AppendLine(arg.Content); + } + foreach (var i in arg.Attachments) result.AppendLine($"[Attachment: {i.Url}]"); + + await Log(result.ToString().TrimEnd(new char[] { ' ', '\r', '\n' })); + } + } +} diff --git a/RegexBot.cs b/RegexBot.cs index 97e8b8f..0cf019b 100644 --- a/RegexBot.cs +++ b/RegexBot.cs @@ -45,6 +45,7 @@ namespace Noikoio.RegexBot // Initialize modules _modules = new BotModule[] { + new Module.DMLogger.DMLogger(_client), new Module.AutoMod.AutoMod(_client), new Module.ModTools.ModTools(_client), new Module.AutoRespond.AutoRespond(_client), diff --git a/RegexBot.csproj b/RegexBot.csproj index 8ceb30a..bb599f3 100644 --- a/RegexBot.csproj +++ b/RegexBot.csproj @@ -4,7 +4,7 @@ Exe netcoreapp2.0 Noikoio.RegexBot - 2.2.0.0 + 2.3.1.0 Highly configurable Discord moderation bot Noikoio