cordo 2.20.0 → 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.
- package/package.json +2 -2
- package/src/components/builtin/button.ts +11 -7
- package/src/components/builtin/checkbox-group.ts +55 -33
- package/src/components/builtin/container.ts +37 -7
- package/src/components/builtin/file-upload.ts +50 -24
- package/src/components/builtin/gallery.ts +12 -4
- package/src/components/builtin/image.ts +3 -1
- package/src/components/builtin/link-button.ts +9 -7
- package/src/components/builtin/menu-item.ts +107 -0
- package/src/components/builtin/menu-link.ts +69 -0
- package/src/components/builtin/modal.ts +12 -9
- package/src/components/builtin/overflow-menu.ts +56 -0
- package/src/components/builtin/radio-group.ts +21 -17
- package/src/components/builtin/row.ts +41 -9
- package/src/components/builtin/section.ts +24 -6
- package/src/components/builtin/select-string.ts +59 -32
- package/src/components/builtin/text-input.ts +33 -24
- package/src/components/builtin/text.ts +3 -1
- package/src/components/component.ts +23 -5
- package/src/components/index.ts +7 -0
- package/src/components/limits.ts +156 -0
- package/src/components/mods/debug-id-to-label.ts +1 -1
- package/src/components/mods/debug-print.ts +2 -1
- package/src/components/mods/disable-all-components.ts +17 -5
- package/src/core/files/command.ts +9 -8
- package/src/core/files/config.ts +39 -7
- package/src/core/files/route.ts +12 -4
- package/src/core/gateway.ts +2 -1
- package/src/core/hooks.ts +2 -1
- package/src/core/index.ts +2 -1
- package/src/core/routing/filesystem.ts +2 -1
- package/src/core/routing/resolve.ts +3 -2
- package/src/core/routing/respond.ts +8 -5
- package/src/errors/handle.ts +3 -2
- package/src/functions/compiler.ts +25 -3
- package/src/lib/constants.ts +35 -2
- package/src/lib/limits.ts +98 -0
- package/src/lib/logger.ts +28 -0
- package/src/plugin/djs.ts +2 -1
- package/tsconfig.json +1 -4
- package/src/lib/ids.test.ts +0 -47
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { URL } from 'node:url'
|
|
2
|
+
import type { APIEmoji } from 'discord-api-types/v10'
|
|
3
|
+
import { ComponentType, createComponent } from '../component'
|
|
4
|
+
import { LibEmoji } from '../../lib/emoji'
|
|
5
|
+
import { Hooks } from '../../core/hooks'
|
|
6
|
+
import { MaxLengthConstants } from '../../lib/constants'
|
|
7
|
+
import { LibLimits } from '../../lib/limits'
|
|
8
|
+
import { MenuActionItemStyle } from './menu-item'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/** An entry in an overflowMenu() that opens a URL when clicked */
|
|
12
|
+
export function menuLink(url: string) {
|
|
13
|
+
let labelVal: string | undefined = undefined
|
|
14
|
+
let descriptionVal: string | undefined = undefined
|
|
15
|
+
let emojiVal: APIEmoji | undefined = undefined
|
|
16
|
+
let disabledVal: boolean | undefined = undefined
|
|
17
|
+
|
|
18
|
+
function getLabel() {
|
|
19
|
+
if (!labelVal)
|
|
20
|
+
return new URL(url).hostname
|
|
21
|
+
|
|
22
|
+
return Hooks.callHook(
|
|
23
|
+
'transformUserFacingText',
|
|
24
|
+
labelVal,
|
|
25
|
+
{ component: 'MenuActionItem', position: 'label' }
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getDescription() {
|
|
30
|
+
if (!descriptionVal)
|
|
31
|
+
return undefined
|
|
32
|
+
return Hooks.callHook(
|
|
33
|
+
'transformUserFacingText',
|
|
34
|
+
descriptionVal,
|
|
35
|
+
{ component: 'MenuActionItem', position: 'description' }
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const out = {
|
|
40
|
+
...createComponent('MenuActionItem', () => ({
|
|
41
|
+
type: ComponentType.MenuActionItem,
|
|
42
|
+
label: getLabel(),
|
|
43
|
+
description: getDescription(),
|
|
44
|
+
emoji: emojiVal,
|
|
45
|
+
style: MenuActionItemStyle.Link,
|
|
46
|
+
disabled: disabledVal,
|
|
47
|
+
url: LibLimits.url(url, MaxLengthConstants.BUTTON_URL, 'A menu link url'),
|
|
48
|
+
})),
|
|
49
|
+
|
|
50
|
+
label: (text: string) => {
|
|
51
|
+
labelVal = text
|
|
52
|
+
return out
|
|
53
|
+
},
|
|
54
|
+
description: (text: string) => {
|
|
55
|
+
descriptionVal = text
|
|
56
|
+
return out
|
|
57
|
+
},
|
|
58
|
+
emoji: (emoji: LibEmoji.Input) => {
|
|
59
|
+
emojiVal = LibEmoji.read(emoji)
|
|
60
|
+
return out
|
|
61
|
+
},
|
|
62
|
+
disabled(disabled = true) {
|
|
63
|
+
disabledVal = disabled
|
|
64
|
+
return out
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return out
|
|
69
|
+
}
|
|
@@ -4,6 +4,9 @@ import type { CordoFunct, CordoFunctRun } from '../../functions'
|
|
|
4
4
|
import { FunctCompiler } from '../../functions/compiler'
|
|
5
5
|
import { ComponentType, createComponent, renderComponent, renderComponentList, type CordoComponent, type CordoComponentPayload, type StringComponentType } from '../component'
|
|
6
6
|
import type { CordoModifier } from '../modifier'
|
|
7
|
+
import { MaxLengthConstants } from '../../lib/constants'
|
|
8
|
+
import { LibLimits } from '../../lib/limits'
|
|
9
|
+
import { LibLogger } from '../../lib/logger'
|
|
7
10
|
|
|
8
11
|
|
|
9
12
|
export type AllowedComponents = CordoComponent<'TextDisplay' | 'TextInput' | 'RoleSelect' | 'UserSelect' | 'StringSelect' | 'ChannelSelect' | 'MentionableSelect' | 'Label' | 'FileUpload' | 'RadioGroup' | 'CheckboxGroup' | 'Checkbox'> | CordoModifier
|
|
@@ -21,9 +24,9 @@ function labelize(component: CordoComponent<StringComponentType>, parsed: CordoC
|
|
|
21
24
|
const rendered = renderComponent(component, 'Modal', hirarchy, attributes)
|
|
22
25
|
return {
|
|
23
26
|
type: ComponentType.Label,
|
|
24
|
-
label: rendered?.['modal:label'] ?? parsed.nativeName,
|
|
27
|
+
label: LibLimits.text(rendered?.['modal:label'] ?? parsed.nativeName, MaxLengthConstants.LABEL_LABEL, 'A label in a modal'),
|
|
25
28
|
id: rendered?.['modal:id'],
|
|
26
|
-
description: rendered?.['modal:description'],
|
|
29
|
+
description: LibLimits.text(rendered?.['modal:description'], MaxLengthConstants.LABEL_DESCRIPTION, 'A description in a modal'),
|
|
27
30
|
component: {
|
|
28
31
|
...rendered,
|
|
29
32
|
'modal:label': undefined,
|
|
@@ -42,22 +45,22 @@ export function modal(...components: AllowedComponentArray) {
|
|
|
42
45
|
function getTitle() {
|
|
43
46
|
if (!titleVal) {
|
|
44
47
|
if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
|
|
45
|
-
|
|
48
|
+
LibLogger.warn('A modal was rendered without a title provided. Cordo will use a placeholder title.')
|
|
46
49
|
return 'Modal'
|
|
47
50
|
}
|
|
48
51
|
|
|
49
|
-
return Hooks.callHook(
|
|
52
|
+
return LibLimits.text(Hooks.callHook(
|
|
50
53
|
'transformUserFacingText',
|
|
51
54
|
titleVal,
|
|
52
55
|
{ component: 'Modal', position: 'title' }
|
|
53
|
-
)
|
|
56
|
+
), MaxLengthConstants.MODAL_TITLE, 'A modal title')
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
const advanced = {
|
|
57
60
|
/** Will override cordo's custom_id generation. Not compatible with onSubmit handlers */
|
|
58
61
|
overrideCustomId(customId: string) {
|
|
59
62
|
if (functVal.length > 0 && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
60
|
-
|
|
63
|
+
LibLogger.warn('You are overriding the custom_id of a modal that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
|
|
61
64
|
overrideCustomIdVal = customId
|
|
62
65
|
return out
|
|
63
66
|
}
|
|
@@ -68,7 +71,7 @@ export function modal(...components: AllowedComponentArray) {
|
|
|
68
71
|
type: ComponentType.Modal,
|
|
69
72
|
components: renderComponentList(components.flat(), 'Modal', hirarchy, attributes, labelize),
|
|
70
73
|
title: getTitle(),
|
|
71
|
-
custom_id: overrideCustomIdVal
|
|
74
|
+
custom_id: overrideCustomIdVal !== undefined ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal) : FunctCompiler.toCustomId(functVal)
|
|
72
75
|
})),
|
|
73
76
|
|
|
74
77
|
title: (value: string) => {
|
|
@@ -76,8 +79,8 @@ export function modal(...components: AllowedComponentArray) {
|
|
|
76
79
|
return out
|
|
77
80
|
},
|
|
78
81
|
onSubmit: (...funct: CordoFunctRun) => {
|
|
79
|
-
if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
80
|
-
|
|
82
|
+
if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
83
|
+
LibLogger.warn('You are adding onSubmit handlers to a modal that already has an overridden custom_id. These handlers will not be called.')
|
|
81
84
|
functVal.push(...funct)
|
|
82
85
|
return out
|
|
83
86
|
},
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ComponentType, createComponent, renderComponentList, type CordoComponent } from '../component'
|
|
2
|
+
import type { CordoModifier } from '../modifier'
|
|
3
|
+
import { Hooks } from '../../core/hooks'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
type AllowedComponents = CordoComponent<'MenuActionItem'> | CordoModifier
|
|
7
|
+
|
|
8
|
+
type MenuGroup = {
|
|
9
|
+
label?: string
|
|
10
|
+
items: AllowedComponents[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A "..." button that opens a menu of grouped menuItem() and menuLink() entries when clicked. Can be used like a button or as a section accessory */
|
|
14
|
+
export function overflowMenu() {
|
|
15
|
+
const groups: MenuGroup[] = []
|
|
16
|
+
|
|
17
|
+
function getGroupLabel(label: string | undefined) {
|
|
18
|
+
if (!label)
|
|
19
|
+
return undefined
|
|
20
|
+
return Hooks.callHook(
|
|
21
|
+
'transformUserFacingText',
|
|
22
|
+
label,
|
|
23
|
+
{ component: 'OverflowMenu', position: 'group.label' }
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const out = {
|
|
28
|
+
...createComponent('OverflowMenu', ({ hirarchy, attributes }) => {
|
|
29
|
+
const rendered = groups
|
|
30
|
+
.map(g => ({
|
|
31
|
+
label: getGroupLabel(g.label),
|
|
32
|
+
components: renderComponentList(g.items, 'OverflowMenu', hirarchy, attributes)
|
|
33
|
+
}))
|
|
34
|
+
.filter(g => g.components.length > 0)
|
|
35
|
+
|
|
36
|
+
if (!rendered.length)
|
|
37
|
+
return null
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
type: ComponentType.OverflowMenu,
|
|
41
|
+
groups: rendered
|
|
42
|
+
}
|
|
43
|
+
}),
|
|
44
|
+
|
|
45
|
+
/** Add a group of menu entries, optionally with a label: `.group('Label', ...items)` or `.group(...items)` */
|
|
46
|
+
group(labelOrItem: string | AllowedComponents, ...items: AllowedComponents[]) {
|
|
47
|
+
if (typeof labelOrItem === 'string')
|
|
48
|
+
groups.push({ label: labelOrItem, items })
|
|
49
|
+
else
|
|
50
|
+
groups.push({ items: [ labelOrItem, ...items ] })
|
|
51
|
+
return out
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return out
|
|
56
|
+
}
|
|
@@ -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 RadioGroupOption<Value extends string = string> = ({
|
|
@@ -26,7 +28,7 @@ export function radioGroup<Values extends string = string>() {
|
|
|
26
28
|
function getLabel() {
|
|
27
29
|
if (!labelVal) {
|
|
28
30
|
if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
|
|
29
|
-
|
|
31
|
+
LibLogger.warn('A radio group was rendered without a label provided. Cordo will use a placeholder label.')
|
|
30
32
|
return 'Pick one'
|
|
31
33
|
}
|
|
32
34
|
return Hooks.callHook(
|
|
@@ -47,15 +49,17 @@ export function radioGroup<Values extends string = string>() {
|
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
function getOptions(): RadioGroupOption[] {
|
|
50
|
-
|
|
52
|
+
if (optionsVal.length < CountConstants.RADIO_OPTIONS_MIN)
|
|
53
|
+
LibLimits.warn('invalidComponent', `A radio group needs at least ${CountConstants.RADIO_OPTIONS_MIN} options, this one has ${optionsVal.length}. Discord will reject it.`)
|
|
54
|
+
return LibLimits.list(optionsVal, CountConstants.RADIO_OPTIONS_MAX, 'A radio group').map(o => ({
|
|
51
55
|
...o,
|
|
52
|
-
label: Hooks.callHook('transformUserFacingText', o.label, { component: 'RadioGroup', position: 'option.label' })
|
|
56
|
+
label: LibLimits.text(Hooks.callHook('transformUserFacingText', o.label, { component: 'RadioGroup', position: 'option.label' }), MaxLengthConstants.SELECT_OPTION_LABEL, 'A radio group option label'),
|
|
53
57
|
description: o.description
|
|
54
|
-
? Hooks.callHook('transformUserFacingText', o.description, { component: 'RadioGroup', position: 'option.description' })
|
|
58
|
+
? LibLimits.text(Hooks.callHook('transformUserFacingText', o.description, { component: 'RadioGroup', position: 'option.description' }), MaxLengthConstants.SELECT_OPTION_DESCRIPTION, 'A radio group option description')
|
|
55
59
|
: undefined,
|
|
56
60
|
/** 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 */
|
|
57
|
-
value: overrideCustomIdVal
|
|
58
|
-
? (o.value ?? FunctCompiler.toCustomId([]))
|
|
61
|
+
value: overrideCustomIdVal !== undefined
|
|
62
|
+
? (LibLimits.text(o.value, MaxLengthConstants.SELECT_OPTION_VALUE, 'A radio group option value') ?? FunctCompiler.toCustomId([]))
|
|
59
63
|
: FunctCompiler.toCustomId([
|
|
60
64
|
...(o.onSelect
|
|
61
65
|
? Array.isArray(o.onSelect)
|
|
@@ -73,9 +77,9 @@ export function radioGroup<Values extends string = string>() {
|
|
|
73
77
|
overrideCustomId(customId: string) {
|
|
74
78
|
if (!CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride')) {
|
|
75
79
|
if (functVal.length > 0)
|
|
76
|
-
|
|
80
|
+
LibLogger.warn('You are overriding the custom_id of a radio group that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
|
|
77
81
|
if (ref)
|
|
78
|
-
|
|
82
|
+
LibLogger.warn('You are overriding the custom_id of a radio group that has an as() id. Your as() id will be overridden.')
|
|
79
83
|
}
|
|
80
84
|
overrideCustomIdVal = customId
|
|
81
85
|
return out
|
|
@@ -83,23 +87,23 @@ export function radioGroup<Values extends string = string>() {
|
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
const out = {
|
|
86
|
-
...createComponent('RadioGroup', () => ({
|
|
90
|
+
...createComponent('RadioGroup', ({ hirarchy }) => ({
|
|
87
91
|
type: ComponentType.RadioGroup,
|
|
88
92
|
label: getLabel(),
|
|
89
93
|
description: getDescription(),
|
|
90
94
|
required: requiredVal,
|
|
91
95
|
options: getOptions(),
|
|
92
|
-
custom_id: overrideCustomIdVal
|
|
96
|
+
custom_id: overrideCustomIdVal !== undefined ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal) : FunctCompiler.toCustomId([
|
|
93
97
|
...functVal,
|
|
94
98
|
...(ref ? [ value(ref) ] : [])
|
|
95
99
|
]),
|
|
96
|
-
'modal:label': getLabel(),
|
|
97
|
-
'modal:description': getDescription(),
|
|
100
|
+
'modal:label': hirarchy.includes('Modal') ? getLabel() : undefined,
|
|
101
|
+
'modal:description': hirarchy.includes('Modal') ? getDescription() : undefined,
|
|
98
102
|
})),
|
|
99
103
|
|
|
100
104
|
as: (id: string) => {
|
|
101
|
-
if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
102
|
-
|
|
105
|
+
if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
106
|
+
LibLogger.warn('You are assigning an as() id to a radio group that already has an overridden custom_id. Your provided id will be ignored.')
|
|
103
107
|
ref = id
|
|
104
108
|
return out
|
|
105
109
|
},
|
|
@@ -108,8 +112,8 @@ export function radioGroup<Values extends string = string>() {
|
|
|
108
112
|
return out
|
|
109
113
|
},
|
|
110
114
|
onSubmit: (...funct: CordoFunctRun) => {
|
|
111
|
-
if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
112
|
-
|
|
115
|
+
if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
116
|
+
LibLogger.warn('You are adding onSubmit handlers to a radio group that already has an overridden custom_id. These handlers will not be called.')
|
|
113
117
|
functVal.push(...funct)
|
|
114
118
|
return out
|
|
115
119
|
},
|
|
@@ -1,14 +1,26 @@
|
|
|
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
|
-
type
|
|
6
|
+
type Clickable = CordoComponent<'Button' | 'OverflowMenu'>
|
|
7
|
+
type Select = CordoComponent<'StringSelect'>
|
|
5
8
|
type AllowedComponentArray
|
|
6
|
-
= [
|
|
7
|
-
| [
|
|
8
|
-
| [
|
|
9
|
-
| [
|
|
10
|
-
| [
|
|
9
|
+
= [ Clickable ]
|
|
10
|
+
| [ Clickable, Clickable ]
|
|
11
|
+
| [ Clickable, Clickable, Clickable ]
|
|
12
|
+
| [ Clickable, Clickable, Clickable, Clickable ]
|
|
13
|
+
| [ Clickable, Clickable, Clickable, Clickable, Clickable ]
|
|
14
|
+
| [ Select ]
|
|
11
15
|
|
|
16
|
+
/** a function because component.ts imports this file, ComponentType is not ready when this module loads */
|
|
17
|
+
const isSelect = (type: number) => type === ComponentType.StringSelect
|
|
18
|
+
|| type === ComponentType.UserSelect
|
|
19
|
+
|| type === ComponentType.RoleSelect
|
|
20
|
+
|| type === ComponentType.MentionableSelect
|
|
21
|
+
|| type === ComponentType.ChannelSelect
|
|
22
|
+
|
|
23
|
+
/** An action row holds up to 5 buttons or a single select. Rows that hold more are split into multiple rows */
|
|
12
24
|
export function row(...components: AllowedComponentArray | [ AllowedComponentArray ]) {
|
|
13
25
|
if (Array.isArray(components[0]))
|
|
14
26
|
components = components[0]
|
|
@@ -19,9 +31,29 @@ export function row(...components: AllowedComponentArray | [ AllowedComponentArr
|
|
|
19
31
|
if (!rendered.length)
|
|
20
32
|
return null
|
|
21
33
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
const rows: Array<Record<string, any>[]> = []
|
|
35
|
+
let current: Record<string, any>[] = []
|
|
36
|
+
for (const item of rendered) {
|
|
37
|
+
if (isSelect(item.type)) {
|
|
38
|
+
if (current.length)
|
|
39
|
+
rows.push(current)
|
|
40
|
+
rows.push([ item ])
|
|
41
|
+
current = []
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
current.push(item)
|
|
45
|
+
if (current.length === CountConstants.ROW_BUTTONS) {
|
|
46
|
+
rows.push(current)
|
|
47
|
+
current = []
|
|
48
|
+
}
|
|
25
49
|
}
|
|
50
|
+
if (current.length)
|
|
51
|
+
rows.push(current)
|
|
52
|
+
|
|
53
|
+
if (rows.length > 1)
|
|
54
|
+
LibLimits.warn('invalidLayout', `An action row can hold up to ${CountConstants.ROW_BUTTONS} buttons or a single select, this one held ${rendered.length} components. Cordo split it into ${rows.length} rows.`)
|
|
55
|
+
|
|
56
|
+
const out = rows.map(r => ({ type: ComponentType.ActionRow, components: r }))
|
|
57
|
+
return out.length === 1 ? out[0] : out
|
|
26
58
|
})
|
|
27
59
|
}
|
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
import { ComponentType, createComponent, renderComponent, renderComponentList, type CordoComponent } from '../component'
|
|
2
|
+
import { CountConstants, MaxLengthConstants } from '../../lib/constants'
|
|
3
|
+
import { LibLimits } from '../../lib/limits'
|
|
2
4
|
|
|
3
5
|
|
|
4
6
|
type AllowedComponentsContent = CordoComponent<'TextDisplay'>
|
|
5
|
-
type AllowedComponentsAccessory = CordoComponent<'Thumbnail' | 'Button'>
|
|
7
|
+
type AllowedComponentsAccessory = CordoComponent<'Thumbnail' | 'Button' | 'OverflowMenu'>
|
|
6
8
|
|
|
9
|
+
/** A section shows text next to an accessory (set with decorate). Without an accessory it renders as plain text */
|
|
7
10
|
export function section(...components: AllowedComponentsContent[]) {
|
|
8
11
|
let accessory: AllowedComponentsAccessory | undefined = undefined
|
|
9
12
|
|
|
10
13
|
const out = {
|
|
11
|
-
...createComponent('Section', ({ hirarchy, attributes }) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
...createComponent('Section', ({ hirarchy, attributes }) => {
|
|
15
|
+
const rendered = renderComponentList(components, 'Section', hirarchy, attributes)
|
|
16
|
+
if (!rendered.length)
|
|
17
|
+
return null
|
|
18
|
+
|
|
19
|
+
const renderedAccessory = accessory ? renderComponent(accessory, 'Section', hirarchy, attributes) : null
|
|
20
|
+
if (!renderedAccessory) {
|
|
21
|
+
LibLimits.warn('invalidLayout', 'A section was rendered without an accessory, Discord requires one. Cordo rendered its content as plain text instead.')
|
|
22
|
+
return {
|
|
23
|
+
type: ComponentType.TextDisplay,
|
|
24
|
+
content: LibLimits.text(rendered.map(c => c.content).join('\n'), MaxLengthConstants.TEXT_DISPLAY_CONTENT, 'A text')
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
type: ComponentType.Section,
|
|
30
|
+
components: LibLimits.list(rendered, CountConstants.SECTION_TEXTS, 'A section'),
|
|
31
|
+
accessory: renderedAccessory
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
16
34
|
|
|
17
35
|
decorate(component: AllowedComponentsAccessory) {
|
|
18
36
|
accessory = component
|
|
@@ -3,8 +3,10 @@ import { ComponentType, createComponent } from '../component'
|
|
|
3
3
|
import { Hooks } from '../../core/hooks'
|
|
4
4
|
import { value, type CordoFunct, type CordoFunctRun } from '../../functions'
|
|
5
5
|
import { FunctCompiler } from '../../functions/compiler'
|
|
6
|
-
import { MaxLengthConstants } from '../../lib/constants'
|
|
6
|
+
import { CountConstants, MaxLengthConstants } from '../../lib/constants'
|
|
7
|
+
import { LibLimits } from '../../lib/limits'
|
|
7
8
|
import { CordoMagic } from '../../core/magic'
|
|
9
|
+
import { LibLogger } from '../../lib/logger'
|
|
8
10
|
|
|
9
11
|
|
|
10
12
|
type SelectMenuOption<Value extends string = string> = Omit<APISelectMenuOption, 'value'> & ({
|
|
@@ -17,7 +19,8 @@ export function selectString<Values extends string = string>() {
|
|
|
17
19
|
let labelVal: string | undefined = undefined
|
|
18
20
|
let descriptionVal: string | undefined = undefined
|
|
19
21
|
let minValues: number | undefined = undefined
|
|
20
|
-
let maxValues: number | undefined = undefined
|
|
22
|
+
let maxValues: number | 'all' | undefined = undefined
|
|
23
|
+
let requiredVal: boolean | undefined = undefined
|
|
21
24
|
let optionsVal: SelectMenuOption[] = []
|
|
22
25
|
let disabledVal: boolean | undefined = undefined
|
|
23
26
|
let ref: string | undefined = undefined
|
|
@@ -27,17 +30,17 @@ export function selectString<Values extends string = string>() {
|
|
|
27
30
|
function getPlaceholder() {
|
|
28
31
|
if (!placeholderVal)
|
|
29
32
|
return undefined
|
|
30
|
-
return Hooks.callHook(
|
|
33
|
+
return LibLimits.text(Hooks.callHook(
|
|
31
34
|
'transformUserFacingText',
|
|
32
35
|
placeholderVal,
|
|
33
36
|
{ component: 'StringSelect', position: 'placeholder' }
|
|
34
|
-
)
|
|
37
|
+
), MaxLengthConstants.SELECT_PLACEHOLDER, 'A string select placeholder')
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
function getLabel() {
|
|
38
41
|
if (!labelVal) {
|
|
39
42
|
if (!CordoMagic.getConfig()?.omitWarnings.includes('placeholderTextAppearance'))
|
|
40
|
-
|
|
43
|
+
LibLogger.warn('A string select was rendered without a label provided. Cordo will use a placeholder label.')
|
|
41
44
|
return 'Your Response'
|
|
42
45
|
}
|
|
43
46
|
return Hooks.callHook(
|
|
@@ -58,15 +61,15 @@ export function selectString<Values extends string = string>() {
|
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
function getOptions(): SelectMenuOption[] {
|
|
61
|
-
return
|
|
64
|
+
return LibLimits.list(optionsVal, CountConstants.SELECT_OPTIONS, 'A string select').map(o => ({
|
|
62
65
|
...o,
|
|
63
|
-
label: Hooks.callHook('transformUserFacingText', o.label, { component: 'StringSelect', position: 'option.label' })
|
|
66
|
+
label: LibLimits.text(Hooks.callHook('transformUserFacingText', o.label, { component: 'StringSelect', position: 'option.label' }), MaxLengthConstants.SELECT_OPTION_LABEL, 'A string select option label'),
|
|
64
67
|
description: o.description
|
|
65
|
-
? Hooks.callHook('transformUserFacingText', o.description, { component: 'StringSelect', position: 'option.description' })
|
|
68
|
+
? LibLimits.text(Hooks.callHook('transformUserFacingText', o.description, { component: 'StringSelect', position: 'option.description' }), MaxLengthConstants.SELECT_OPTION_DESCRIPTION, 'A string select option description')
|
|
66
69
|
: undefined,
|
|
67
70
|
/** 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 */
|
|
68
|
-
value: overrideCustomIdVal
|
|
69
|
-
? (o.value ?? FunctCompiler.toCustomId([]))
|
|
71
|
+
value: overrideCustomIdVal !== undefined
|
|
72
|
+
? (LibLimits.text(o.value, MaxLengthConstants.SELECT_OPTION_VALUE, 'A string select option value') ?? FunctCompiler.toCustomId([]))
|
|
70
73
|
: FunctCompiler.toCustomId([
|
|
71
74
|
...(o.onClick
|
|
72
75
|
? Array.isArray(o.onClick)
|
|
@@ -84,9 +87,9 @@ export function selectString<Values extends string = string>() {
|
|
|
84
87
|
overrideCustomId(customId: string) {
|
|
85
88
|
if (!CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride')) {
|
|
86
89
|
if (functVal.length > 0)
|
|
87
|
-
|
|
90
|
+
LibLogger.warn('You are overriding the custom_id of a string select that has onSubmit handlers. This will prevent the onSubmit handlers from working.')
|
|
88
91
|
if (ref)
|
|
89
|
-
|
|
92
|
+
LibLogger.warn('You are overriding the custom_id of a string select that has an as() id. Your as() id will be overridden.')
|
|
90
93
|
}
|
|
91
94
|
overrideCustomIdVal = customId
|
|
92
95
|
return out
|
|
@@ -94,24 +97,41 @@ export function selectString<Values extends string = string>() {
|
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
const out = {
|
|
97
|
-
...createComponent('StringSelect', () =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
100
|
+
...createComponent('StringSelect', ({ hirarchy }) => {
|
|
101
|
+
const inModal = hirarchy.includes('Modal')
|
|
102
|
+
const range = LibLimits.selectionRange({
|
|
103
|
+
where: 'a string select',
|
|
104
|
+
min: minValues,
|
|
105
|
+
max: maxValues,
|
|
106
|
+
limit: Math.min(optionsVal.length, CountConstants.SELECT_VALUES),
|
|
107
|
+
defaultMax: 1,
|
|
108
|
+
required: requiredVal,
|
|
109
|
+
inModal
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
if (inModal && disabledVal === true)
|
|
113
|
+
LibLimits.warn('unsupportedAttribute', 'A string select inside a modal cannot be disabled. Cordo left out the disabled attribute.')
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
type: ComponentType.StringSelect,
|
|
117
|
+
placeholder: getPlaceholder(),
|
|
118
|
+
min_values: range.min,
|
|
119
|
+
max_values: range.max,
|
|
120
|
+
disabled: inModal ? undefined : disabledVal,
|
|
121
|
+
required: inModal ? range.required : undefined,
|
|
122
|
+
options: getOptions(),
|
|
123
|
+
custom_id: overrideCustomIdVal !== undefined ? FunctCompiler.checkCustomIdOverride(overrideCustomIdVal) : FunctCompiler.toCustomId([
|
|
124
|
+
...((disabledVal && !inModal) ? [] : functVal),
|
|
125
|
+
...(ref ? [ value(ref) ] : [])
|
|
126
|
+
]),
|
|
127
|
+
'modal:label': hirarchy.includes('Modal') ? getLabel() : undefined,
|
|
128
|
+
'modal:description': hirarchy.includes('Modal') ? getDescription() : undefined,
|
|
129
|
+
}
|
|
130
|
+
}),
|
|
111
131
|
|
|
112
132
|
as: (id: string) => {
|
|
113
|
-
if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
114
|
-
|
|
133
|
+
if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
134
|
+
LibLogger.warn('You are assigning an as() id to a string select that already has an overridden custom_id. Your provided id will be ignored.')
|
|
115
135
|
ref = id
|
|
116
136
|
return out
|
|
117
137
|
},
|
|
@@ -123,17 +143,24 @@ export function selectString<Values extends string = string>() {
|
|
|
123
143
|
minValues = num
|
|
124
144
|
return out
|
|
125
145
|
},
|
|
126
|
-
|
|
127
|
-
|
|
146
|
+
/** omit num to allow selecting all options */
|
|
147
|
+
max: (num?: number) => {
|
|
148
|
+
maxValues = num ?? 'all'
|
|
128
149
|
return out
|
|
129
150
|
},
|
|
151
|
+
/** messages only. Cordo leaves this out inside modals */
|
|
130
152
|
disabled(disabled = true) {
|
|
131
153
|
disabledVal = disabled
|
|
132
154
|
return out
|
|
133
155
|
},
|
|
156
|
+
/** modals only. Discord ignores this in messages */
|
|
157
|
+
required(required = true) {
|
|
158
|
+
requiredVal = required
|
|
159
|
+
return out
|
|
160
|
+
},
|
|
134
161
|
onSubmit: (...funct: CordoFunctRun) => {
|
|
135
|
-
if (overrideCustomIdVal && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
136
|
-
|
|
162
|
+
if (overrideCustomIdVal !== undefined && !CordoMagic.getConfig()?.omitWarnings.includes('customIdOverride'))
|
|
163
|
+
LibLogger.warn('You are adding onSubmit handlers to a string select that already has an overridden custom_id. These handlers will not be called.')
|
|
137
164
|
functVal.push(...funct)
|
|
138
165
|
return out
|
|
139
166
|
},
|