-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathUsernameZalgoMonitor.cs
217 lines (201 loc) · 8.98 KB
/
UsernameZalgoMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Globalization;
using CompatApiClient.Utils;
using CompatBot.Database;
namespace CompatBot.EventHandlers;
public static class UsernameZalgoMonitor
{
private static readonly HashSet<char> OversizedChars =
[
'꧁', '꧂', '⎝', '⎠', '⧹', '⧸', '⎛', '⎞', '﷽', '⸻', 'ဪ', '꧅', '꧄', '˞',
];
public static async Task OnUserUpdated(DiscordClient c, UserUpdatedEventArgs args)
{
try
{
if (await c.GetMemberAsync(args.UserAfter).ConfigureAwait(false) is DiscordMember m
&& await NeedsRenameAsync(m.DisplayName).ConfigureAwait(false))
{
var suggestedName = await StripZalgoAsync(m.DisplayName, m.Username, m.Id).ConfigureAwait(false);
suggestedName = suggestedName.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);
}
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
public static async Task OnMemberUpdated(DiscordClient c, GuildMemberUpdatedEventArgs args)
{
try
{
//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 (await NeedsRenameAsync(name).ConfigureAwait(false))
{
var suggestedName = await StripZalgoAsync(name, fallback, args.Member.Id).ConfigureAwait(false);
suggestedName = suggestedName.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);
}
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
public static async Task OnMemberAdded(DiscordClient c, GuildMemberAddedEventArgs args)
{
try
{
var name = args.Member.DisplayName;
if (await NeedsRenameAsync(name).ConfigureAwait(false))
{
var suggestedName = await StripZalgoAsync(name, args.Member.Username, args.Member.Id).ConfigureAwait(false);
suggestedName = suggestedName.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);
await DmAndRenameUserAsync(c, args.Member, suggestedName).ConfigureAwait(false);
}
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
public static async ValueTask<bool> NeedsRenameAsync(string displayName)
{
displayName = displayName.Normalize().TrimEager();
return displayName != await StripZalgoAsync(displayName, null, 0ul, NormalizationForm.FormC, 3).ConfigureAwait(false);
}
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)
{
Config.Log.Warn(e);
}
}
public static async ValueTask<string> StripZalgoAsync(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 await GenerateRandomNameAsync(userId).ConfigureAwait(false);
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))
{
case UnicodeCategory.EnclosingMark:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.NonSpacingMark:
if (++consecutive < level && hasNormalCharacterBefore)
builder.Append(c);
break;
case UnicodeCategory.Control:
case UnicodeCategory.Format:
case UnicodeCategory.PrivateUse:
break;
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;
default:
if (char.IsLowSurrogate(c) && skipLowSurrogate)
skipLowSurrogate = false;
else
{
if (!OversizedChars.Contains(c))
{
builder.Append(c);
hasNormalCharacterBefore = true;
consecutive = 0;
}
}
break;
}
}
var result = builder.ToString().TrimEager();
if (result is null or {Length: <minNicknameLength})
{
if (userName is null)
return await GenerateRandomNameAsync(userId).ConfigureAwait(false);
return await StripZalgoAsync(userName, null, userId, normalizationForm, level).ConfigureAwait(false);
}
return result;
}
public static async ValueTask<string> GenerateRandomNameAsync(ulong userId)
{
var hash = userId.GetHashCode();
var rng = new Random(hash);
await using var db = await ThumbnailDb.OpenReadAsync().ConfigureAwait(false);
var count = db.NamePool.Count();
var name = db.NamePool.Skip(rng.Next(count)).First().Name;
return name + Config.RenameNameSuffix;
}
}