From 06a4b6a8e6f6efd0d5efc4b99c86e09600b38bc0 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Fri, 16 Sep 2022 23:04:18 +0300 Subject: [PATCH] Add checks for message content intent on bot accounts Related to #918 --- DiscordChatExporter.Core/Discord/Data/Message.cs | 12 +++++++++++- DiscordChatExporter.Core/Discord/DiscordClient.cs | 5 +++++ .../Exceptions/DiscordChatExporterException.cs | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/DiscordChatExporter.Core/Discord/Data/Message.cs b/DiscordChatExporter.Core/Discord/Data/Message.cs index cdda0aba..71a58283 100644 --- a/DiscordChatExporter.Core/Discord/Data/Message.cs +++ b/DiscordChatExporter.Core/Discord/Data/Message.cs @@ -10,7 +10,7 @@ using JsonExtensions.Reading; namespace DiscordChatExporter.Core.Discord.Data; // https://discord.com/developers/docs/resources/channel#message-object -public record Message( +public partial record Message( Snowflake Id, MessageKind Kind, User Author, @@ -26,6 +26,16 @@ public record Message( IReadOnlyList MentionedUsers, MessageReference? Reference, Message? ReferencedMessage) : IHasId +{ + public bool IsEmpty => + Kind == MessageKind.Default && + string.IsNullOrEmpty(Content) && + !Attachments.Any() && + !Embeds.Any() && + !Stickers.Any(); +} + +public partial record Message { private static IReadOnlyList NormalizeEmbeds(IReadOnlyList embeds) { diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 9ec5b112..5c732841 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -323,6 +323,11 @@ public class DiscordClient if (message.Timestamp > lastMessage.Timestamp) yield break; + // Make sure the messages are not empty when exporting via a bot + // https://github.com/Tyrrrz/DiscordChatExporter/issues/918 + if (_resolvedTokenKind == TokenKind.Bot && message.IsEmpty && !message.Author.IsBot) + throw DiscordChatExporterException.MessageContentIntentMissing(); + // Report progress based on the duration of exported messages divided by total if (progress is not null) { diff --git a/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs b/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs index 7543c5f4..97ec6d88 100644 --- a/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs +++ b/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs @@ -41,4 +41,7 @@ Failed to perform an HTTP request. internal static DiscordChatExporterException ChannelIsEmpty() => new("No messages found for the specified period."); + + internal static DiscordChatExporterException MessageContentIntentMissing() => + new("Failed to retrieve message content because the bot doesn't have the Message Content Intent enabled."); } \ No newline at end of file