#96 Added nickname to filename variables

This commit is contained in:
Serraniel 2021-12-05 19:10:40 +01:00
parent 4d2d111c74
commit 55baa35cba
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

View file

@ -7,7 +7,6 @@ using System.Threading.Tasks;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using DML.Application.Classes; using DML.Application.Classes;
using SweetLib.Utils;
using SweetLib.Utils.Extensions; using SweetLib.Utils.Extensions;
using SweetLib.Utils.Logger; using SweetLib.Utils.Logger;
@ -15,14 +14,14 @@ namespace DML.AppCore.Classes
{ {
public class JobScheduler public class JobScheduler
{ {
private ulong messageScanned = 0; private ulong attachmentsDownloaded;
private ulong totalAttachments = 0; private ulong messageScanned;
private ulong attachmentsDownloaded = 0;
private bool Run { get; set; } = false;
public List<Job> JobList { get; set; } = new List<Job>();
public Dictionary<int, Queue<IMessage>> RunningJobs = new Dictionary<int, Queue<IMessage>>(); public Dictionary<int, Queue<IMessage>> RunningJobs = new Dictionary<int, Queue<IMessage>>();
internal int RunningThreads { get; set; } = 0; private ulong totalAttachments;
private bool Run { get; set; }
public List<Job> JobList { get; set; } = new List<Job>();
internal int RunningThreads { get; set; }
internal ulong MessagesScanned internal ulong MessagesScanned
{ {
@ -91,7 +90,6 @@ namespace DML.AppCore.Classes
{ {
Logger.Info("Started JobScheduler..."); Logger.Info("Started JobScheduler...");
while (Run) while (Run)
{
try try
{ {
Logger.Debug("Entering job list handler loop..."); Logger.Debug("Entering job list handler loop...");
@ -108,6 +106,7 @@ namespace DML.AppCore.Classes
Logger.Trace("Checking if job is already performed..."); Logger.Trace("Checking if job is already performed...");
hasJob = RunningJobs.ContainsKey(job.Id); hasJob = RunningJobs.ContainsKey(job.Id);
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
if (!hasJob) if (!hasJob)
@ -121,6 +120,7 @@ namespace DML.AppCore.Classes
Logger.Trace("Adding job to running jobs."); Logger.Trace("Adding job to running jobs.");
RunningJobs.Add(job.Id, queue); RunningJobs.Add(job.Id, queue);
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
Logger.Trace("Issuing job message scan..."); Logger.Trace("Issuing job message scan...");
@ -130,10 +130,7 @@ namespace DML.AppCore.Classes
continue; continue;
Logger.Trace($"Adding {messages.Count} messages to queue..."); Logger.Trace($"Adding {messages.Count} messages to queue...");
foreach (var msg in messages) foreach (var msg in messages) queue.Enqueue(msg);
{
queue.Enqueue(msg);
}
Logger.Trace($"Added {queue.Count} messages to queue."); Logger.Trace($"Added {queue.Count} messages to queue.");
if (messages.Count != queue.Count) if (messages.Count != queue.Count)
@ -155,6 +152,7 @@ namespace DML.AppCore.Classes
RunningThreads++; RunningThreads++;
startedDownload = true; startedDownload = true;
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
} }
@ -167,7 +165,6 @@ namespace DML.AppCore.Classes
{ {
Logger.Error(ex.Message); Logger.Error(ex.Message);
} }
}
}); });
} }
@ -184,6 +181,7 @@ namespace DML.AppCore.Classes
Logger.Warn($"Associating job not found! JobId: {jobId}"); Logger.Warn($"Associating job not found! JobId: {jobId}");
return; return;
} }
Logger.Trace("Found job."); Logger.Trace("Found job.");
Queue<IMessage> queue; Queue<IMessage> queue;
@ -196,8 +194,10 @@ namespace DML.AppCore.Classes
Logger.Warn($"Queue for job {jobId} not found!"); Logger.Warn($"Queue for job {jobId} not found!");
return; return;
} }
Logger.Trace("Queue found."); Logger.Trace("Queue found.");
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
Logger.Debug($"Messages to process for job {jobId}: {queue.Count}"); Logger.Debug($"Messages to process for job {jobId}: {queue.Count}");
@ -212,8 +212,10 @@ namespace DML.AppCore.Classes
Logger.Warn($"Queue for job {jobId} not found!"); Logger.Warn($"Queue for job {jobId} not found!");
return; return;
} }
Logger.Trace("Continue working..."); Logger.Trace("Continue working...");
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
Logger.Trace("Dequeueing message..."); Logger.Trace("Dequeueing message...");
@ -221,7 +223,6 @@ namespace DML.AppCore.Classes
Logger.Debug($"Attachments for message {message.Id}: {message.Attachments.Count}"); Logger.Debug($"Attachments for message {message.Id}: {message.Attachments.Count}");
foreach (var a in message.Attachments) foreach (var a in message.Attachments)
{
try try
{ {
var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme); var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme);
@ -231,16 +232,20 @@ namespace DML.AppCore.Classes
var extensionRequired = !fileName.EndsWith("%name%"); var extensionRequired = !fileName.EndsWith("%name%");
var serverName = "unknown"; var serverName = "unknown";
string nickname = null;
var socketTextChannel = message.Channel as SocketTextChannel; var socketTextChannel = message.Channel as SocketTextChannel;
if (socketTextChannel != null) if (socketTextChannel != null)
{ {
serverName = socketTextChannel.Guild.Name; serverName = socketTextChannel.Guild.Name;
serverName = Path.GetInvalidFileNameChars().Aggregate(serverName, (current, c) => current.Replace(c, ' ')); serverName = Path.GetInvalidFileNameChars()
.Aggregate(serverName, (current, c) => current.Replace(c, ' '));
nickname = socketTextChannel.GetUser(message.Author.Id).Nickname;
} }
var channelName = message.Channel.Name; var channelName = message.Channel.Name;
channelName = Path.GetInvalidFileNameChars().Aggregate(channelName, (current, c) => current.Replace(c, ' ')); channelName = Path.GetInvalidFileNameChars()
.Aggregate(channelName, (current, c) => current.Replace(c, ' '));
fileName = fileName =
fileName.Replace("%guild%", serverName) fileName.Replace("%guild%", serverName)
@ -249,7 +254,9 @@ namespace DML.AppCore.Classes
.Replace("%name%", a.Filename) .Replace("%name%", a.Filename)
.Replace("%id%", a.Id.ToString()) .Replace("%id%", a.Id.ToString())
.Replace("%userid%", message.Author.Id.ToString()) .Replace("%userid%", message.Author.Id.ToString())
.Replace("%username%", message.Author.Username); .Replace("%username%", message.Author.Username)
.Replace("%nickname%",
!string.IsNullOrEmpty(nickname) ? nickname : message.Author.Username);
if (extensionRequired) if (extensionRequired)
fileName += Path.GetExtension(a.Filename); fileName += Path.GetExtension(a.Filename);
@ -287,7 +294,6 @@ namespace DML.AppCore.Classes
{ {
AttachmentsDownloaded++; AttachmentsDownloaded++;
} }
}
} }
} }
finally finally
@ -300,6 +306,7 @@ namespace DML.AppCore.Classes
Logger.Trace("Decreasing thread count..."); Logger.Trace("Decreasing thread count...");
RunningThreads--; RunningThreads--;
} }
Logger.Trace("Unlocked scheduler."); Logger.Trace("Unlocked scheduler.");
} }
} }