From 767b2b35d129e157130b21ef185b74232991a480 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Thu, 16 Dec 2021 14:41:05 +0100 Subject: [PATCH 1/3] #105 Added check if user is assigned during server nickname fetching --- Discord Media Loader.Application/Classes/JobScheduler.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Discord Media Loader.Application/Classes/JobScheduler.cs b/Discord Media Loader.Application/Classes/JobScheduler.cs index b5ccb03..ff90b4b 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -240,7 +240,11 @@ namespace DML.AppCore.Classes serverName = socketTextChannel.Guild.Name; serverName = Path.GetInvalidFileNameChars() .Aggregate(serverName, (current, c) => current.Replace(c, ' ')); - nickname = socketTextChannel.GetUser(message.Author.Id).Nickname; + + var serverUser = + socketTextChannel.GetUser(message.Author.Id); // can be null if user left the server + if (serverUser != null) + nickname = serverUser.Nickname; } var channelName = message.Channel.Name; -- 2.43.5 From dccbd4ab0b85c8e43c3057c9f825dcea38fc1caf Mon Sep 17 00:00:00 2001 From: Serraniel Date: Thu, 16 Dec 2021 14:47:47 +0100 Subject: [PATCH 2/3] #105 Only fetch some data if it is required to generate filename --- .../Classes/JobScheduler.cs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Discord Media Loader.Application/Classes/JobScheduler.cs b/Discord Media Loader.Application/Classes/JobScheduler.cs index ff90b4b..4f2e4a2 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -234,19 +234,29 @@ namespace DML.AppCore.Classes var serverName = "unknown"; string nickname = null; - var socketTextChannel = message.Channel as SocketTextChannel; - if (socketTextChannel != null) + if (fileName.Contains("%guildid%") || fileName.Contains("%nickname%")) { - serverName = socketTextChannel.Guild.Name; - serverName = Path.GetInvalidFileNameChars() - .Aggregate(serverName, (current, c) => current.Replace(c, ' ')); + var socketTextChannel = message.Channel as SocketTextChannel; + if (socketTextChannel != null) + { + // fetch servername + serverName = socketTextChannel.Guild.Name; + serverName = Path.GetInvalidFileNameChars() + .Aggregate(serverName, (current, c) => current.Replace(c, ' ')); - var serverUser = - socketTextChannel.GetUser(message.Author.Id); // can be null if user left the server - if (serverUser != null) - nickname = serverUser.Nickname; + // fetch nickname + if (fileName.Contains("%nickname%")) + { + var serverUser = + socketTextChannel.GetUser(message.Author + .Id); // can be null if user left the server + if (serverUser != null) + nickname = serverUser.Nickname; + } + } } + // fetch channelname var channelName = message.Channel.Name; channelName = Path.GetInvalidFileNameChars() .Aggregate(channelName, (current, c) => current.Replace(c, ' ')); -- 2.43.5 From ed8039dfbcab7c3158aec8b66bbe0e50fdb440ab Mon Sep 17 00:00:00 2001 From: Serraniel Date: Thu, 16 Dec 2021 14:53:37 +0100 Subject: [PATCH 3/3] #105 Extended filename legalisation to usernames --- .../Classes/JobScheduler.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Discord Media Loader.Application/Classes/JobScheduler.cs b/Discord Media Loader.Application/Classes/JobScheduler.cs index 4f2e4a2..710b761 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -251,7 +251,13 @@ namespace DML.AppCore.Classes socketTextChannel.GetUser(message.Author .Id); // can be null if user left the server if (serverUser != null) + { nickname = serverUser.Nickname; + + if (!string.IsNullOrEmpty(nickname)) + nickname = Path.GetInvalidFileNameChars() + .Aggregate(nickname, (current, c) => current.Replace(c, ' ')); + } } } } @@ -261,6 +267,12 @@ namespace DML.AppCore.Classes channelName = Path.GetInvalidFileNameChars() .Aggregate(channelName, (current, c) => current.Replace(c, ' ')); + // fetch username + var username = message.Author.Username; + username = Path.GetInvalidFileNameChars() + .Aggregate(username, (current, c) => current.Replace(c, ' ')); + + fileName = fileName.Replace("%guild%", serverName) .Replace("%channel%", channelName) @@ -268,9 +280,9 @@ namespace DML.AppCore.Classes .Replace("%name%", a.Filename) .Replace("%id%", a.Id.ToString()) .Replace("%userid%", message.Author.Id.ToString()) - .Replace("%username%", message.Author.Username) + .Replace("%username%", username) .Replace("%nickname%", - !string.IsNullOrEmpty(nickname) ? nickname : message.Author.Username); + !string.IsNullOrEmpty(nickname) ? nickname : username); if (extensionRequired) fileName += Path.GetExtension(a.Filename); -- 2.43.5