cordo 2.20.1 → 2.21.0

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.
Files changed (41) hide show
  1. package/package.json +1 -1
  2. package/src/components/builtin/button.ts +11 -7
  3. package/src/components/builtin/checkbox-group.ts +55 -33
  4. package/src/components/builtin/container.ts +37 -7
  5. package/src/components/builtin/file-upload.ts +50 -24
  6. package/src/components/builtin/gallery.ts +12 -4
  7. package/src/components/builtin/image.ts +3 -1
  8. package/src/components/builtin/link-button.ts +9 -7
  9. package/src/components/builtin/menu-item.ts +107 -0
  10. package/src/components/builtin/menu-link.ts +69 -0
  11. package/src/components/builtin/modal.ts +12 -9
  12. package/src/components/builtin/overflow-menu.ts +56 -0
  13. package/src/components/builtin/radio-group.ts +21 -17
  14. package/src/components/builtin/row.ts +41 -9
  15. package/src/components/builtin/section.ts +24 -6
  16. package/src/components/builtin/select-string.ts +59 -32
  17. package/src/components/builtin/text-input.ts +33 -24
  18. package/src/components/builtin/text.ts +3 -1
  19. package/src/components/component.ts +23 -5
  20. package/src/components/index.ts +7 -0
  21. package/src/components/limits.ts +156 -0
  22. package/src/components/mods/debug-id-to-label.ts +1 -1
  23. package/src/components/mods/debug-print.ts +2 -1
  24. package/src/components/mods/disable-all-components.ts +17 -5
  25. package/src/core/files/command.ts +9 -8
  26. package/src/core/files/config.ts +39 -7
  27. package/src/core/files/route.ts +12 -4
  28. package/src/core/gateway.ts +2 -1
  29. package/src/core/hooks.ts +2 -1
  30. package/src/core/index.ts +2 -1
  31. package/src/core/routing/filesystem.ts +2 -1
  32. package/src/core/routing/resolve.ts +3 -2
  33. package/src/core/routing/respond.ts +8 -5
  34. package/src/errors/handle.ts +3 -2
  35. package/src/functions/compiler.ts +25 -3
  36. package/src/lib/constants.ts +35 -2
  37. package/src/lib/limits.ts +98 -0
  38. package/src/lib/logger.ts +28 -0
  39. package/src/plugin/djs.ts +2 -1
  40. package/tsconfig.json +1 -4
  41. package/src/lib/ids.test.ts +0 -47
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordo",
3
- "version": "2.20.1",
3
+ "version": "2.21.0",
4
4
  "description": "A framework for handling complex discord api interactions",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -6,6 +6,8 @@ import type { CordoFunct, CordoFunctRun } from '../../functions'
6
6
  import { FunctCompiler } from '../../functions/compiler'
7
7
  import { MaxLengthConstants } from '../../lib/constants'
8
8
  import { CordoMagic } from '../../core/magic'
9
+ import { LibLimits } from '../../lib/limits'
10
+ import { LibLogger } from '../../lib/logger'
9
11
 
10
12
 
11
13
  export function button() {
@@ -21,22 +23,22 @@ export function button() {
21
23
  if (emojiVal)
22
24
  return ''
23
25
  if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
24
- console.warn('A button was rendered with neither a label or an emoji provided. Cordo will use a placeholder label.')
26
+ LibLogger.warn('A button was rendered with neither a label or an emoji provided. Cordo will use a placeholder label.')
25
27
  return 'Click'
26
28
  }
27
29
 
28
- return Hooks.callHook(
30
+ return LibLimits.text(Hooks.callHook(
29
31
  'transformUserFacingText',
30
32
  labelVal,
31
33
  { component: 'Button', position: 'label' }
32
- )?.slice(0, MaxLengthConstants.BUTTON_LABEL)
34
+ ), MaxLengthConstants.BUTTON_LABEL, 'A button label')
33
35
  }
34
36
 
35
37
  const advanced = {
36
38
  /** Will override cordo's custom_id generation. Not compatible with onClick handlers */
37
39
  overrideCustomId(customId: string) {
38
40
  if (functVal.length > 0 && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
39
- console.warn('You are overriding the custom_id of a button that has onClick handlers. This will prevent the onClick handlers from working.')
41
+ LibLogger.warn('You are overriding the custom_id of a button that has onClick handlers. This will prevent the onClick handlers from working.')
40
42
  overrideCustomIdVal = customId
41
43
  return out
42
44
  }
@@ -49,7 +51,9 @@ export function button() {
49
51
  emoji: emojiVal,
50
52
  style: styleVal,
51
53
  disabled: disabledVal,
52
- custom_id: overrideCustomIdVal ?? FunctCompiler.toCustomId(disabledVal ? [] : functVal)
54
+ custom_id: overrideCustomIdVal !== undefined
55
+ ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal)
56
+ : FunctCompiler.toCustomId(disabledVal ? [] : functVal)
53
57
  })),
54
58
 
55
59
  label: (text: string) => {
@@ -78,8 +82,8 @@ export function button() {
78
82
  return out
79
83
  },
80
84
  onClick: (...funct: CordoFunctRun) => {
81
- if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
82
- console.warn('You are adding onClick handlers to a button that already has an overridden custom_id. These handlers will not be called.')
85
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
86
+ LibLogger.warn('You are adding onClick handlers to a button that already has an overridden custom_id. These handlers will not be called.')
83
87
  functVal.push(...funct)
84
88
  return out
85
89
  },
@@ -2,8 +2,10 @@ import { ComponentType, createComponent } from '../component'
2
2
  import { Hooks } from '../../core/hooks'
3
3
  import { value, type CordoFunct, type CordoFunctRun } from '../../functions'
4
4
  import { FunctCompiler } from '../../functions/compiler'
5
- import { MaxLengthConstants } from '../../lib/constants'
5
+ import { CountConstants, MaxLengthConstants } from '../../lib/constants'
6
+ import { LibLimits } from '../../lib/limits'
6
7
  import { CordoMagic } from '../../core/magic'
8
+ import { LibLogger } from '../../lib/logger'
7
9
 
8
10
 
9
11
  type CheckboxGroupOption<Value extends string = string> = ({
@@ -19,7 +21,7 @@ export function checkboxGroup<Values extends string = string>() {
19
21
  let descriptionVal: string | undefined = undefined
20
22
  let optionsVal: CheckboxGroupOption[] = []
21
23
  let minValues: number | undefined = undefined
22
- let maxValues: number | undefined = undefined
24
+ let maxValues: number | 'all' | undefined = undefined
23
25
  let requiredVal: boolean = false
24
26
  let ref: string | undefined = undefined
25
27
  const functVal: CordoFunct[] = []
@@ -28,7 +30,7 @@ export function checkboxGroup<Values extends string = string>() {
28
30
  function getLabel() {
29
31
  if (!labelVal) {
30
32
  if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
31
- console.warn('A checkbox group was rendered without a label provided. Cordo will use a placeholder label.')
33
+ LibLogger.warn('A checkbox group was rendered without a label provided. Cordo will use a placeholder label.')
32
34
  return 'Pick one'
33
35
  }
34
36
  return Hooks.callHook(
@@ -49,15 +51,17 @@ export function checkboxGroup<Values extends string = string>() {
49
51
  }
50
52
 
51
53
  function getOptions(): CheckboxGroupOption[] {
52
- return optionsVal.slice(0, 10).map(o => ({
54
+ if (optionsVal.length < CountConstants.CHECKBOX_OPTIONS_MIN)
55
+ LibLimits.warn('invalidComponent', `A checkbox group needs at least ${CountConstants.CHECKBOX_OPTIONS_MIN} option. Discord will reject it.`)
56
+ return LibLimits.list(optionsVal, CountConstants.CHECKBOX_OPTIONS_MAX, 'A checkbox group').map(o => ({
53
57
  ...o,
54
- label: Hooks.callHook('transformUserFacingText', o.label, { component: 'CheckboxGroup', position: 'option.label' })?.slice(0, MaxLengthConstants.SELECT_OPTION_LABEL),
58
+ label: LibLimits.text(Hooks.callHook('transformUserFacingText', o.label, { component: 'CheckboxGroup', position: 'option.label' }), MaxLengthConstants.SELECT_OPTION_LABEL, 'A checkbox group option label'),
55
59
  description: o.description
56
- ? Hooks.callHook('transformUserFacingText', o.description, { component: 'CheckboxGroup', position: 'option.description' })?.slice(0, MaxLengthConstants.SELECT_OPTION_DESCRIPTION)
60
+ ? LibLimits.text(Hooks.callHook('transformUserFacingText', o.description, { component: 'CheckboxGroup', position: 'option.description' }), MaxLengthConstants.SELECT_OPTION_DESCRIPTION, 'A checkbox group option description')
57
61
  : undefined,
58
62
  /** If the custom_id is overriden, use the override value (or an empty custom_id) - otherwise compile the custom_ids and values into the value field */
59
- value: overrideCustomIdVal
60
- ? (o.value ?? FunctCompiler.toCustomId([]))
63
+ value: overrideCustomIdVal !== undefined
64
+ ? (LibLimits.text(o.value, MaxLengthConstants.SELECT_OPTION_VALUE, 'A checkbox group option value') ?? FunctCompiler.toCustomId([]))
61
65
  : FunctCompiler.toCustomId([
62
66
  ...(o.onSelect
63
67
  ? Array.isArray(o.onSelect)
@@ -71,39 +75,56 @@ export function checkboxGroup<Values extends string = string>() {
71
75
  }
72
76
 
73
77
  const advanced = {
74
- /** Will override cordo's custom_id generation. Not compatible with onClick handlers */
75
- rawCustomIds(customId: string) {
78
+ /** Will override cordo's custom_id generation. Not compatible with onSubmit handlers */
79
+ overrideCustomId(customId: string) {
76
80
  if (!CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride')) {
77
81
  if (functVal.length > 0)
78
- console.warn('You are overriding the custom_id of a checkbox group that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
82
+ LibLogger.warn('You are overriding the custom_id of a checkbox group that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
79
83
  if (ref)
80
- console.warn('You are overriding the custom_id of a checkbox group that has an as() id. Your as() id will be overridden.')
84
+ LibLogger.warn('You are overriding the custom_id of a checkbox group that has an as() id. Your as() id will be overridden.')
81
85
  }
82
86
  overrideCustomIdVal = customId
83
87
  return out
88
+ },
89
+ /** @deprecated renamed to overrideCustomId, like on all other components */
90
+ rawCustomIds(customId: string) {
91
+ return advanced.overrideCustomId(customId)
84
92
  }
85
93
  }
86
94
 
87
95
  const out = {
88
- ...createComponent('CheckboxGroup', () => ({
89
- type: ComponentType.CheckboxGroup,
90
- label: getLabel(),
91
- description: getDescription(),
92
- min_values: Math.max(minValues ?? 1, 0),
93
- max_values: Math.min(maxValues ?? minValues ?? 1, optionsVal.length),
94
- required: requiredVal,
95
- options: getOptions(),
96
- custom_id: overrideCustomIdVal ?? FunctCompiler.toCustomId([
97
- ...functVal,
98
- ...(ref ? [ value(ref) ] : [])
99
- ]),
100
- 'modal:label': getLabel(),
101
- 'modal:description': getDescription(),
102
- })),
96
+ ...createComponent('CheckboxGroup', ({ hirarchy }) => {
97
+ const limit = Math.min(optionsVal.length, CountConstants.CHECKBOX_OPTIONS_MAX)
98
+ const range = LibLimits.selectionRange({
99
+ where: 'a checkbox group',
100
+ min: minValues ?? 1,
101
+ max: maxValues,
102
+ limit,
103
+ defaultMax: 1,
104
+ required: requiredVal,
105
+ inModal: true
106
+ })
107
+
108
+ return {
109
+ type: ComponentType.CheckboxGroup,
110
+ label: getLabel(),
111
+ description: getDescription(),
112
+ min_values: range.min,
113
+ max_values: range.max ?? Math.min(1, Math.max(limit, 1)),
114
+ required: range.required,
115
+ options: getOptions(),
116
+ custom_id: overrideCustomIdVal !== undefined ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal) : FunctCompiler.toCustomId([
117
+ ...functVal,
118
+ ...(ref ? [ value(ref) ] : [])
119
+ ]),
120
+ 'modal:label': hirarchy.includes('Modal') ? getLabel() : undefined,
121
+ 'modal:description': hirarchy.includes('Modal') ? getDescription() : undefined,
122
+ }
123
+ }),
103
124
 
104
125
  as: (id: string) => {
105
- if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
106
- console.warn('You are assigning an as() id to a checkbox group that already has an overridden custom_id. Your provided id will be ignored.')
126
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
127
+ LibLogger.warn('You are assigning an as() id to a checkbox group that already has an overridden custom_id. Your provided id will be ignored.')
107
128
  ref = id
108
129
  return out
109
130
  },
@@ -115,13 +136,14 @@ export function checkboxGroup<Values extends string = string>() {
115
136
  minValues = num
116
137
  return out
117
138
  },
118
- max: (num: number = 10) => {
119
- maxValues = num
139
+ /** omit num to allow checking all options */
140
+ max: (num?: number) => {
141
+ maxValues = num ?? 'all'
120
142
  return out
121
143
  },
122
144
  onSubmit: (...funct: CordoFunctRun) => {
123
- if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
124
- console.warn('You are adding onSubmit handlers to a checkbox group that already has an overridden custom_id. These handlers will not be called.')
145
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
146
+ LibLogger.warn('You are adding onSubmit handlers to a checkbox group that already has an overridden custom_id. These handlers will not be called.')
125
147
  functVal.push(...funct)
126
148
  return out
127
149
  },
@@ -1,21 +1,51 @@
1
1
  import { ComponentType, createComponent, renderComponentList, type CordoComponent } from '../component'
2
2
  import type { CordoModifier } from '../modifier'
3
+ import { CountConstants } from '../../lib/constants'
4
+ import { LibLimits } from '../../lib/limits'
3
5
 
4
6
 
5
- export type AllowedComponents = CordoComponent<'ActionRow' | 'Button' | 'TextDisplay' | 'Section' | 'MediaGallery' | 'Seperator' | 'File' | 'StringSelect'> | CordoModifier
7
+ export type AllowedComponents = CordoComponent<'ActionRow' | 'Button' | 'OverflowMenu' | 'TextDisplay' | 'Section' | 'MediaGallery' | 'Seperator' | 'File' | 'StringSelect'> | CordoModifier
6
8
  type AllowedComponentArray = Array<AllowedComponents | AllowedComponents[]>
7
9
 
10
+ /** reads '#rgb', '#rrggbb' (the # is optional) or a number into a color Discord accepts. Returns undefined and warns otherwise */
11
+ export function parseColor(color: string | number | undefined): number | undefined {
12
+ if (color === undefined)
13
+ return undefined
14
+
15
+ if (typeof color === 'number') {
16
+ if (Number.isInteger(color) && color >= 0 && color <= CountConstants.ACCENT_COLOR_MAX)
17
+ return color
18
+ LibLimits.warn('invalidColor', `The color ${color} is not between 0x000000 and 0xFFFFFF. Cordo left it out.`)
19
+ return undefined
20
+ }
21
+
22
+ const hex = color.trim().replace(/^#/, '')
23
+ if (/^[0-9a-f]{3}$/i.test(hex))
24
+ return parseInt(hex.split('').map(c => c + c).join(''), 16)
25
+ if (/^[0-9a-f]{6}$/i.test(hex))
26
+ return parseInt(hex, 16)
27
+
28
+ LibLimits.warn('invalidColor', `The color '${color}' could not be read, use '#rgb', '#rrggbb' or a number. Cordo left it out.`)
29
+ return undefined
30
+ }
31
+
8
32
  export function container(...components: AllowedComponentArray) {
9
33
  let accentColor: string | number | undefined = undefined
10
34
  let spoiler: boolean | undefined = undefined
11
35
 
12
36
  const out = {
13
- ...createComponent('Container', ({ hirarchy, attributes }) => ({
14
- type: ComponentType.Container,
15
- components: renderComponentList(components.flat(), 'Container', hirarchy, attributes),
16
- accent_color: typeof accentColor === 'string' ? parseInt(accentColor.slice(1), 16) : accentColor,
17
- spoiler
18
- })),
37
+ ...createComponent('Container', ({ hirarchy, attributes }) => {
38
+ const rendered = renderComponentList(components.flat(), 'Container', hirarchy, attributes)
39
+ if (!rendered.length)
40
+ return null
41
+
42
+ return {
43
+ type: ComponentType.Container,
44
+ components: rendered,
45
+ accent_color: parseColor(accentColor),
46
+ spoiler
47
+ }
48
+ }),
19
49
 
20
50
  accentColor: (color: string | number | null) => {
21
51
  accentColor = color ?? undefined
@@ -3,13 +3,20 @@ import { Hooks } from '../../core/hooks'
3
3
  import { value, type CordoFunct, type CordoFunctRun } from '../../functions'
4
4
  import { FunctCompiler } from '../../functions/compiler'
5
5
  import { CordoMagic } from '../../core/magic'
6
+ import { CountConstants } from '../../lib/constants'
7
+ import { LibLimits } from '../../lib/limits'
8
+ import { LibLogger } from '../../lib/logger'
9
+
10
+
11
+ type FileType = 'image' | 'video' | 'audio' | `.${string}`
6
12
 
7
13
 
8
14
  export function fileUpload() {
9
15
  let labelVal: string | undefined = undefined
10
16
  let descriptionVal: string | undefined = undefined
11
17
  let minValues: number | undefined = undefined
12
- let maxValues: number | undefined = undefined
18
+ let maxValues: number | 'all' | undefined = undefined
19
+ let fileTypesVal: FileType[] | undefined = undefined
13
20
  let requiredVal: boolean = false
14
21
  let ref: string | undefined = undefined
15
22
  const functVal: CordoFunct[] = []
@@ -18,7 +25,7 @@ export function fileUpload() {
18
25
  function getLabel() {
19
26
  if (!labelVal) {
20
27
  if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
21
- console.warn('A file upload was rendered without a label provided. Cordo will use a placeholder label.')
28
+ LibLogger.warn('A file upload was rendered without a label provided. Cordo will use a placeholder label.')
22
29
  return 'Your response'
23
30
  }
24
31
  return Hooks.callHook(
@@ -43,9 +50,9 @@ export function fileUpload() {
43
50
  overrideCustomId(customId: string) {
44
51
  if (!CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride')) {
45
52
  if (functVal.length > 0)
46
- console.warn('You are overriding the custom_id of a file upload that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
53
+ LibLogger.warn('You are overriding the custom_id of a file upload that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
47
54
  if (ref)
48
- console.warn('You are overriding the custom_id of a file upload that has an as() id. Your as() id will be overridden.')
55
+ LibLogger.warn('You are overriding the custom_id of a file upload that has an as() id. Your as() id will be overridden.')
49
56
  }
50
57
  overrideCustomIdVal = customId
51
58
  return out
@@ -53,24 +60,37 @@ export function fileUpload() {
53
60
  }
54
61
 
55
62
  const out = {
56
- ...createComponent('FileUpload', () => ({
57
- type: ComponentType.FileUpload,
58
- label: getLabel(),
59
- description: getDescription(),
60
- min_values: Math.max(minValues ?? 1, 0),
61
- max_values: Math.min(maxValues ?? minValues ?? 1, 10),
62
- required: requiredVal,
63
- custom_id: overrideCustomIdVal ?? FunctCompiler.toCustomId([
64
- ...functVal,
65
- ...(ref ? [ value(ref) ] : [])
66
- ]),
67
- 'modal:label': getLabel(),
68
- 'modal:description': getDescription(),
69
- })),
63
+ ...createComponent('FileUpload', ({ hirarchy }) => {
64
+ const range = LibLimits.selectionRange({
65
+ where: 'a file upload',
66
+ min: minValues ?? 1,
67
+ max: maxValues,
68
+ limit: CountConstants.FILE_UPLOAD_VALUES,
69
+ defaultMax: 1,
70
+ required: requiredVal,
71
+ inModal: true
72
+ })
73
+
74
+ return {
75
+ type: ComponentType.FileUpload,
76
+ label: getLabel(),
77
+ description: getDescription(),
78
+ min_values: range.min,
79
+ max_values: range.max ?? 1,
80
+ required: range.required,
81
+ file_types: fileTypesVal ? LibLimits.list(fileTypesVal, CountConstants.FILE_UPLOAD_TYPES, 'The file types of a file upload') : undefined,
82
+ custom_id: overrideCustomIdVal !== undefined ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal) : FunctCompiler.toCustomId([
83
+ ...functVal,
84
+ ...(ref ? [ value(ref) ] : [])
85
+ ]),
86
+ 'modal:label': hirarchy.includes('Modal') ? getLabel() : undefined,
87
+ 'modal:description': hirarchy.includes('Modal') ? getDescription() : undefined,
88
+ }
89
+ }),
70
90
 
71
91
  as: (id: string) => {
72
- if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
73
- console.warn('You are assigning an as() id to a file upload that already has an overridden custom_id. Your provided id will be ignored.')
92
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
93
+ LibLogger.warn('You are assigning an as() id to a file upload that already has an overridden custom_id. Your provided id will be ignored.')
74
94
  ref = id
75
95
  return out
76
96
  },
@@ -78,8 +98,14 @@ export function fileUpload() {
78
98
  minValues = num
79
99
  return out
80
100
  },
81
- max: (num: number = 10) => {
82
- maxValues = num
101
+ /** omit num to allow the most files Discord allows */
102
+ max: (num?: number) => {
103
+ maxValues = num ?? 'all'
104
+ return out
105
+ },
106
+ /** only allow certain file types: 'image', 'video', 'audio' or extensions like '.pdf'. Discord only checks the file extension */
107
+ fileTypes: (...types: FileType[]) => {
108
+ fileTypesVal = types
83
109
  return out
84
110
  },
85
111
  required(required = true) {
@@ -87,8 +113,8 @@ export function fileUpload() {
87
113
  return out
88
114
  },
89
115
  onSubmit: (...funct: CordoFunctRun) => {
90
- if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
91
- console.warn('You are adding onSubmit handlers to a file upload that already has an overridden custom_id. These handlers will not be called.')
116
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
117
+ LibLogger.warn('You are adding onSubmit handlers to a file upload that already has an overridden custom_id. These handlers will not be called.')
92
118
  functVal.push(...funct)
93
119
  return out
94
120
  },
@@ -1,12 +1,20 @@
1
1
  import { ComponentType, createComponent, renderComponentList, type CordoComponent } from '../component'
2
+ import { CountConstants } from '../../lib/constants'
3
+ import { LibLimits } from '../../lib/limits'
2
4
 
3
5
 
4
6
  export function gallery(...items: CordoComponent<'Thumbnail'>[]) {
5
7
  const out = {
6
- ...createComponent('MediaGallery', ({ }) => ({
7
- type: ComponentType.MediaGallery,
8
- items: renderComponentList(items, 'MediaGallery')
9
- })),
8
+ ...createComponent('MediaGallery', ({ hirarchy, attributes }) => {
9
+ const rendered = renderComponentList(items, 'MediaGallery', hirarchy, attributes)
10
+ if (!rendered.length)
11
+ return null
12
+
13
+ return {
14
+ type: ComponentType.MediaGallery,
15
+ items: LibLimits.list(rendered, CountConstants.GALLERY_ITEMS, 'A gallery')
16
+ }
17
+ }),
10
18
  }
11
19
 
12
20
  return out
@@ -1,4 +1,6 @@
1
1
  import { ComponentType, createComponent } from '../component'
2
+ import { MaxLengthConstants } from '../../lib/constants'
3
+ import { LibLimits } from '../../lib/limits'
2
4
 
3
5
 
4
6
  export function image(url: string) {
@@ -14,7 +16,7 @@ export function image(url: string) {
14
16
  url
15
17
  },
16
18
  spoiler,
17
- description
19
+ description: LibLimits.text(description, MaxLengthConstants.MEDIA_DESCRIPTION, 'An image description')
18
20
  })),
19
21
 
20
22
  description: (value: string) => {
@@ -1,24 +1,26 @@
1
1
  import { URL } from 'node:url'
2
- import { ButtonStyle } from 'discord-api-types/v10'
2
+ import { ButtonStyle, type APIEmoji } from 'discord-api-types/v10'
3
3
  import { ComponentType, createComponent } from '../component'
4
4
  import { Hooks } from '../../core/hooks'
5
5
  import { MaxLengthConstants } from '../../lib/constants'
6
+ import { LibLimits } from '../../lib/limits'
7
+ import { LibEmoji } from '../../lib/emoji'
6
8
 
7
9
 
8
10
  export function linkButton(url: string) {
9
11
  let labelVal: string | undefined = undefined
10
- let emojiVal: string | undefined = undefined
12
+ let emojiVal: APIEmoji | undefined = undefined
11
13
 
12
14
 
13
15
  function getLabel() {
14
16
  if (!labelVal)
15
17
  return emojiVal ? '' : new URL(url).hostname.slice(0, MaxLengthConstants.BUTTON_LABEL)
16
18
 
17
- return Hooks.callHook(
19
+ return LibLimits.text(Hooks.callHook(
18
20
  'transformUserFacingText',
19
21
  labelVal,
20
22
  { component: 'Button', position: 'label' }
21
- )?.slice(0, MaxLengthConstants.BUTTON_LABEL)
23
+ ), MaxLengthConstants.BUTTON_LABEL, 'A link button label')
22
24
  }
23
25
 
24
26
  const out = {
@@ -27,15 +29,15 @@ export function linkButton(url: string) {
27
29
  label: getLabel(),
28
30
  emoji: emojiVal,
29
31
  style: ButtonStyle.Link,
30
- url: url?.slice(0, MaxLengthConstants.BUTTON_URL),
32
+ url: LibLimits.url(url, MaxLengthConstants.BUTTON_URL, 'A link button url'),
31
33
  })),
32
34
 
33
35
  label: (text: string) => {
34
36
  labelVal = text
35
37
  return out
36
38
  },
37
- emoji: (emoji: string) => {
38
- emojiVal = emoji
39
+ emoji: (emoji: LibEmoji.Input) => {
40
+ emojiVal = LibEmoji.read(emoji)
39
41
  return out
40
42
  },
41
43
  }
@@ -0,0 +1,107 @@
1
+ import type { APIEmoji } from 'discord-api-types/v10'
2
+ import { ComponentType, createComponent } from '../component'
3
+ import { LibEmoji } from '../../lib/emoji'
4
+ import { Hooks } from '../../core/hooks'
5
+ import type { CordoFunct, CordoFunctRun } from '../../functions'
6
+ import { FunctCompiler } from '../../functions/compiler'
7
+ import { CordoMagic } from '../../core/magic'
8
+ import { LibLogger } from '../../lib/logger'
9
+
10
+
11
+ export const MenuActionItemStyle = {
12
+ Default: 1,
13
+ Danger: 2,
14
+ Link: 3,
15
+ } as const
16
+
17
+ /** An entry in an overflowMenu(). Behaves like a button: when clicked, it executes the Funct assigned to its onClick handler */
18
+ export function menuItem() {
19
+ let labelVal: string | undefined = undefined
20
+ let descriptionVal: string | undefined = undefined
21
+ let emojiVal: APIEmoji | undefined = undefined
22
+ let disabledVal: boolean | undefined = undefined
23
+ let styleVal: number = MenuActionItemStyle.Default
24
+ const functVal: CordoFunct[] = []
25
+ let overrideCustomIdVal: string | undefined = undefined
26
+
27
+ function getLabel() {
28
+ if (!labelVal) {
29
+ if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
30
+ LibLogger.warn('A menu item was rendered without a label provided. Cordo will use a placeholder label.')
31
+ return 'Click'
32
+ }
33
+
34
+ return Hooks.callHook(
35
+ 'transformUserFacingText',
36
+ labelVal,
37
+ { component: 'MenuActionItem', position: 'label' }
38
+ )
39
+ }
40
+
41
+ function getDescription() {
42
+ if (!descriptionVal)
43
+ return undefined
44
+ return Hooks.callHook(
45
+ 'transformUserFacingText',
46
+ descriptionVal,
47
+ { component: 'MenuActionItem', position: 'description' }
48
+ )
49
+ }
50
+
51
+ const advanced = {
52
+ /** Will override cordo's custom_id generation. Not compatible with onClick handlers */
53
+ overrideCustomId(customId: string) {
54
+ if (functVal.length > 0 && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
55
+ LibLogger.warn('You are overriding the custom_id of a menu item that has onClick handlers. This will prevent the onClick handlers from working.')
56
+ overrideCustomIdVal = customId
57
+ return out
58
+ }
59
+ }
60
+
61
+ const out = {
62
+ ...createComponent('MenuActionItem', () => ({
63
+ type: ComponentType.MenuActionItem,
64
+ label: getLabel(),
65
+ description: getDescription(),
66
+ emoji: emojiVal,
67
+ style: styleVal,
68
+ disabled: disabledVal,
69
+ custom_id: overrideCustomIdVal !== undefined
70
+ ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal)
71
+ : FunctCompiler.toCustomId(disabledVal ? [] : functVal)
72
+ })),
73
+
74
+ label: (text: string) => {
75
+ labelVal = text
76
+ return out
77
+ },
78
+ description: (text: string) => {
79
+ descriptionVal = text
80
+ return out
81
+ },
82
+ emoji: (emoji: LibEmoji.Input) => {
83
+ emojiVal = LibEmoji.read(emoji)
84
+ return out
85
+ },
86
+ style: (style: 'default' | 'danger') => {
87
+ styleVal = (style === 'danger')
88
+ ? MenuActionItemStyle.Danger
89
+ : MenuActionItemStyle.Default
90
+ return out
91
+ },
92
+ disabled(disabled = true) {
93
+ disabledVal = disabled
94
+ return out
95
+ },
96
+ onClick: (...funct: CordoFunctRun) => {
97
+ if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
98
+ LibLogger.warn('You are adding onClick handlers to a menu item that already has an overridden custom_id. These handlers will not be called.')
99
+ functVal.push(...funct)
100
+ return out
101
+ },
102
+ /** This namespace contains advanced features you normally do not need */
103
+ advanced
104
+ }
105
+
106
+ return out
107
+ }