| @@ -19,6 +19,14 @@ namespace Discord | |||||
| /// <summary> Parses a provided user mention string. </summary> | /// <summary> Parses a provided user mention string. </summary> | ||||
| public static ulong ParseUser(string mentionText) | public static ulong ParseUser(string mentionText) | ||||
| { | |||||
| ulong id; | |||||
| if (TryParseUser(mentionText, out id)) | |||||
| return id; | |||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| } | |||||
| /// <summary> Tries to parse a provided user mention string. </summary> | |||||
| public static bool TryParseUser(string mentionText, out ulong userId) | |||||
| { | { | ||||
| mentionText = mentionText.Trim(); | mentionText = mentionText.Trim(); | ||||
| if (mentionText.Length >= 3 && mentionText[0] == '<' && mentionText[1] == '@' && mentionText[mentionText.Length - 1] == '>') | if (mentionText.Length >= 3 && mentionText[0] == '<' && mentionText[1] == '@' && mentionText[mentionText.Length - 1] == '>') | ||||
| @@ -27,40 +35,57 @@ namespace Discord | |||||
| mentionText = mentionText.Substring(3, mentionText.Length - 4); //<@!123> | mentionText = mentionText.Substring(3, mentionText.Length - 4); //<@!123> | ||||
| else | else | ||||
| mentionText = mentionText.Substring(2, mentionText.Length - 3); //<@123> | mentionText = mentionText.Substring(2, mentionText.Length - 3); //<@123> | ||||
| ulong id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||||
| return id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out userId)) | |||||
| return true; | |||||
| } | } | ||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| userId = 0; | |||||
| return false; | |||||
| } | } | ||||
| /// <summary> Parses a provided channel mention string. </summary> | /// <summary> Parses a provided channel mention string. </summary> | ||||
| public static ulong ParseChannel(string mentionText) | public static ulong ParseChannel(string mentionText) | ||||
| { | |||||
| ulong id; | |||||
| if (TryParseChannel(mentionText, out id)) | |||||
| return id; | |||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| } | |||||
| /// <summary>Tries to parse a provided channel mention string. </summary> | |||||
| public static bool TryParseChannel(string mentionText, out ulong channelId) | |||||
| { | { | ||||
| mentionText = mentionText.Trim(); | mentionText = mentionText.Trim(); | ||||
| if (mentionText.Length >= 3 && mentionText[0] == '<' && mentionText[1] == '#' && mentionText[mentionText.Length - 1] == '>') | if (mentionText.Length >= 3 && mentionText[0] == '<' && mentionText[1] == '#' && mentionText[mentionText.Length - 1] == '>') | ||||
| { | { | ||||
| mentionText = mentionText.Substring(2, mentionText.Length - 3); //<#123> | mentionText = mentionText.Substring(2, mentionText.Length - 3); //<#123> | ||||
| ulong id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||||
| return id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out channelId)) | |||||
| return true; | |||||
| } | } | ||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| channelId = 0; | |||||
| return false; | |||||
| } | } | ||||
| /// <summary> Parses a provided role mention string. </summary> | /// <summary> Parses a provided role mention string. </summary> | ||||
| public static ulong ParseRole(string mentionText) | public static ulong ParseRole(string mentionText) | ||||
| { | |||||
| ulong id; | |||||
| if (TryParseRole(mentionText, out id)) | |||||
| return id; | |||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| } | |||||
| /// <summary>Tries to parse a provided role mention string. </summary> | |||||
| public static bool TryParseRole(string mentionText, out ulong roleId) | |||||
| { | { | ||||
| mentionText = mentionText.Trim(); | mentionText = mentionText.Trim(); | ||||
| if (mentionText.Length >= 4 && mentionText[0] == '<' && mentionText[1] == '@' && mentionText[2] == '&' && mentionText[mentionText.Length - 1] == '>') | if (mentionText.Length >= 4 && mentionText[0] == '<' && mentionText[1] == '@' && mentionText[2] == '&' && mentionText[mentionText.Length - 1] == '>') | ||||
| { | { | ||||
| mentionText = mentionText.Substring(3, mentionText.Length - 4); //<@&123> | mentionText = mentionText.Substring(3, mentionText.Length - 4); //<@&123> | ||||
| ulong id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||||
| return id; | |||||
| if (ulong.TryParse(mentionText, NumberStyles.None, CultureInfo.InvariantCulture, out roleId)) | |||||
| return true; | |||||
| } | } | ||||
| throw new ArgumentException("Invalid mention format", nameof(mentionText)); | |||||
| roleId = 0; | |||||
| return false; | |||||
| } | } | ||||
| /// <summary> Gets the ids of all users mentioned in a provided text.</summary> | /// <summary> Gets the ids of all users mentioned in a provided text.</summary> | ||||