File
The File
component allows you to display file attachments in your Discord messages. This is useful for sharing text files, markdown files, or other file types.
Basic Usage
src/app/commands/file-example.tsx
import {
type ChatInputCommand,
Container,
File,
TextDisplay,
} from 'commandkit';
import { AttachmentBuilder, Colors, MessageFlags } from 'discord.js';
export const chatInput: ChatInputCommand = async (ctx) => {
const fileContent = '# Hello World\nThis is a test file.';
const container = (
<Container accentColor={Colors.Blue}>
<TextDisplay content="Here's a file:" />
<File url="attachment://example.md" />
</Container>
);
await ctx.interaction.reply({
components: [container],
files: [
new AttachmentBuilder(Buffer.from(fileContent), {
name: 'example.md',
}),
],
flags: MessageFlags.IsComponentsV2,
});
};
Multiple Files
You can display multiple files in a single message:
src/app/commands/multiple-files.tsx
import {
type ChatInputCommand,
Container,
File,
TextDisplay,
} from 'commandkit';
import { AttachmentBuilder, Colors, MessageFlags } from 'discord.js';
export const chatInput: ChatInputCommand = async (ctx) => {
const files = [
{
name: 'readme.md',
content: '# Readme\nThis is a readme file.',
},
{
name: 'config.json',
content: JSON.stringify({ setting: 'value' }, null, 2),
},
];
const container = (
<Container accentColor={Colors.Green}>
<TextDisplay content="# Project Files" />
{files.map((file) => (
<File url={`attachment://${file.name}`} />
))}
</Container>
);
await ctx.interaction.reply({
components: [container],
files: files.map(
(file) =>
new AttachmentBuilder(Buffer.from(file.content), {
name: file.name,
}),
),
flags: MessageFlags.IsComponentsV2,
});
};
Best Practices
- File Names: Use clear, descriptive file names
- File Types: Ensure the file type matches the content
- File Size: Keep files small to avoid Discord's size limits
- Organization: Group related files together
- Context: Provide context about the files using TextDisplay components
Available Props
File
url
: The URL of the file attachment (must start with "attachment://")
Common Use Cases
- Documentation: Share markdown files with documentation
- Configuration: Display configuration files
- Code Snippets: Share code files with syntax highlighting
- Logs: Show log files or error reports