RegexBot/Module/DMLogger/DMLogger.cs
Noikoio 287bb33d77 Removed manual module name setting
Modules will now be named based on their existing class name.
Their respective configuration sections are now also defined by the
same value. Documentation has been updated to reflect this.
2018-03-21 23:51:50 -07:00

53 lines
1.9 KiB
C#

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 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;
if (arg.Author.IsBot) return;
await ProcessMessage(arg, false);
}
private async Task Client_MessageUpdated(Cacheable<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
{
if (!(arg2.Channel is IDMChannel)) return;
if (arg2.Author.IsBot) 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' }));
}
}
}