strip the whole cuneiform block from nicknames

This commit is contained in:
13xforever
2021-05-01 22:54:07 +05:00
parent cc15ace406
commit b6dd4bfd29
2 changed files with 19 additions and 7 deletions

View File

@@ -20,11 +20,6 @@ namespace CompatBot.EventHandlers
'꧁', '꧂', '⎝', '⎠', '', '', '⎛', '⎞', '﷽', '⸻', 'ဪ', '꧅', '꧄',
};
private static readonly List<string> OversizedLiterals = new()
{
"𒐫", "𒈙",
};
public static async Task OnUserUpdated(DiscordClient c, UserUpdateEventArgs args)
{
try
@@ -119,14 +114,14 @@ namespace CompatBot.EventHandlers
public static string StripZalgo(string displayName, ulong userId, NormalizationForm normalizationForm = NormalizationForm.FormD, int level = 0)
{
displayName = displayName.Normalize(normalizationForm).TrimEager();
foreach (var literal in OversizedLiterals)
displayName = displayName.Replace(literal, "");
if (string.IsNullOrEmpty(displayName))
return GenerateRandomName(userId);
var builder = new StringBuilder();
bool skipLowSurrogate = false;
int consecutive = 0;
int codePoint = 0;
char highSurrogate = '\0';
foreach (var c in displayName)
{
switch (char.GetUnicodeCategory(c))
@@ -141,6 +136,22 @@ namespace CompatBot.EventHandlers
case UnicodeCategory.Format:
break;
case UnicodeCategory.Surrogate:
if (char.IsHighSurrogate(c))
{
codePoint = 0x10000 | ((c & 0x3ff) << 10);
highSurrogate = c;
}
else
{
codePoint |= c & 0x3ff;
if (codePoint is >= 0x12000 and < 0x13000) //Cuneiform
continue;
builder.Append(highSurrogate).Append(c);
}
break;
case UnicodeCategory.OtherNotAssigned when c >= 0xdb40:
skipLowSurrogate = true;
break;

View File

@@ -77,6 +77,7 @@ namespace Tests
[TestCase("꧁꧂🥴🥴🥴HOJU🥴🥴🥴╲⎝", true)]
[TestCase("", true)]
[TestCase("", true, "Reserved block")]
[TestCase("𒁃𒃋𒋬𒑭𒕃", true, "Cuneiform block")]
public void ZalgoDetectionTest(string name, bool isBad, string comment = null)
{
Assert.That(UsernameZalgoMonitor.NeedsRename(name), Is.EqualTo(isBad), comment);