@discord-chat-exporter/core
v0.1.3
Published
TypeScript SDK for exporting Discord chat history
Maintainers
Readme
@discord-chat-exporter/core
TypeScript SDK for exporting Discord chat history to various formats.
Installation
npm install @discord-chat-exporter/coreOr with other package managers:
# pnpm
pnpm add @discord-chat-exporter/core
# yarn
yarn add @discord-chat-exporter/coreRequirements
- Node.js >= 20.0.0
- Discord authentication token (user or bot)
Quick Start
import {
DiscordClient,
ChannelExporter,
ExportRequest,
ExportFormat,
Snowflake,
} from '@discord-chat-exporter/core';
// Initialize the client
const client = new DiscordClient('YOUR_DISCORD_TOKEN');
// Get channel and guild info
const channelId = Snowflake.parse('123456789012345678');
const channel = await client.getChannel(channelId);
const guild = await client.getGuild(channel.guildId);
// Create export request
const request = new ExportRequest({
guild,
channel,
outputPath: './export.html',
format: ExportFormat.HtmlDark,
});
// Export with progress callback
const exporter = new ChannelExporter(client);
await exporter.exportChannel(request, (progress) => {
console.log(`Progress: ${progress.toFixed(1)}%`);
});Export Formats
| Format | Enum Value | Extension | Best For |
|--------|------------|-----------|----------|
| HTML Dark | ExportFormat.HtmlDark | .html | Human reading (dark theme) |
| HTML Light | ExportFormat.HtmlLight | .html | Human reading (light theme) |
| JSON | ExportFormat.Json | .json | Data processing, archival |
| CSV | ExportFormat.Csv | .csv | Spreadsheet analysis |
| Plain Text | ExportFormat.PlainText | .txt | Simple text archives |
API Reference
DiscordClient
HTTP client for Discord API with rate limiting and retry logic.
const client = new DiscordClient(token);
// List guilds
for await (const guild of client.getUserGuilds()) {
console.log(guild.name);
}
// List channels in a guild
for await (const channel of client.getGuildChannels(guildId)) {
console.log(channel.name);
}
// Get direct message channels
for await (const dm of client.getDirectMessageChannels()) {
console.log(dm.name);
}ExportRequest Options
interface ExportRequestOptions {
guild: Guild; // Required: Guild containing the channel
channel: Channel; // Required: Channel to export
outputPath: string; // Required: Output file path or directory
format: ExportFormat; // Required: Export format
// Optional filters
after?: Snowflake | null; // Only messages after this ID/date
before?: Snowflake | null; // Only messages before this ID/date
messageFilter?: MessageFilter; // Custom message filter
// Optional features
partitionLimit?: PartitionLimit; // Split output by count or size
shouldDownloadAssets?: boolean; // Download media attachments
shouldReuseAssets?: boolean; // Reuse previously downloaded media
assetsDirPath?: string; // Directory for downloaded media
// Formatting options
shouldFormatMarkdown?: boolean; // Enable markdown processing (default: true)
locale?: string; // Locale for date/number formatting
isUtcNormalizationEnabled?: boolean; // Normalize timestamps to UTC
}Message Filtering
import { MessageFilter } from '@discord-chat-exporter/core';
// Parse filter expression
const filter = MessageFilter.parse('from:username has:attachment');
// Use in export request
const request = new ExportRequest({
// ...
messageFilter: filter,
});Filter syntax:
from:username- Messages from specific userhas:attachment- Messages with attachmentshas:embed- Messages with embedshas:image- Messages with imagescontains:text- Messages containing textis:pinned- Pinned messages- Combine with
and,or,-(not)
Partition Limits
import { PartitionLimit } from '@discord-chat-exporter/core';
// Split by message count
const countLimit = PartitionLimit.parse('1000');
// Split by file size
const sizeLimit = PartitionLimit.parse('10mb');
const request = new ExportRequest({
// ...
partitionLimit: sizeLimit,
});Date Boundaries
import { Snowflake } from '@discord-chat-exporter/core';
// From specific date
const after = Snowflake.fromDate(new Date('2024-01-01'));
// From message ID
const afterId = Snowflake.parse('123456789012345678');
const request = new ExportRequest({
// ...
after,
before: Snowflake.fromDate(new Date('2024-12-31')),
});Exported Types
Core Classes
DiscordClient- API clientChannelExporter- Export orchestratorExportRequest- Export configurationSnowflake- Discord ID wrapper
Data Models
Guild,Channel,Message,User,MemberRole,Embed,Attachment,Reaction,Sticker
Enums
ExportFormat- Output formatsChannelKind- Channel typesMessageKind- Message types
Utilities
MessageFilter- Filter expressionsPartitionLimit- Output splittingFileSize- Byte size handlingColor- RGB color manipulation
Exceptions
DiscordChatExporterError- Base errorChannelEmptyError- Empty channel (non-fatal)UnsupportedChannelError- Unsupported channel type (fatal)
Authentication
User Token
- Open Discord in browser
- Press
F12for Developer Tools - Go to Console tab
- Run:
(webpackChunkdiscord_app.push([[''],{},e=>{m=[];for(let c in e.c)m.push(e.c[c])}]),m).find(m=>m?.exports?.default?.getToken!==void 0).exports.default.getToken()
Bot Token
- Create application at Discord Developer Portal
- Enable "Message Content Intent" in Bot settings
- Use the token from Bot settings
Related Packages
- @discord-chat-exporter/cli - Command-line interface
License
MIT
