diff --git a/Plugins/ProfanityDeterment/Plugin.cs b/Plugins/ProfanityDeterment/Plugin.cs index 9745e016..a7a3abac 100644 --- a/Plugins/ProfanityDeterment/Plugin.cs +++ b/Plugins/ProfanityDeterment/Plugin.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Data.Models; using Microsoft.Extensions.DependencyInjection; using SharedLibraryCore; +using SharedLibraryCore.Database.Models; using SharedLibraryCore.Events.Game; using SharedLibraryCore.Events.Management; using SharedLibraryCore.Interfaces; @@ -23,16 +24,11 @@ public class Plugin : IPluginV2 private readonly ProfanityDetermentConfiguration _configuration; - public static void RegisterDependencies(IServiceCollection serviceProvider) - { - serviceProvider.AddConfiguration("ProfanityDetermentSettings"); - } - public Plugin(ProfanityDetermentConfiguration configuration) { _configuration = configuration; - if (!(_configuration?.EnableProfanityDeterment ?? false)) + if (!_configuration.EnableProfanityDeterment) { return; } @@ -46,46 +42,30 @@ public class Plugin : IPluginV2 }; } + public static void RegisterDependencies(IServiceCollection serviceProvider) + { + serviceProvider.AddConfiguration("ProfanityDetermentSettings"); + } + private Task GameEventSubscriptionsOnClientMessaged(ClientMessageEvent clientEvent, CancellationToken token) { - if (!(_configuration?.EnableProfanityDeterment ?? false)) + if (!_configuration.EnableProfanityDeterment) { return Task.CompletedTask; } - var offensiveWords = _configuration!.OffensiveWords; - var containsOffensiveWord = false; - var matchedFilters = new List(); - - foreach (var word in offensiveWords.Where(word => - Regex.IsMatch(clientEvent.Message?.StripColors() ?? string.Empty, word, - RegexOptions.IgnoreCase))) - { - containsOffensiveWord = true; - matchedFilters.Add(word); - } - - if (!containsOffensiveWord) + var sender = clientEvent.Server.AsConsoleClient(); + var messages = FindOffensiveWordsAndWarn(sender, clientEvent.Message); + if (messages.Count is 0) { return Task.CompletedTask; } var profanityInfringements = clientEvent.Origin.GetAdditionalProperty(ProfanityKey); - - var sender = clientEvent.Server.AsConsoleClient(); - sender.AdministeredPenalties = new List - { - new() - { - AutomatedOffense = $"{clientEvent.Message} - {string.Join(",", matchedFilters)}" - } - }; - if (profanityInfringements >= _configuration.KickAfterInfringementCount) { clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender); } - else if (profanityInfringements < _configuration.KickAfterInfringementCount) { clientEvent.Client.SetAdditionalProperty(ProfanityKey, profanityInfringements + 1); @@ -97,7 +77,7 @@ public class Plugin : IPluginV2 private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token) { - if (!(_configuration?.EnableProfanityDeterment ?? false)) + if (!_configuration.EnableProfanityDeterment) { return Task.CompletedTask; } @@ -109,34 +89,30 @@ public class Plugin : IPluginV2 clientEvent.Client.SetAdditionalProperty(ProfanityKey, 0); - var offensiveWords = _configuration!.OffensiveWords; - var matchedFilters = new List(); - var containsOffensiveWord = false; - - foreach (var word in offensiveWords.Where(word => - Regex.IsMatch(clientEvent.Client.CleanedName, word, RegexOptions.IgnoreCase))) - { - containsOffensiveWord = true; - matchedFilters.Add(word); - break; - } - - if (!containsOffensiveWord) + var sender = clientEvent.Client.CurrentServer.AsConsoleClient(); + var messages = FindOffensiveWordsAndWarn(sender, clientEvent.Client.CleanedName); + if (messages.Count is 0) { return Task.CompletedTask; } - var sender = Utilities.IW4MAdminClient(clientEvent.Client.CurrentServer); - sender.AdministeredPenalties = new List - { - new() - { - AutomatedOffense = $"{clientEvent.Client.Name} - {string.Join(",", matchedFilters)}" - } - }; - clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender); - return Task.CompletedTask; } + + private List FindOffensiveWordsAndWarn(EFClient sender, string message) + { + var offensiveWords = _configuration.OffensiveWords; + var matchedWords = offensiveWords.Where(word => Regex.IsMatch(message.StripColors(), word, RegexOptions.IgnoreCase)).ToList(); + + sender.AdministeredPenalties = + [ + new EFPenalty + { + AutomatedOffense = $"{message} - {string.Join(",", matchedWords)}" + } + ]; + + return matchedWords; + } } diff --git a/Plugins/ProfanityDeterment/ProfanityDetermentConfiguration.cs b/Plugins/ProfanityDeterment/ProfanityDetermentConfiguration.cs index a6004cbe..d66e8a57 100644 --- a/Plugins/ProfanityDeterment/ProfanityDetermentConfiguration.cs +++ b/Plugins/ProfanityDeterment/ProfanityDetermentConfiguration.cs @@ -6,10 +6,10 @@ namespace IW4MAdmin.Plugins.ProfanityDeterment { public class ProfanityDetermentConfiguration : IBaseConfiguration { - public List OffensiveWords { get; set; } + public List OffensiveWords { get; set; } = []; public bool EnableProfanityDeterment { get; set; } - public string ProfanityWarningMessage { get; set; } - public string ProfanityKickMessage { get; set; } + public required string ProfanityWarningMessage { get; set; } + public required string ProfanityKickMessage { get; set; } public int KickAfterInfringementCount { get; set; } public bool KickOnInfringingName { get; set; } = true; diff --git a/SharedLibraryCore/Utilities.cs b/SharedLibraryCore/Utilities.cs index db5babbc..e251e96e 100644 --- a/SharedLibraryCore/Utilities.cs +++ b/SharedLibraryCore/Utilities.cs @@ -49,7 +49,7 @@ namespace SharedLibraryCore public static Layout CurrentLocalization = new Layout(new Dictionary()); public static TimeSpan DefaultCommandTimeout { get; set; } = new(0, 0, IsDevelopment ? 360 : 25); - public static char[] DirectorySeparatorChars = { '\\', '/' }; + public static readonly char[] DirectorySeparatorChars = ['\\', '/']; public static char CommandPrefix { get; set; } = '!'; public static string ToStandardFormat(this DateTime? time) => time?.ToString("yyyy-MM-dd HH:mm:ss UTC"); @@ -97,21 +97,15 @@ namespace SharedLibraryCore //Remove words from a space delimited string public static string RemoveWords(this string str, int num) { - if (str == null || str.Length == 0) + if (string.IsNullOrEmpty(str)) { - return ""; + return string.Empty; } var newStr = string.Empty; var tmp = str.Split(' '); - for (var i = 0; i < tmp.Length; i++) - if (i >= num) - { - newStr += tmp[i] + ' '; - } - - return newStr; + return tmp.Where((t, i) => i >= num).Aggregate(newStr, (current, t) => current + (t + ' ')); } ///