Add checks for message content intent on bot accounts

Related to #918
This commit is contained in:
Tyrrrz 2022-09-16 23:04:18 +03:00
parent cc1ad8b435
commit 06a4b6a8e6
3 changed files with 19 additions and 1 deletions

View file

@ -10,7 +10,7 @@ using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data; namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/channel#message-object // https://discord.com/developers/docs/resources/channel#message-object
public record Message( public partial record Message(
Snowflake Id, Snowflake Id,
MessageKind Kind, MessageKind Kind,
User Author, User Author,
@ -26,6 +26,16 @@ public record Message(
IReadOnlyList<User> MentionedUsers, IReadOnlyList<User> MentionedUsers,
MessageReference? Reference, MessageReference? Reference,
Message? ReferencedMessage) : IHasId 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<Embed> NormalizeEmbeds(IReadOnlyList<Embed> embeds) private static IReadOnlyList<Embed> NormalizeEmbeds(IReadOnlyList<Embed> embeds)
{ {

View file

@ -323,6 +323,11 @@ public class DiscordClient
if (message.Timestamp > lastMessage.Timestamp) if (message.Timestamp > lastMessage.Timestamp)
yield break; 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 // Report progress based on the duration of exported messages divided by total
if (progress is not null) if (progress is not null)
{ {

View file

@ -41,4 +41,7 @@ Failed to perform an HTTP request.
internal static DiscordChatExporterException ChannelIsEmpty() => internal static DiscordChatExporterException ChannelIsEmpty() =>
new("No messages found for the specified period."); 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.");
} }