component v2

This commit is contained in:
TotallyTung
2025-06-23 22:04:53 +07:00
committed by GitHub
parent 714f8bd5de
commit 7a2a3d7a05
12 changed files with 250 additions and 1 deletions

View File

@@ -91,6 +91,41 @@ class BaseMessageComponent {
component = data instanceof TextInputComponent ? data : new TextInputComponent(data); component = data instanceof TextInputComponent ? data : new TextInputComponent(data);
break; break;
} }
case MessageComponentTypes.SECTION: {
const SectionComponent = require('./SectionComponent');
component = data instanceof SectionComponent ? data : new SectionComponent(data);
break;
}
case MessageComponentTypes.TEXT_DISPLAY: {
const TextDisplayComponent = require('./TextDisplayComponent');
component = data instanceof TextDisplayComponent ? data : new TextDisplayComponent(data);
break;
}
case MessageComponentTypes.THUMBNAIL: {
const ThumbnailComponent = require('./ThumbnailComponent');
component = data instanceof ThumbnailComponent ? data : new ThumbnailComponent(data);
break;
}
case MessageComponentTypes.MEDIA_GALLERY: {
const MediaGalleryComponent = require('./MediaGalleryComponent');
component = data instanceof MediaGalleryComponent ? data : new MediaGalleryComponent(data);
break;
}
case MessageComponentTypes.FILE: {
const FileComponent = require('./FileComponent');
component = data instanceof FileComponent ? data : new FileComponent(data);
break;
}
case MessageComponentTypes.SEPARATOR: {
const SeparatorComponent = require('./SeparatorComponent');
component = data instanceof SeparatorComponent ? data : new SeparatorComponent(data);
break;
}
case MessageComponentTypes.CONTAINER: {
const ContainerComponent = require('./ContainerComponent');
component = data instanceof ContainerComponent ? data : new ContainerComponent(data);
break;
}
default: default:
if (client) { if (client) {
client.emit(Events.DEBUG, `[BaseMessageComponent] Received component with unknown type: ${data.type}`); client.emit(Events.DEBUG, `[BaseMessageComponent] Received component with unknown type: ${data.type}`);

View File

@@ -0,0 +1,24 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { MessageComponentTypes } = require('../util/Constants');
class ContainerComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'CONTAINER' });
this.components = data.components?.map(c => BaseMessageComponent.create(c)) ?? null;
this.accent_color = data.accent_color ?? null;
this.spoiler = data.spoiler ?? false;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
components: this.components.map(c => c.toJSON()),
accent_color: this.accent_color,
spoiler: this.spoiler,
};
}
}
module.exports = ContainerComponent;

View File

@@ -0,0 +1,22 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { UnfurledMediaItem } = require('./UnfurledMediaItem');
const { MessageComponentTypes } = require('../util/Constants');
class FileComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'FILE' });
this.file = new UnfurledMediaItem(data.file) ?? null;
this.spoiler = data.spoiler ?? false;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
file: this.content,
};
}
}
module.exports = FileComponent;

View File

@@ -0,0 +1,21 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { MediaGalleryItem } = require('./MediaGalleryItem');
const { MessageComponentTypes } = require('../util/Constants');
class MediaGalleryComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'MEDIA_GALLERY' });
this.items = data.items?.map(item => new MediaGalleryItem(item)) ?? [];
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
items: this.items.map(c => c.toJSON()),
};
}
}
module.exports = MediaGalleryComponent;

View File

@@ -0,0 +1,21 @@
'use strict';
const { UnfurledMediaItem } = require('./UnfurledMediaItem');
class MediaGalleryItem {
constructor(data = {}) {
this.media = new UnfurledMediaItem(data.media);
this.description = data.description ?? null;
this.spoiler = data.spoiler ?? false;
}
toJSON() {
return {
media: this.media.toJSON(),
description: this.description,
spoiler: this.spoiler,
};
}
}
module.exports = MediaGalleryItem;

View File

@@ -0,0 +1,23 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { TextDisplayComponent } = require('./TextDisplayComponent');
const { MessageComponentTypes } = require('../util/Constants');
class SectionComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'SECTION' });
this.components = data.components?.map(c => BaseMessageComponent.create(c)) ?? [];
this.accessory = BaseMessageComponent.create(c) ?? null;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
components: this.components.map(c => c.toJSON()),
accessory: this.accessory.toJSON(),
};
}
}
module.exports = SectionComponent;

View File

@@ -0,0 +1,22 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { MessageComponentTypes, SeparatorSpacingSizes } = require('../util/Constants');
class SeparatorComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'SEPARATOR' });
this.spacing = data.spacing ?? SeparatorSpacingSizes.SMALL;
this.divider = data.divider ?? true;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
spacing: this.spacing,
divider: this.divider,
};
}
}
module.exports = SeparatorComponent;

View File

@@ -0,0 +1,20 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { MessageComponentTypes } = require('../util/Constants');
class TextDisplayComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'TEXT_DISPLAY' });
this.content = data.content ?? null;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
content: this.content,
};
}
}
module.exports = TextDisplayComponent;

View File

@@ -0,0 +1,25 @@
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { UnfurledMediaItem } = require('./UnfurledMediaItem');
const { MessageComponentTypes } = require('../util/Constants');
class ThumbnailComponent extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'THUMBNAIL' });
this.media = new UnfurledMediaItem(data.media) ?? null;
this.description = data.description ?? null;
this.spoiler = data.spoiler ?? false;
}
toJSON() {
return {
type: MessageComponentTypes[this.type],
media: this.media.toJSON(),
description: this.description,
spoiler: this.spoiler,
};
}
}
module.exports = ThumbnailComponent;

View File

@@ -0,0 +1,15 @@
'use strict';
class UnfurledMediaItem {
constructor(data = {}) {
this.url = data.url ?? null;
}
toJSON() {
return {
url: this.url,
};
}
}
module.exports = UnfurledMediaItem;

View File

@@ -1615,6 +1615,15 @@ exports.MessageComponentTypes = createEnum([
'ROLE_SELECT', 'ROLE_SELECT',
'MENTIONABLE_SELECT', 'MENTIONABLE_SELECT',
'CHANNEL_SELECT', 'CHANNEL_SELECT',
'SECTION',
'TEXT_DISPLAY',
'THUMBNAIL',
'MEDIA_GALLERY',
'FILE',
'SEPARATOR',
null,
null,
'CONTAINER',
]); ]);
/** /**
@@ -1796,6 +1805,12 @@ exports.RelationshipTypes = createEnum([
'IMPLICIT', 'IMPLICIT',
]); ]);
exports.SeparatorSpacingSizes = createEnum([
null,
'SMALL',
'LARGE',
]);
exports._cleanupSymbol = Symbol('djsCleanup'); exports._cleanupSymbol = Symbol('djsCleanup');
function keyMirror(arr) { function keyMirror(arr) {
@@ -1855,6 +1870,7 @@ function createEnum(keys) {
* @property {Object<InteractionType, number>} InteractionTypes The type of an {@link Interaction} object. * @property {Object<InteractionType, number>} InteractionTypes The type of an {@link Interaction} object.
* @property {InviteScope[]} InviteScopes The scopes of an invite. * @property {InviteScope[]} InviteScopes The scopes of an invite.
* @property {Object<RelationshipType, number>} RelationshipTypes Relationship Enums * @property {Object<RelationshipType, number>} RelationshipTypes Relationship Enums
* * @property {Object<SeparatorSpacingSize, number>} SeparatorSpacingSize Relationship Enums
* @property {Object<MembershipState, number>} MembershipStates The value set for a team members membership state. * @property {Object<MembershipState, number>} MembershipStates The value set for a team members membership state.
* @property {Object<MessageButtonStyle, number>} MessageButtonStyles The style of a message button. * @property {Object<MessageButtonStyle, number>} MessageButtonStyles The style of a message button.
* @property {Object<MessageComponentType, number>} MessageComponentTypes The type of a message component. * @property {Object<MessageComponentType, number>} MessageComponentTypes The type of a message component.

7
typings/enums.d.ts vendored
View File

@@ -226,7 +226,7 @@ export const enum MessageComponentTypes {
ROLE_SELECT = 6, ROLE_SELECT = 6,
MENTIONABLE_SELECT = 7, MENTIONABLE_SELECT = 7,
CHANNEL_SELECT = 8, CHANNEL_SELECT = 8,
SELECTION = 9, SECTION = 9,
TEXT_DISPLAY = 10, TEXT_DISPLAY = 10,
THUMBNAIL = 11, THUMBNAIL = 11,
MEDIA_GALLERY = 12, MEDIA_GALLERY = 12,
@@ -332,3 +332,8 @@ export const enum RelationshipTypes {
PENDING_OUTGOING = 4, PENDING_OUTGOING = 4,
IMPLICIT = 5, IMPLICIT = 5,
} }
export const enum SeparatorSpacingSize {
SMALL = 1,
LARGE = 2,
}