From 20f58e16c3dd12a96b3d2ad9192a8bb8bb306774 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 25 Feb 2019 18:51:46 +0100 Subject: [PATCH] #27 Added handler for received messages --- .../Classes/JobScheduler.cs | 39 +++++++++++++++++++ .../Core/Core.cs | 2 + 2 files changed, 41 insertions(+) diff --git a/src/Discord Media Loader.Application/Classes/JobScheduler.cs b/src/Discord Media Loader.Application/Classes/JobScheduler.cs index a0a05dd..43d8d16 100644 --- a/src/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/src/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -14,7 +14,9 @@ #endregion using Discord; +using Discord.WebSocket; using DML.AppCore.Classes; +using SweetLib.Utils.Extensions; using SweetLib.Utils.Logger; using System.Collections.Generic; using System.Threading.Tasks; @@ -129,5 +131,42 @@ namespace DML.Application.Classes Logger.Trace("SchedulerTask started"); // TODO } + + public Task HandleMessageReceived(SocketMessage msg) + { + Logger.Trace($"Received message: Id: {msg.Id}, Channel: {msg.Channel.Id}, Attachments: {msg.Attachments.Count}"); + + if (msg.Attachments.Count > 0) + { + foreach (var job in JobList) + { + if (job.State != JobState.Listening || job.ChannelId != msg.Channel.Id) + { + continue; + } + + Logger.Debug($"Job found [{job.Id}]"); + foreach (var attachment in msg.Attachments) + { + var mediaData = new MediaData + { + Id = attachment.Id, + GuildId = (msg.Channel as SocketTextChannel)?.Guild?.Id ?? 0, + ChannelId = msg.Channel.Id, + DownloadSource = attachment.Url, + Filename = attachment.Filename, + TimeStamp = msg.CreatedAt.UtcDateTime.ToUnixTimeStamp(), + FileSize = attachment.Size + }; + mediaData.Store(); + } + + job.LastMessageId = msg.Id; + job.Store(); + } + } + + return Task.CompletedTask; + } } } diff --git a/src/Discord Media Loader.Application/Core/Core.cs b/src/Discord Media Loader.Application/Core/Core.cs index 130e714..7316073 100644 --- a/src/Discord Media Loader.Application/Core/Core.cs +++ b/src/Discord Media Loader.Application/Core/Core.cs @@ -307,6 +307,8 @@ namespace DML.Application.Core }); } + DMLClient.Client.MessageReceived += Scheduler.HandleMessageReceived; + splash.Close(); Logger.Info("Starting scheduler...");