Fix crash on encountering invalid snowflake (#479)

pull/490/head
Lucas LaBuff 4 years ago committed by GitHub
parent 0872d6d44b
commit 915f4c8d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Discord.Models; using DiscordChatExporter.Domain.Discord.Models;
using DiscordChatExporter.Domain.Markdown; using DiscordChatExporter.Domain.Markdown;
using DiscordChatExporter.Domain.Markdown.Ast; using DiscordChatExporter.Domain.Markdown.Ast;
using DiscordChatExporter.Domain.Utilities;
namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
{ {
@ -76,6 +77,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
protected override MarkdownNode VisitMention(MentionNode mention) protected override MarkdownNode VisitMention(MentionNode mention)
{ {
var mentionId = Snowflake.TryParse(mention.Id);
if (mention.Type == MentionType.Meta) if (mention.Type == MentionType.Meta)
{ {
_buffer _buffer
@ -85,7 +87,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
} }
else if (mention.Type == MentionType.User) else if (mention.Type == MentionType.User)
{ {
var member = _context.TryGetMember(Snowflake.Parse(mention.Id)); var member = mentionId?.Pipe(_context.TryGetMember);
var fullName = member?.User.FullName ?? "Unknown"; var fullName = member?.User.FullName ?? "Unknown";
var nick = member?.Nick ?? "Unknown"; var nick = member?.Nick ?? "Unknown";
@ -96,7 +98,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
} }
else if (mention.Type == MentionType.Channel) else if (mention.Type == MentionType.Channel)
{ {
var channel = _context.TryGetChannel(Snowflake.Parse(mention.Id)); var channel = mentionId?.Pipe(_context.TryGetChannel);
var name = channel?.Name ?? "deleted-channel"; var name = channel?.Name ?? "deleted-channel";
_buffer _buffer
@ -106,7 +108,7 @@ namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
} }
else if (mention.Type == MentionType.Role) else if (mention.Type == MentionType.Role)
{ {
var role = _context.TryGetRole(Snowflake.Parse(mention.Id)); var role = mentionId?.Pipe(_context.TryGetRole);
var name = role?.Name ?? "deleted-role"; var name = role?.Name ?? "deleted-role";
var color = role?.Color; var color = role?.Color;

@ -2,6 +2,7 @@
using DiscordChatExporter.Domain.Discord; using DiscordChatExporter.Domain.Discord;
using DiscordChatExporter.Domain.Markdown; using DiscordChatExporter.Domain.Markdown;
using DiscordChatExporter.Domain.Markdown.Ast; using DiscordChatExporter.Domain.Markdown.Ast;
using DiscordChatExporter.Domain.Utilities;
namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
{ {
@ -24,27 +25,28 @@ namespace DiscordChatExporter.Domain.Exporting.Writers.MarkdownVisitors
protected override MarkdownNode VisitMention(MentionNode mention) protected override MarkdownNode VisitMention(MentionNode mention)
{ {
var mentionId = Snowflake.TryParse(mention.Id);
if (mention.Type == MentionType.Meta) if (mention.Type == MentionType.Meta)
{ {
_buffer.Append($"@{mention.Id}"); _buffer.Append($"@{mention.Id}");
} }
else if (mention.Type == MentionType.User) else if (mention.Type == MentionType.User)
{ {
var member = _context.TryGetMember(Snowflake.Parse(mention.Id)); var member = mentionId?.Pipe(_context.TryGetMember);
var name = member?.User.Name ?? "Unknown"; var name = member?.User.Name ?? "Unknown";
_buffer.Append($"@{name}"); _buffer.Append($"@{name}");
} }
else if (mention.Type == MentionType.Channel) else if (mention.Type == MentionType.Channel)
{ {
var channel = _context.TryGetChannel(Snowflake.Parse(mention.Id)); var channel = mentionId?.Pipe(_context.TryGetChannel);
var name = channel?.Name ?? "deleted-channel"; var name = channel?.Name ?? "deleted-channel";
_buffer.Append($"#{name}"); _buffer.Append($"#{name}");
} }
else if (mention.Type == MentionType.Role) else if (mention.Type == MentionType.Role)
{ {
var role = _context.TryGetRole(Snowflake.Parse(mention.Id)); var role = mentionId?.Pipe(_context.TryGetRole);
var name = role?.Name ?? "deleted-role"; var name = role?.Name ?? "deleted-role";
_buffer.Append($"@{name}"); _buffer.Append($"@{name}");

Loading…
Cancel
Save