update profanity determent plugin to IPluginV2

This commit is contained in:
RaidMax 2023-02-11 20:49:21 -06:00
parent a50e61318c
commit 4ba56b53a4
3 changed files with 115 additions and 102 deletions

View File

@ -1,124 +1,137 @@
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Data.Models;
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore;
using SharedLibraryCore.Events.Game;
using SharedLibraryCore.Events.Management;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Interfaces.Events;
namespace IW4MAdmin.Plugins.ProfanityDeterment
namespace IW4MAdmin.Plugins.ProfanityDeterment;
public class Plugin : IPluginV2
{
public class Plugin : IPlugin
public string Name => "ProfanityDeterment";
public string Version => Utilities.GetVersionAsString();
public string Author => "RaidMax";
private const string ProfanityKey = "_profanityInfringements";
private readonly ProfanityDetermentConfiguration _configuration;
public static void RegisterDependencies(IServiceCollection serviceProvider)
{
public string Name => "ProfanityDeterment";
serviceProvider.AddConfiguration<ProfanityDetermentConfiguration>("ProfanityDetermentSettings");
}
public float Version => Assembly.GetExecutingAssembly().GetName().Version.Major + Assembly.GetExecutingAssembly().GetName().Version.Minor / 10.0f;
public Plugin(ProfanityDetermentConfiguration configuration)
{
_configuration = configuration;
public string Author => "RaidMax";
private readonly IConfigurationHandler<Configuration> _configHandler;
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
if (_configuration?.EnableProfanityDeterment ?? false)
{
_configHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("ProfanityDetermentSettings");
return;
}
public Task OnEventAsync(GameEvent E, Server S)
IManagementEventSubscriptions.ClientStateInitialized += OnClientStateInitialized;
IGameEventSubscriptions.ClientMessaged += GameEventSubscriptionsOnClientMessaged;
IManagementEventSubscriptions.ClientStateDisposed += (clientEvent, _) =>
{
if (!_configHandler.Configuration().EnableProfanityDeterment)
return Task.CompletedTask;
clientEvent.Client.SetAdditionalProperty(ProfanityKey, null);
return Task.CompletedTask;
};
}
if (E.Type == GameEvent.EventType.Connect)
{
E.Origin.SetAdditionalProperty("_profanityInfringements", 0);
var objectionalWords = _configHandler.Configuration().OffensiveWords;
var matchedFilters = new List<string>();
bool containsObjectionalWord = false;
foreach (string word in objectionalWords)
{
if (Regex.IsMatch(E.Origin.Name.ToLower(), word, RegexOptions.IgnoreCase))
{
containsObjectionalWord |= true;
matchedFilters.Add(word);
}
}
if (containsObjectionalWord)
{
var sender = Utilities.IW4MAdminClient(E.Owner);
sender.AdministeredPenalties = new List<EFPenalty>()
{
new EFPenalty()
{
AutomatedOffense = $"{E.Origin.Name} - {string.Join(",", matchedFilters)}"
}
};
E.Origin.Kick(_configHandler.Configuration().ProfanityKickMessage, sender);
};
}
if (E.Type == GameEvent.EventType.Disconnect)
{
E.Origin.SetAdditionalProperty("_profanityInfringements", 0);
}
if (E.Type == GameEvent.EventType.Say)
{
var objectionalWords = _configHandler.Configuration().OffensiveWords;
bool containsObjectionalWord = false;
var matchedFilters = new List<string>();
foreach (string word in objectionalWords)
{
if (Regex.IsMatch(E.Data.ToLower(), word, RegexOptions.IgnoreCase))
{
containsObjectionalWord |= true;
matchedFilters.Add(word);
}
}
if (containsObjectionalWord)
{
int profanityInfringments = E.Origin.GetAdditionalProperty<int>("_profanityInfringements");
var sender = Utilities.IW4MAdminClient(E.Owner);
sender.AdministeredPenalties = new List<EFPenalty>()
{
new EFPenalty()
{
AutomatedOffense = $"{E.Data} - {string.Join(",", matchedFilters)}"
}
};
if (profanityInfringments >= _configHandler.Configuration().KickAfterInfringementCount)
{
E.Origin.Kick(_configHandler.Configuration().ProfanityKickMessage, sender);
}
else if (profanityInfringments < _configHandler.Configuration().KickAfterInfringementCount)
{
E.Origin.SetAdditionalProperty("_profanityInfringements", profanityInfringments + 1);
E.Origin.Warn(_configHandler.Configuration().ProfanityWarningMessage, sender);
}
}
}
private Task GameEventSubscriptionsOnClientMessaged(ClientMessageEvent clientEvent, CancellationToken token)
{
if (!_configuration?.EnableProfanityDeterment ?? false)
{
return Task.CompletedTask;
}
public async Task OnLoadAsync(IManager manager)
var offensiveWords = _configuration!.OffensiveWords;
var containsOffensiveWord = false;
var matchedFilters = new List<string>();
foreach (var word in offensiveWords.Where(word =>
Regex.IsMatch(clientEvent.Message?.StripColors() ?? string.Empty, word,
RegexOptions.IgnoreCase)))
{
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
{
_configHandler.Set((Configuration)new Configuration().Generate());
await _configHandler.Save();
}
containsOffensiveWord = true;
matchedFilters.Add(word);
}
public Task OnTickAsync(Server S) => Task.CompletedTask;
if (!containsOffensiveWord)
{
return Task.CompletedTask;
}
public Task OnUnloadAsync() => Task.CompletedTask;
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)
{
clientEvent.Client.Kick(_configuration.ProfanityKickMessage, sender);
}
else if (profanityInfringements < _configuration.KickAfterInfringementCount)
{
clientEvent.Client.SetAdditionalProperty(ProfanityKey, profanityInfringements + 1);
clientEvent.Client.Warn(_configuration.ProfanityWarningMessage, sender);
}
return Task.CompletedTask;
}
private Task OnClientStateInitialized(ClientStateInitializeEvent clientEvent, CancellationToken token)
{
if (_configuration?.EnableProfanityDeterment ?? false)
{
return Task.CompletedTask;
}
clientEvent.Client.SetAdditionalProperty(ProfanityKey, 0);
var offensiveWords = _configuration!.OffensiveWords;
var matchedFilters = new List<string>();
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)
{
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);
return Task.CompletedTask;
}
}

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.10.13.1" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2023.2.11.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -4,7 +4,7 @@ using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Plugins.ProfanityDeterment
{
class Configuration : IBaseConfiguration
public class ProfanityDetermentConfiguration : IBaseConfiguration
{
public List<string> OffensiveWords { get; set; }
public bool EnableProfanityDeterment { get; set; }
@ -45,6 +45,6 @@ namespace IW4MAdmin.Plugins.ProfanityDeterment
return this;
}
public string Name() => "Configuration";
public string Name() => "ProfanityDetermentSettings";
}
}