discord-bot/CompatBot/EventHandlers/UsernameZalgoMonitor.cs

223 lines
8.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-03-15 15:09:31 +00:00
using System.Globalization;
using System.Linq;
2019-03-15 15:09:31 +00:00
using System.Text;
2019-03-13 18:14:00 +00:00
using System.Threading.Tasks;
using CompatApiClient.Utils;
using CompatBot.Database;
2019-03-13 18:14:00 +00:00
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.Entities;
2019-03-13 18:14:00 +00:00
using DSharpPlus.EventArgs;
namespace CompatBot.EventHandlers;
public static class UsernameZalgoMonitor
2019-03-13 18:14:00 +00:00
{
private static readonly HashSet<char> OversizedChars =
[
'꧁', '꧂', '⎝', '⎠', '', '', '⎛', '⎞', '﷽', '⸻', 'ဪ', '꧅', '꧄', '˞',
];
2021-03-09 18:04:57 +00:00
public static async Task OnUserUpdated(DiscordClient c, UserUpdateEventArgs args)
{
try
2019-03-13 18:14:00 +00:00
{
if (await c.GetMemberAsync(args.UserAfter).ConfigureAwait(false) is DiscordMember m
&& NeedsRename(m.DisplayName))
{
var suggestedName = StripZalgo(m.DisplayName, m.Username, m.Id).Sanitize();
await c.ReportAsync("🔣 Potential display name issue",
$"""
User {m.GetMentionWithNickname()} has changed their __username__ and is now shown as **{m.DisplayName.Sanitize()}**
Automatically renamed to: **{suggestedName}**
""",
null,
ReportSeverity.Low);
await DmAndRenameUserAsync(c, m, suggestedName).ConfigureAwait(false);
}
2019-03-13 18:14:00 +00:00
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
2019-03-13 18:14:00 +00:00
public static async Task OnMemberUpdated(DiscordClient c, GuildMemberUpdateEventArgs args)
{
try
2019-03-13 18:14:00 +00:00
{
//member object most likely will not be updated in client cache at this moment
string? fallback;
if (args.NicknameAfter is string name)
fallback = args.Member.Username;
else
{
name = args.Member.Username;
fallback = null;
}
var member = await args.Guild.GetMemberAsync(args.Member.Id).ConfigureAwait(false) ?? args.Member;
if (NeedsRename(name))
{
var suggestedName = StripZalgo(name, fallback, args.Member.Id).Sanitize();
await c.ReportAsync("🔣 Potential display name issue",
$"""
Member {member.GetMentionWithNickname()} has changed their __display name__ and is now shown as **{name.Sanitize()}**
Automatically renamed to: **{suggestedName}**
""",
null,
ReportSeverity.Low);
await DmAndRenameUserAsync(c, member, suggestedName).ConfigureAwait(false);
}
2019-03-13 18:14:00 +00:00
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
2019-03-13 18:14:00 +00:00
public static async Task OnMemberAdded(DiscordClient c, GuildMemberAddEventArgs args)
{
try
2019-03-13 18:14:00 +00:00
{
var name = args.Member.DisplayName;
if (NeedsRename(name))
{
var suggestedName = StripZalgo(name, args.Member.Username, args.Member.Id).Sanitize();
await c.ReportAsync("🔣 Potential display name issue",
$"""
New member joined the server: {args.Member.GetMentionWithNickname()} and is shown as **{name.Sanitize()}**
Automatically renamed to: **{suggestedName}**
""",
null,
ReportSeverity.Low);
2021-03-09 09:03:41 +00:00
await DmAndRenameUserAsync(c, args.Member, suggestedName).ConfigureAwait(false);
}
2019-03-13 18:14:00 +00:00
}
catch (Exception e)
2019-03-15 15:09:31 +00:00
{
Config.Log.Error(e);
2019-03-15 15:09:31 +00:00
}
}
2019-03-15 15:09:31 +00:00
public static bool NeedsRename(string displayName)
{
displayName = displayName.Normalize().TrimEager();
return displayName != StripZalgo(displayName, null, 0ul, NormalizationForm.FormC, 3);
}
private static async Task DmAndRenameUserAsync(DiscordClient client, DiscordMember member, string suggestedName)
{
try
{
var renameTask = member.ModifyAsync(m => m.Nickname = suggestedName);
Config.Log.Info($"Renamed {member.Username}#{member.Discriminator} ({member.Id}) to {suggestedName}");
var rulesChannel = await client.GetChannelAsync(Config.BotRulesChannelId).ConfigureAwait(false);
var msg = $"""
Hello, your current _display name_ is breaking {rulesChannel.Mention} #7, so you have been renamed to `{suggestedName}`.
I'm not perfect and can't clean all the junk in names in some cases, so change your nickname at your discretion.
You can change your _display name_ by clicking on the server name at the top left and selecting **Change Nickname**.
""";
var dm = await member.CreateDmChannelAsync().ConfigureAwait(false);
await dm.SendMessageAsync(msg).ConfigureAwait(false);
await renameTask.ConfigureAwait(false);
}
catch (Exception e)
2019-03-13 18:14:00 +00:00
{
Config.Log.Warn(e);
}
}
public static string StripZalgo(string displayName, string? userName, ulong userId, NormalizationForm normalizationForm = NormalizationForm.FormD, int level = 0)
{
const int minNicknameLength = 2;
displayName = displayName.Normalize(normalizationForm).TrimEager();
if (displayName is null or {Length: <minNicknameLength} && userName is not null)
displayName = userName.Normalize(normalizationForm).TrimEager();
if (displayName is null or {Length: <minNicknameLength})
return GenerateRandomName(userId);
2019-03-13 18:14:00 +00:00
var builder = new StringBuilder();
bool skipLowSurrogate = false;
int consecutive = 0;
int codePoint = 0;
char highSurrogate = '\0';
bool hasNormalCharacterBefore = false;
foreach (var c in displayName)
{
switch (char.GetUnicodeCategory(c))
2019-03-13 18:14:00 +00:00
{
case UnicodeCategory.EnclosingMark:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.NonSpacingMark:
if (++consecutive < level && hasNormalCharacterBefore)
builder.Append(c);
break;
2019-03-13 18:14:00 +00:00
case UnicodeCategory.Control:
case UnicodeCategory.Format:
case UnicodeCategory.PrivateUse:
break;
2019-03-15 15:09:31 +00:00
case UnicodeCategory.Surrogate:
if (char.IsHighSurrogate(c))
{
codePoint = 0x10000 | ((c & 0x3ff) << 10);
highSurrogate = c;
}
else
{
codePoint |= c & 0x3ff;
if (codePoint is >= 0x016a0 and < 0x01700 // Runic
or >= 0x101d0 and < 0x10200 // Phaistos Disc
or >= 0x10380 and < 0x10400 // Ugaritic and Old Persian
or >= 0x12000 and < 0x13000) // Cuneiform
continue;
builder.Append(highSurrogate).Append(c);
hasNormalCharacterBefore = true;
consecutive = 0;
}
break;
case UnicodeCategory.OtherNotAssigned when c >= 0xdb40:
skipLowSurrogate = true;
break;
2019-03-13 18:14:00 +00:00
default:
if (char.IsLowSurrogate(c) && skipLowSurrogate)
skipLowSurrogate = false;
else
{
if (!OversizedChars.Contains(c))
2019-03-15 15:09:31 +00:00
{
builder.Append(c);
hasNormalCharacterBefore = true;
consecutive = 0;
2019-03-15 15:09:31 +00:00
}
}
break;
}
2019-03-13 18:14:00 +00:00
}
var result = builder.ToString().TrimEager();
if (result is null or {Length: <minNicknameLength})
2021-03-09 11:46:31 +00:00
{
if (userName is null)
return GenerateRandomName(userId);
return StripZalgo(userName, null, userId, normalizationForm, level);
2021-03-09 11:46:31 +00:00
}
return result;
}
public static string GenerateRandomName(ulong userId)
{
var hash = userId.GetHashCode();
var rng = new Random(hash);
using var db = new ThumbnailDb();
var count = db.NamePool.Count();
var name = db.NamePool.Skip(rng.Next(count)).First().Name;
return name + Config.RenameNameSuffix;
2019-03-13 18:14:00 +00:00
}
}