discord-message-transcript 1.3.1-dev.1.46 → 1.3.1-dev.1.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  import { TopLevelComponent } from "discord.js";
2
- import { JsonTopLevelComponent, JsonComponentInContainer, TranscriptOptionsBase } from "discord-message-transcript-base";
2
+ import { JsonTopLevelComponent, TranscriptOptionsBase, JsonComponentInContainer } from "discord-message-transcript-base";
3
3
  export declare function componentsToJson(components: TopLevelComponent[], options: TranscriptOptionsBase): Promise<JsonTopLevelComponent[]>;
4
4
  export declare function isJsonComponentInContainer(component: JsonTopLevelComponent): component is JsonComponentInContainer;
@@ -3,137 +3,151 @@ import { mapButtonStyle, mapSelectorType, mapSeparatorSpacing } from "./mappers.
3
3
  import { JsonComponentType } from "discord-message-transcript-base";
4
4
  import { isValidHexColor } from "discord-message-transcript-base";
5
5
  export async function componentsToJson(components, options) {
6
- const processedComponents = await Promise.all(components.filter(component => !(!options.includeV2Components && component.type != ComponentType.ActionRow))
7
- .map(async (component) => {
8
- switch (component.type) {
9
- case ComponentType.ActionRow: {
10
- const actionRowComponents = await Promise.all(component.components.filter(c => {
11
- if (c.type == ComponentType.Button && !options.includeButtons)
12
- return false;
13
- if (c.type != ComponentType.Button && !options.includeComponents)
14
- return false;
15
- return true;
16
- }).map(async (c) => {
17
- if (c.type === ComponentType.Button) {
18
- return {
19
- type: JsonComponentType.Button,
20
- style: mapButtonStyle(c.style),
21
- label: c.label,
22
- emoji: c.emoji?.name ? c.emoji.name : null,
23
- url: c.url,
24
- disabled: c.disabled,
25
- };
26
- }
27
- else if (c.type === ComponentType.StringSelect) {
28
- return {
29
- type: JsonComponentType.StringSelect,
30
- placeholder: c.placeholder,
31
- disabled: c.disabled,
32
- options: c.options.map(option => ({
33
- label: option.label,
34
- description: option.description ?? null,
35
- emoji: option.emoji ? {
36
- id: option.emoji.id ?? null,
37
- name: option.emoji.name ?? null,
38
- animated: option.emoji.animated ?? false,
39
- } : null,
40
- }))
41
- };
42
- }
43
- else {
44
- return {
45
- type: mapSelectorType(c.type),
46
- placeholder: c.placeholder,
47
- disabled: c.disabled,
48
- };
49
- }
50
- }));
51
- if (actionRowComponents.length > 0)
52
- return { type: JsonComponentType.ActionRow, components: actionRowComponents };
53
- return null;
54
- }
55
- case ComponentType.Container: {
56
- const newOptions = { ...options, includeComponents: true, includeButtons: true };
57
- const componentsJson = await componentsToJson(component.components, newOptions);
58
- return {
59
- type: JsonComponentType.Container,
60
- components: componentsJson.filter(isJsonComponentInContainer), // Input components that are container-safe must always produce container-safe output.
61
- hexAccentColor: isValidHexColor(component.hexAccentColor, false),
62
- spoiler: component.spoiler,
63
- };
64
- }
65
- case ComponentType.File: {
66
- return {
67
- type: JsonComponentType.File,
68
- fileName: component.data.name ?? null,
69
- size: component.data.size ?? 0,
70
- url: component.file.url,
71
- spoiler: component.spoiler,
72
- };
73
- }
74
- case ComponentType.MediaGallery: {
75
- const mediaItems = component.items.map(item => {
76
- return {
77
- media: { url: item.media.url },
78
- spoiler: item.spoiler,
79
- };
80
- });
81
- return {
82
- type: JsonComponentType.MediaGallery,
83
- items: mediaItems,
84
- };
85
- }
86
- case ComponentType.Section: {
87
- let accessoryJson;
88
- if (component.accessory.type === ComponentType.Button) {
89
- accessoryJson = {
90
- type: JsonComponentType.Button,
91
- style: mapButtonStyle(component.accessory.style),
92
- label: component.accessory.label,
93
- emoji: component.accessory.emoji?.name ? component.accessory.emoji.name : null,
94
- url: component.accessory.url,
95
- disabled: component.accessory.disabled,
96
- };
97
- }
98
- else if (component.accessory.type === ComponentType.Thumbnail) {
99
- accessoryJson = {
100
- type: JsonComponentType.Thumbnail,
101
- media: {
102
- url: component.accessory.media.url,
103
- },
104
- spoiler: component.accessory.spoiler,
105
- };
106
- }
107
- else
108
- return null;
109
- const sectionComponents = component.components.map(c => ({
110
- type: JsonComponentType.TextDisplay,
111
- content: c.content,
112
- }));
113
- return {
114
- type: JsonComponentType.Section,
115
- accessory: accessoryJson,
116
- components: sectionComponents,
117
- };
118
- }
119
- case ComponentType.Separator: {
120
- return {
121
- type: JsonComponentType.Separator,
122
- spacing: mapSeparatorSpacing(component.spacing),
123
- divider: component.divider,
124
- };
125
- }
126
- case ComponentType.TextDisplay: {
127
- return {
128
- type: JsonComponentType.TextDisplay,
129
- content: component.content,
130
- };
131
- }
132
- default:
133
- return null;
6
+ const filtered = components.filter(c => options.includeV2Components || c.type === ComponentType.ActionRow);
7
+ const processed = await Promise.all(filtered.map(c => convertComponent(c, options)));
8
+ return processed.filter(c => c != null);
9
+ }
10
+ async function convertComponent(component, options) {
11
+ switch (component.type) {
12
+ case ComponentType.ActionRow:
13
+ return convertActionRow(component, options);
14
+ case ComponentType.Container:
15
+ return convertContainer(component, options);
16
+ case ComponentType.File:
17
+ return convertFile(component);
18
+ case ComponentType.MediaGallery:
19
+ return convertMediaGallery(component);
20
+ case ComponentType.Section:
21
+ return convertSection(component);
22
+ case ComponentType.Separator:
23
+ return convertSeparator(component);
24
+ case ComponentType.TextDisplay:
25
+ return convertTextDisplay(component);
26
+ default:
27
+ return null;
28
+ }
29
+ }
30
+ async function convertActionRow(component, options) {
31
+ const rowComponents = await Promise.all(component.components
32
+ .filter(c => (c.type === ComponentType.Button ? options.includeButtons : options.includeComponents))
33
+ .map(c => convertActionRowChild(c)));
34
+ if (rowComponents.length === 0)
35
+ return null;
36
+ return { type: JsonComponentType.ActionRow, components: rowComponents };
37
+ }
38
+ async function convertActionRowChild(component) {
39
+ switch (component.type) {
40
+ case ComponentType.Button: {
41
+ return {
42
+ type: JsonComponentType.Button,
43
+ style: mapButtonStyle(component.style),
44
+ label: component.label,
45
+ emoji: component.emoji?.name ?? null,
46
+ url: component.url,
47
+ disabled: component.disabled,
48
+ };
49
+ }
50
+ case ComponentType.StringSelect: {
51
+ return {
52
+ type: JsonComponentType.StringSelect,
53
+ placeholder: component.placeholder,
54
+ disabled: component.disabled,
55
+ options: component.options.map(option => ({
56
+ label: option.label,
57
+ description: option.description ?? null,
58
+ emoji: option.emoji ? { id: option.emoji.id ?? null, name: option.emoji.name ?? null, animated: option.emoji.animated ?? false } : null,
59
+ })),
60
+ };
61
+ }
62
+ default: {
63
+ return {
64
+ type: mapSelectorType(component.type),
65
+ placeholder: component.placeholder,
66
+ disabled: component.disabled,
67
+ };
68
+ }
69
+ }
70
+ }
71
+ async function convertContainer(component, options) {
72
+ const newOptions = { ...options, includeComponents: true, includeButtons: true };
73
+ const componentsJson = await componentsToJson(component.components, newOptions);
74
+ return {
75
+ type: JsonComponentType.Container,
76
+ components: componentsJson.filter(isJsonComponentInContainer), // Input components that are container-safe must always produce container-safe output.
77
+ hexAccentColor: isValidHexColor(component.hexAccentColor, false),
78
+ spoiler: component.spoiler,
79
+ };
80
+ }
81
+ function convertFile(component) {
82
+ return {
83
+ type: JsonComponentType.File,
84
+ fileName: component.data.name ?? null,
85
+ size: component.data.size ?? 0,
86
+ url: component.file.url,
87
+ spoiler: component.spoiler,
88
+ };
89
+ }
90
+ async function convertMediaGallery(component) {
91
+ const mediaItems = await Promise.all(component.items.map(item => {
92
+ return {
93
+ media: { url: item.media.url },
94
+ spoiler: item.spoiler,
95
+ };
96
+ }));
97
+ return {
98
+ type: JsonComponentType.MediaGallery,
99
+ items: mediaItems,
100
+ };
101
+ }
102
+ function convertSection(component) {
103
+ let accessoryJson = null;
104
+ switch (component.accessory.type) {
105
+ case ComponentType.Button: {
106
+ accessoryJson = {
107
+ type: JsonComponentType.Button,
108
+ style: mapButtonStyle(component.accessory.style),
109
+ label: component.accessory.label,
110
+ emoji: component.accessory.emoji?.name ? component.accessory.emoji.name : null,
111
+ url: component.accessory.url,
112
+ disabled: component.accessory.disabled,
113
+ };
114
+ break;
134
115
  }
116
+ case ComponentType.Thumbnail: {
117
+ accessoryJson = {
118
+ type: JsonComponentType.Thumbnail,
119
+ media: {
120
+ url: component.accessory.media.url,
121
+ },
122
+ spoiler: component.accessory.spoiler,
123
+ };
124
+ break;
125
+ }
126
+ default:
127
+ break;
128
+ }
129
+ const sectionComponents = component.components.map(c => ({
130
+ type: JsonComponentType.TextDisplay,
131
+ content: c.content,
135
132
  }));
136
- return processedComponents.filter(c => c != null);
133
+ return {
134
+ type: JsonComponentType.Section,
135
+ accessory: accessoryJson,
136
+ components: sectionComponents,
137
+ };
138
+ }
139
+ function convertSeparator(component) {
140
+ return {
141
+ type: JsonComponentType.Separator,
142
+ spacing: mapSeparatorSpacing(component.spacing),
143
+ divider: component.divider,
144
+ };
145
+ }
146
+ function convertTextDisplay(component) {
147
+ return {
148
+ type: JsonComponentType.TextDisplay,
149
+ content: component.content,
150
+ };
137
151
  }
138
152
  export function isJsonComponentInContainer(component) {
139
153
  return (component.type == JsonComponentType.ActionRow ||
@@ -57,7 +57,7 @@ export async function messagesUrlResolver(messages, options, cdnOptions, urlCach
57
57
  }));
58
58
  async function componentsFunction(components) {
59
59
  return Promise.all(components.map(async (component) => {
60
- if (component.type == JsonComponentType.Section) {
60
+ if (component.type == JsonComponentType.Section && component.accessory) {
61
61
  if (component.accessory.type == JsonComponentType.Thumbnail) {
62
62
  return {
63
63
  ...component,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discord-message-transcript",
3
- "version": "1.3.1-dev.1.46",
3
+ "version": "1.3.1-dev.1.47",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "homepage": "https://github.com/HenriqueMairesse/discord-message-transcript#readme",
47
47
  "dependencies": {
48
- "discord-message-transcript-base": "1.3.1-dev.1.46"
48
+ "discord-message-transcript-base": "1.3.1-dev.1.47"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "discord.js": ">=14.19.0 <15"