Dont fail cleanmentions if a message contains a very large mention id

This commit is contained in:
RogueException
2015-12-27 15:47:10 -04:00
parent 1f17de5ae6
commit dc4ca83a0d
2 changed files with 24 additions and 20 deletions

View File

@@ -66,7 +66,7 @@ namespace Discord.API.Converters
reader.Read(); reader.Read();
while (reader.TokenType != JsonToken.EndArray) while (reader.TokenType != JsonToken.EndArray)
{ {
result.Add(((string)reader.Value).ToId()); result.Add(((string)reader.Value).TryToId());
reader.Read(); reader.Read();
} }
} }

View File

@@ -22,8 +22,8 @@ namespace Discord
public sealed class Message public sealed class Message
{ {
private static readonly Regex _userRegex = new Regex(@"<@([0-9]+)>", RegexOptions.Compiled); private static readonly Regex _userRegex = new Regex(@"<@[0-9]+>", RegexOptions.Compiled);
private static readonly Regex _channelRegex = new Regex(@"<#([0-9]+)>", RegexOptions.Compiled); private static readonly Regex _channelRegex = new Regex(@"<#[0-9]+>", RegexOptions.Compiled);
private static readonly Regex _roleRegex = new Regex(@"@everyone", RegexOptions.Compiled); private static readonly Regex _roleRegex = new Regex(@"@everyone", RegexOptions.Compiled);
private static readonly Attachment[] _initialAttachments = new Attachment[0]; private static readonly Attachment[] _initialAttachments = new Attachment[0];
private static readonly Embed[] _initialEmbeds = new Embed[0]; private static readonly Embed[] _initialEmbeds = new Embed[0];
@@ -32,7 +32,9 @@ namespace Discord
{ {
return _userRegex.Replace(text, new MatchEvaluator(e => return _userRegex.Replace(text, new MatchEvaluator(e =>
{ {
var id = e.Value.Substring(2, e.Value.Length - 3).ToId(); ulong id;
if (e.Value.Substring(2, e.Value.Length - 3).TryToId(out id))
{
var user = channel.GetUser(id); var user = channel.GetUser(id);
if (user != null) if (user != null)
{ {
@@ -40,8 +42,8 @@ namespace Discord
users.Add(user); users.Add(user);
return '@' + user.Name; return '@' + user.Name;
} }
else //User not found }
return '@' + e.Value; return e.Value; //User not found or parse failed
})); }));
} }
internal static string CleanChannelMentions(Channel channel, string text, List<Channel> channels = null) internal static string CleanChannelMentions(Channel channel, string text, List<Channel> channels = null)
@@ -51,7 +53,9 @@ namespace Discord
return _channelRegex.Replace(text, new MatchEvaluator(e => return _channelRegex.Replace(text, new MatchEvaluator(e =>
{ {
var id = e.Value.Substring(2, e.Value.Length - 3).ToId(); ulong id;
if (e.Value.Substring(2, e.Value.Length - 3).TryToId(out id))
{
var mentionedChannel = server.GetChannel(id); var mentionedChannel = server.GetChannel(id);
if (mentionedChannel != null && mentionedChannel.Server.Id == server.Id) if (mentionedChannel != null && mentionedChannel.Server.Id == server.Id)
{ {
@@ -59,8 +63,8 @@ namespace Discord
channels.Add(mentionedChannel); channels.Add(mentionedChannel);
return '#' + mentionedChannel.Name; return '#' + mentionedChannel.Name;
} }
else //Channel not found }
return '#' + e.Value; return e.Value; //Channel not found or parse failed
})); }));
} }
/*internal static string CleanRoleMentions(User user, Channel channel, string text, List<Role> roles = null) /*internal static string CleanRoleMentions(User user, Channel channel, string text, List<Role> roles = null)