Skip to content

Commit f034511

Browse files
committed
fix: components children issue
1 parent e563176 commit f034511

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
ChatInputCommand,
3+
CommandData,
4+
Container,
5+
TextDisplay,
6+
} from 'commandkit';
7+
import { Colors, MessageFlags } from 'discord.js';
8+
9+
export const command: CommandData = {
10+
name: 'components-test',
11+
description: 'Test components v2 again',
12+
};
13+
14+
export const chatInput: ChatInputCommand = async (ctx) => {
15+
const container = (
16+
<Container accentColor={Colors.Fuchsia}>
17+
<TextDisplay content="# CommandKit Components v2 test" />
18+
</Container>
19+
);
20+
21+
await ctx.interaction.reply({
22+
components: [container],
23+
24+
flags: MessageFlags.IsComponentsV2,
25+
});
26+
};

packages/commandkit/src/components/v2/container.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
SectionBuilder,
99
SeparatorBuilder,
1010
TextDisplayBuilder,
11-
ThumbnailBuilder,
1211
} from 'discord.js';
1312
import { applyId } from './common';
1413

@@ -30,7 +29,10 @@ export function Container(props: ContainerProps): ContainerBuilder {
3029
container.setSpoiler(props.spoiler);
3130
}
3231

33-
if (props.children?.length) {
32+
if (props.children != null) {
33+
if (!Array.isArray(props.children)) props.children = [props.children];
34+
if (props.children.length === 0) return container;
35+
3436
for (const child of props.children.flat()) {
3537
if (child instanceof TextDisplayBuilder) {
3638
container.addTextDisplayComponents(child);

packages/commandkit/src/components/v2/media-gallery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function MediaGallery(props: MediaGalleryProps) {
1212
applyId(props, gallery);
1313

1414
if (props.children != null) {
15+
if (!Array.isArray(props.children)) props.children = [props.children];
1516
gallery.addItems(props.children.flat());
1617
}
1718

packages/commandkit/src/components/v2/section.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function Section(props: SectionProps): SectionBuilder {
1717
applyId(props, section);
1818

1919
if (props.children != null) {
20+
if (!Array.isArray(props.children)) props.children = [props.children];
2021
for (const accessory of props.children.flat()) {
2122
if (accessory instanceof ThumbnailBuilder) {
2223
section.setThumbnailAccessory(accessory);

packages/commandkit/src/components/v2/text-display.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { TextDisplayBuilder, TextDisplayComponentData } from 'discord.js';
22

3+
export type StringEncodable = string | number | boolean;
4+
35
export interface TextDisplayProps
46
extends Omit<TextDisplayComponentData, 'type' | 'content'> {
5-
children?: string | string[];
7+
children?: StringEncodable | StringEncodable[];
68
content?: string;
79
}
810

@@ -13,7 +15,11 @@ export function TextDisplay(props: TextDisplayProps) {
1315
if (Array.isArray(props.children)) {
1416
textDisplay.setContent(props.children.join(' '));
1517
} else {
16-
textDisplay.setContent(props.children);
18+
textDisplay.setContent(
19+
typeof props.children === 'string'
20+
? props.children
21+
: String(props.children),
22+
);
1723
}
1824
}
1925

0 commit comments

Comments
 (0)