Added DMLogger module

This commit is contained in:
Noikoio 2017-11-17 12:13:47 -08:00
parent 6165129d81
commit 06c94e370a
3 changed files with 55 additions and 1 deletions

View file

@ -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
{
/// <summary>
/// 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.
/// </summary>
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<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
{
if (!(arg2.Channel is IDMChannel)) return;
await ProcessMessage(arg2, true);
}
public override Task<object> ProcessConfiguration(JToken configSection) => Task.FromResult<object>(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' }));
}
}
}

View file

@ -45,6 +45,7 @@ namespace Noikoio.RegexBot
// Initialize modules // Initialize modules
_modules = new BotModule[] _modules = new BotModule[]
{ {
new Module.DMLogger.DMLogger(_client),
new Module.AutoMod.AutoMod(_client), new Module.AutoMod.AutoMod(_client),
new Module.ModTools.ModTools(_client), new Module.ModTools.ModTools(_client),
new Module.AutoRespond.AutoRespond(_client), new Module.AutoRespond.AutoRespond(_client),

View file

@ -4,7 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>Noikoio.RegexBot</RootNamespace> <RootNamespace>Noikoio.RegexBot</RootNamespace>
<AssemblyVersion>2.2.0.0</AssemblyVersion> <AssemblyVersion>2.3.1.0</AssemblyVersion>
<Description>Highly configurable Discord moderation bot</Description> <Description>Highly configurable Discord moderation bot</Description>
<Authors>Noikoio</Authors> <Authors>Noikoio</Authors>
<Company /> <Company />