mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-07 21:58:06 -05:00
Profanity small refactor/cleanup + nullability.
This commit is contained in:
parent
2464c58ccf
commit
d0a022f897
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using Data.Models;
|
using Data.Models;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using SharedLibraryCore;
|
using SharedLibraryCore;
|
||||||
|
using SharedLibraryCore.Database.Models;
|
||||||
using SharedLibraryCore.Events.Game;
|
using SharedLibraryCore.Events.Game;
|
||||||
using SharedLibraryCore.Events.Management;
|
using SharedLibraryCore.Events.Management;
|
||||||
using SharedLibraryCore.Interfaces;
|
using SharedLibraryCore.Interfaces;
|
||||||
@ -23,16 +24,11 @@ public class Plugin : IPluginV2
|
|||||||
|
|
||||||
private readonly ProfanityDetermentConfiguration _configuration;
|
private readonly ProfanityDetermentConfiguration _configuration;
|
||||||
|
|
||||||
public static void RegisterDependencies(IServiceCollection serviceProvider)
|
|
||||||
{
|
|
||||||
serviceProvider.AddConfiguration<ProfanityDetermentConfiguration>("ProfanityDetermentSettings");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Plugin(ProfanityDetermentConfiguration configuration)
|
public Plugin(ProfanityDetermentConfiguration configuration)
|
||||||
{
|
{
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
|
|
||||||
if (!(_configuration?.EnableProfanityDeterment ?? false))
|
if (!_configuration.EnableProfanityDeterment)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -46,46 +42,30 @@ public class Plugin : IPluginV2
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void RegisterDependencies(IServiceCollection serviceProvider)
|
||||||
|
{
|
||||||
|
serviceProvider.AddConfiguration<ProfanityDetermentConfiguration>("ProfanityDetermentSettings");
|
||||||
|
}
|
||||||
|
|
||||||
private Task GameEventSubscriptionsOnClientMessaged(ClientMessageEvent clientEvent, CancellationToken token)
|
private Task GameEventSubscriptionsOnClientMessaged(ClientMessageEvent clientEvent, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!(_configuration?.EnableProfanityDeterment ?? false))
|
if (!_configuration.EnableProfanityDeterment)
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
var offensiveWords = _configuration!.OffensiveWords;
|
var sender = clientEvent.Server.AsConsoleClient();
|
||||||
var containsOffensiveWord = false;
|
var messages = FindOffensiveWordsAndWarn(sender, clientEvent.Message);
|
||||||
var matchedFilters = new List<string>();
|
if (messages.Count is 0)
|
||||||
|
|
||||||
foreach (var word in offensiveWords.Where(word =>
|
|
||||||
Regex.IsMatch(clientEvent.Message?.StripColors() ?? string.Empty, word,
|
|
||||||
RegexOptions.IgnoreCase)))
|
|
||||||
{
|
|
||||||
containsOffensiveWord = true;
|
|
||||||
matchedFilters.Add(word);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!containsOffensiveWord)
|
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
var profanityInfringements = clientEvent.Origin.GetAdditionalProperty<int>(ProfanityKey);
|
var profanityInfringements = clientEvent.Origin.GetAdditionalProperty<int>(ProfanityKey);
|
||||||
|
|
||||||
var sender = clientEvent.Server.AsConsoleClient();
|
|
||||||
sender.AdministeredPenalties = new List<EFPenalty>
|
|
||||||
{
|
|
||||||
new()
|
|
||||||
{
|
|
||||||
AutomatedOffense = $"{clientEvent.Message} - {string.Join(",", matchedFilters)}"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (profanityInfringements >= _configuration.KickAfterInfringementCount)
|
if (profanityInfringements >= _configuration.KickAfterInfringementCount)
|
||||||
{
|
{
|
||||||
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (profanityInfringements < _configuration.KickAfterInfringementCount)
|
else if (profanityInfringements < _configuration.KickAfterInfringementCount)
|
||||||
{
|
{
|
||||||
clientEvent.Client.SetAdditionalProperty(ProfanityKey, profanityInfringements + 1);
|
clientEvent.Client.SetAdditionalProperty(ProfanityKey, profanityInfringements + 1);
|
||||||
@ -97,7 +77,7 @@ public class Plugin : IPluginV2
|
|||||||
|
|
||||||
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
|
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!(_configuration?.EnableProfanityDeterment ?? false))
|
if (!_configuration.EnableProfanityDeterment)
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@ -109,34 +89,30 @@ public class Plugin : IPluginV2
|
|||||||
|
|
||||||
clientEvent.Client.SetAdditionalProperty(ProfanityKey, 0);
|
clientEvent.Client.SetAdditionalProperty(ProfanityKey, 0);
|
||||||
|
|
||||||
var offensiveWords = _configuration!.OffensiveWords;
|
var sender = clientEvent.Client.CurrentServer.AsConsoleClient();
|
||||||
var matchedFilters = new List<string>();
|
var messages = FindOffensiveWordsAndWarn(sender, clientEvent.Client.CleanedName);
|
||||||
var containsOffensiveWord = false;
|
if (messages.Count is 0)
|
||||||
|
|
||||||
foreach (var word in offensiveWords.Where(word =>
|
|
||||||
Regex.IsMatch(clientEvent.Client.CleanedName, word, RegexOptions.IgnoreCase)))
|
|
||||||
{
|
|
||||||
containsOffensiveWord = true;
|
|
||||||
matchedFilters.Add(word);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!containsOffensiveWord)
|
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
var sender = Utilities.IW4MAdminClient(clientEvent.Client.CurrentServer);
|
|
||||||
sender.AdministeredPenalties = new List<EFPenalty>
|
|
||||||
{
|
|
||||||
new()
|
|
||||||
{
|
|
||||||
AutomatedOffense = $"{clientEvent.Client.Name} - {string.Join(",", matchedFilters)}"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<string> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@ namespace IW4MAdmin.Plugins.ProfanityDeterment
|
|||||||
{
|
{
|
||||||
public class ProfanityDetermentConfiguration : IBaseConfiguration
|
public class ProfanityDetermentConfiguration : IBaseConfiguration
|
||||||
{
|
{
|
||||||
public List<string> OffensiveWords { get; set; }
|
public List<string> OffensiveWords { get; set; } = [];
|
||||||
public bool EnableProfanityDeterment { get; set; }
|
public bool EnableProfanityDeterment { get; set; }
|
||||||
public string ProfanityWarningMessage { get; set; }
|
public required string ProfanityWarningMessage { get; set; }
|
||||||
public string ProfanityKickMessage { get; set; }
|
public required string ProfanityKickMessage { get; set; }
|
||||||
public int KickAfterInfringementCount { get; set; }
|
public int KickAfterInfringementCount { get; set; }
|
||||||
public bool KickOnInfringingName { get; set; } = true;
|
public bool KickOnInfringingName { get; set; } = true;
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ namespace SharedLibraryCore
|
|||||||
public static Layout CurrentLocalization = new Layout(new Dictionary<string, string>());
|
public static Layout CurrentLocalization = new Layout(new Dictionary<string, string>());
|
||||||
|
|
||||||
public static TimeSpan DefaultCommandTimeout { get; set; } = new(0, 0, IsDevelopment ? 360 : 25);
|
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 char CommandPrefix { get; set; } = '!';
|
||||||
|
|
||||||
public static string ToStandardFormat(this DateTime? time) => time?.ToString("yyyy-MM-dd HH:mm:ss UTC");
|
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
|
//Remove words from a space delimited string
|
||||||
public static string RemoveWords(this string str, int num)
|
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 newStr = string.Empty;
|
||||||
var tmp = str.Split(' ');
|
var tmp = str.Split(' ');
|
||||||
|
|
||||||
for (var i = 0; i < tmp.Length; i++)
|
return tmp.Where((t, i) => i >= num).Aggregate(newStr, (current, t) => current + (t + ' '));
|
||||||
if (i >= num)
|
|
||||||
{
|
|
||||||
newStr += tmp[i] + ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
return newStr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user