2020-02-12 14:16:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
|
using DSharpPlus.Entities;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
|
{
|
|
|
|
|
public static class StringUtils
|
|
|
|
|
{
|
2020-04-17 21:10:01 +00:00
|
|
|
|
public static DiscordColor? ToDiscordColor(this string color)
|
2020-02-12 14:16:19 +00:00
|
|
|
|
{
|
2020-04-17 21:10:01 +00:00
|
|
|
|
if (int.TryParse(color, NumberStyles.HexNumber, null, out var colorInt))
|
|
|
|
|
return new DiscordColor(colorInt);
|
2020-02-12 14:16:19 +00:00
|
|
|
|
throw new ArgumentException($"Invalid color string '{color}'.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool HasMentionPrefix(string content, ref int argPos, out ulong mentionId)
|
|
|
|
|
{
|
|
|
|
|
mentionId = 0;
|
|
|
|
|
|
|
|
|
|
// Roughly ported from Discord.Commands.MessageExtensions.HasMentionPrefix
|
|
|
|
|
if (string.IsNullOrEmpty(content) || content.Length <= 3 || (content[0] != '<' || content[1] != '@'))
|
|
|
|
|
return false;
|
|
|
|
|
int num = content.IndexOf('>');
|
2020-04-17 21:10:01 +00:00
|
|
|
|
if (num == -1 || content.Length < num + 2 || content[num + 1] != ' ' || !TryParseMention(content.Substring(0, num + 1), out mentionId))
|
2020-02-12 14:16:19 +00:00
|
|
|
|
return false;
|
|
|
|
|
argPos = num + 2;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool TryParseMention(this string potentialMention, out ulong id)
|
|
|
|
|
{
|
|
|
|
|
if (ulong.TryParse(potentialMention, out id)) return true;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
|
|
|
|
|
// Roughly ported from Discord.MentionUtils.TryParseUser
|
|
|
|
|
if (potentialMention.Length >= 3 && potentialMention[0] == '<' && potentialMention[1] == '@' && potentialMention[potentialMention.Length - 1] == '>')
|
|
|
|
|
{
|
|
|
|
|
if (potentialMention.Length >= 4 && potentialMention[2] == '!')
|
|
|
|
|
potentialMention = potentialMention.Substring(3, potentialMention.Length - 4); //<@!123>
|
|
|
|
|
else
|
|
|
|
|
potentialMention = potentialMention.Substring(2, potentialMention.Length - 3); //<@123>
|
|
|
|
|
|
|
|
|
|
if (ulong.TryParse(potentialMention, NumberStyles.None, CultureInfo.InvariantCulture, out id))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-12 14:16:19 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string SanitizeMentions(this string input) =>
|
|
|
|
|
Regex.Replace(Regex.Replace(input, "<@[!&]?(\\d{17,19})>", "<\u200B@$1>"), "@(everyone|here)", "@\u200B$1");
|
|
|
|
|
|
|
|
|
|
public static string SanitizeEveryone(this string input) =>
|
|
|
|
|
Regex.Replace(input, "@(everyone|here)", "@\u200B$1");
|
|
|
|
|
|
|
|
|
|
public static string EscapeMarkdown(this string input)
|
|
|
|
|
{
|
|
|
|
|
Regex pattern = new Regex(@"[*_~>`(||)\\]", RegexOptions.Multiline);
|
|
|
|
|
if (input != null) return pattern.Replace(input, @"\$&");
|
|
|
|
|
else return input;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|