fix(proxy): correctly apply Discord webhook name restrictions
Closes #484.
This commit is contained in:
parent
494bd8aab3
commit
a441c00d94
@ -115,7 +115,7 @@ public class WebhookExecutorService
|
|||||||
|
|
||||||
var webhookReq = new ExecuteWebhookRequest
|
var webhookReq = new ExecuteWebhookRequest
|
||||||
{
|
{
|
||||||
Username = FixProxyName(req.Name).Truncate(80),
|
Username = req.Name.FixProxyName().Truncate(80),
|
||||||
Content = content,
|
Content = content,
|
||||||
AllowedMentions = allowedMentions,
|
AllowedMentions = allowedMentions,
|
||||||
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
||||||
@ -243,21 +243,46 @@ public class WebhookExecutorService
|
|||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string FixProxyName(string name) => FixSingleCharacterName(FixClyde(name));
|
}
|
||||||
|
|
||||||
private string FixClyde(string name)
|
public static class ProxyNameExt
|
||||||
{
|
{
|
||||||
static string Replacement(Match m) => m.Groups[1].Value + "\u200A" + m.Groups[2].Value;
|
public static string FixProxyName(this string name) => name
|
||||||
|
.FixClyde()
|
||||||
|
.FixHere()
|
||||||
|
.FixEveryone()
|
||||||
|
.FixBackticks()
|
||||||
|
.FixSingleCharacterName()
|
||||||
|
.ThrowOnInvalidCharacters();
|
||||||
|
|
||||||
|
static string ThrowOnInvalidCharacters(this string name)
|
||||||
|
{
|
||||||
|
var invalidCharacters = new[] { "@", "#", ":" };
|
||||||
|
if (invalidCharacters.Any(x => name.Contains(x)))
|
||||||
|
throw new PKError("Due to Discord limitations, proxy names cannot contain the characters `@`, `#` or `:`. "
|
||||||
|
+ $"The webhook's name, {name.AsCode()}, contains one or more of these characters.");
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static string FixHere(this string name)
|
||||||
|
=> Regex.Replace(name, "(h)(ere)", Replacement, RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
static string FixEveryone(this string name)
|
||||||
|
=> Regex.Replace(name, "(e)(veryone)", Replacement, RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
static string FixBackticks(this string name)
|
||||||
|
=> Regex.Replace(name, "(e)(veryone)", Replacement, RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
// Adds a Unicode hair space (\u200A) between the "c" and the "lyde" to avoid Discord matching it
|
// Adds a Unicode hair space (\u200A) between the "c" and the "lyde" to avoid Discord matching it
|
||||||
// since Discord blocks webhooks containing the word "Clyde"... for some reason. /shrug
|
// since Discord blocks webhooks containing the word "Clyde"... for some reason. /shrug
|
||||||
return Regex.Replace(name, "(c)(lyde)", Replacement, RegexOptions.IgnoreCase);
|
static string FixClyde(this string name)
|
||||||
}
|
=> Regex.Replace(name, "(c)(lyde)", Replacement, RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
private string FixSingleCharacterName(string proxyName)
|
static string FixSingleCharacterName(this string proxyName)
|
||||||
{
|
{
|
||||||
if (proxyName.Length == 1)
|
if (proxyName.Length == 1)
|
||||||
return proxyName + "\u17b5";
|
return proxyName + "\u17b5";
|
||||||
return proxyName;
|
return proxyName;
|
||||||
}
|
}
|
||||||
|
static string Replacement(Match m) => m.Groups[1].Value + "\u200A" + m.Groups[2].Value;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user