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.
- package/package.json +1 -1
- 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
package/src/core/gateway.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ComponentType, InteractionResponseType, InteractionType, type APIApplic
|
|
|
2
2
|
import { FunctCompiler } from '../functions/compiler'
|
|
3
3
|
import { FunctInternals } from '../functions/funct'
|
|
4
4
|
import { MissingContextError } from '../errors/builtin/missing-context'
|
|
5
|
+
import { LibLogger } from '../lib/logger'
|
|
5
6
|
import { InteractionInternals, type CordoInteraction } from './interaction'
|
|
6
7
|
import { CordoMagic } from './magic'
|
|
7
8
|
import type { LockfileInternals } from './files/lockfile'
|
|
@@ -167,7 +168,7 @@ export namespace CordoGateway {
|
|
|
167
168
|
if (!config)
|
|
168
169
|
throw new MissingContextError('Could not respond to interaction, no config found in context.')
|
|
169
170
|
if (!config.client.id) {
|
|
170
|
-
|
|
171
|
+
LibLogger.warn('No client id provided in config. Cannot respond to interactions.')
|
|
171
172
|
return null
|
|
172
173
|
}
|
|
173
174
|
|
package/src/core/hooks.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LibLogger } from '../lib/logger'
|
|
1
2
|
import type { CordoConfig } from './files/config'
|
|
2
3
|
import { CordoMagic } from './magic'
|
|
3
4
|
|
|
@@ -20,7 +21,7 @@ export namespace Hooks {
|
|
|
20
21
|
|
|
21
22
|
const contextConfig = CordoMagic.getConfig()
|
|
22
23
|
if (!contextConfig) {
|
|
23
|
-
|
|
24
|
+
LibLogger.warn(`Calling hook '${hookName}' failed, no config provided and no config found in context.`)
|
|
24
25
|
return value as any
|
|
25
26
|
}
|
|
26
27
|
|
package/src/core/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { CommandInternals } from './files/command'
|
|
|
13
13
|
export { type DynamicTypes } from './dynamic-types'
|
|
14
14
|
export { type CordoApiResponse, type HttpMethod } from './gateway'
|
|
15
15
|
export { type CordoConfig, defineCordoConfig } from './files/config'
|
|
16
|
+
export { type CordoLogger } from '../lib/logger'
|
|
16
17
|
export { type CordoRoute, type RouteRequest, defineCordoRoute, assertCordoRequest } from './files/route'
|
|
17
18
|
export { type CordoCommand, defineCordoCommand } from './files/command'
|
|
18
19
|
export { type CordoErrorBoundary, defineCordoErrorBoundary } from './files/error-boundary'
|
|
@@ -23,7 +24,7 @@ export { type CordoInteraction } from './interaction'
|
|
|
23
24
|
async function mountCordo(configOverrides?: PartialDeep<CordoConfig>) {
|
|
24
25
|
const fileConfig = await ConfigInternals.readAndParseConfig()
|
|
25
26
|
CordoMagic.globalConfig = configOverrides
|
|
26
|
-
? defu(configOverrides, fileConfig) as ParsedCordoConfig
|
|
27
|
+
? { ...defu(configOverrides, fileConfig), logger: configOverrides.logger ?? fileConfig.logger } as ParsedCordoConfig
|
|
27
28
|
: fileConfig
|
|
28
29
|
|
|
29
30
|
if (!CordoMagic.globalConfig.headless) {
|
|
@@ -6,6 +6,7 @@ import { LockfileInternals } from '../files/lockfile'
|
|
|
6
6
|
import { LibIds } from '../../lib/ids'
|
|
7
7
|
import { CommandInternals, type AutocompleteHandler, type CordoCommand } from '../files/command'
|
|
8
8
|
import type { ParsedCordoConfig } from '../files/config'
|
|
9
|
+
import { LibLogger } from '../../lib/logger'
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
export namespace RoutingFilesystem {
|
|
@@ -57,7 +58,7 @@ export namespace RoutingFilesystem {
|
|
|
57
58
|
const command = await CommandInternals.readCommand(join(treeRoot, item.name))
|
|
58
59
|
if (command) {
|
|
59
60
|
if (!treeRoot.includes(config.defaults.commandRoutePrefix ?? 'command'))
|
|
60
|
-
|
|
61
|
+
LibLogger.warn(`File ${join(treeRoot, item.name)} defined a command but is not in the command route prefix folder (${config.defaults.commandRoutePrefix ?? 'command'}). The command will probably not trigger the configured route when run.`)
|
|
61
62
|
|
|
62
63
|
out.commands.push({
|
|
63
64
|
path: [ item.name ],
|
|
@@ -4,6 +4,7 @@ import { FunctCompiler } from '../../functions/compiler'
|
|
|
4
4
|
import { Hooks } from '../hooks'
|
|
5
5
|
import type { CordoInteraction } from '../interaction'
|
|
6
6
|
import { CordoMagic } from '../magic'
|
|
7
|
+
import { LibLogger } from '../../lib/logger'
|
|
7
8
|
|
|
8
9
|
const DefaultFileName = 'index'
|
|
9
10
|
|
|
@@ -78,7 +79,7 @@ export namespace RoutingResolve {
|
|
|
78
79
|
|
|
79
80
|
const start = resolvePathDots(path, startingPoint)
|
|
80
81
|
if (start === null) {
|
|
81
|
-
|
|
82
|
+
LibLogger.warn(`Invalid route '${path}', it climbs above the routes folder or contains too many dots.`)
|
|
82
83
|
return { routeId: FunctCompiler.InvalidRoutePlaceholderId, args: [] }
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -121,7 +122,7 @@ export namespace RoutingResolve {
|
|
|
121
122
|
})
|
|
122
123
|
|
|
123
124
|
if (options.length === 0) {
|
|
124
|
-
|
|
125
|
+
LibLogger.warn(`Could not find a route for '${path}'.`)
|
|
125
126
|
return { routeId: FunctCompiler.InvalidRoutePlaceholderId, args: [] }
|
|
126
127
|
}
|
|
127
128
|
|
|
@@ -8,6 +8,7 @@ import { CordoGateway } from '../gateway'
|
|
|
8
8
|
import { FunctInternals } from '../../functions/funct'
|
|
9
9
|
import { goto, run } from '../../functions'
|
|
10
10
|
import { isModifier, readModifier } from '../../components/modifier'
|
|
11
|
+
import { ComponentLimits } from '../../components/limits'
|
|
11
12
|
import { RoutingResolve } from './resolve'
|
|
12
13
|
|
|
13
14
|
|
|
@@ -56,7 +57,7 @@ export namespace RoutingRespond {
|
|
|
56
57
|
return {
|
|
57
58
|
type,
|
|
58
59
|
data: {
|
|
59
|
-
components: renderComponentList(response, null, [], {}),
|
|
60
|
+
components: ComponentLimits.enforceMessageLimits(renderComponentList(response, null, [], {})),
|
|
60
61
|
flags: (1 << 15) | (opts.isPrivate ? MessageFlags.Ephemeral : 0)
|
|
61
62
|
}
|
|
62
63
|
}
|
|
@@ -74,7 +75,7 @@ export namespace RoutingRespond {
|
|
|
74
75
|
data: {
|
|
75
76
|
custom_id: rendered.custom_id,
|
|
76
77
|
title: rendered.title,
|
|
77
|
-
components: rendered.components,
|
|
78
|
+
components: ComponentLimits.enforceModalLimits(rendered.components),
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
}
|
|
@@ -121,9 +122,11 @@ export namespace RoutingRespond {
|
|
|
121
122
|
: (interaction.type === InteractionType.MessageComponent)
|
|
122
123
|
? (interaction.data.component_type === ComponentType.Button)
|
|
123
124
|
? 'button'
|
|
124
|
-
: (interaction.data.component_type === ComponentType.
|
|
125
|
-
? '
|
|
126
|
-
:
|
|
125
|
+
: ((interaction.data.component_type as number) === ComponentType.MenuActionItem)
|
|
126
|
+
? 'menu'
|
|
127
|
+
: (interaction.data.component_type === ComponentType.StringSelect)
|
|
128
|
+
? 'select'
|
|
129
|
+
: null
|
|
127
130
|
: null
|
|
128
131
|
|
|
129
132
|
if (!source)
|
package/src/errors/handle.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { RoutingResolve } from "../core/routing/resolve"
|
|
|
3
3
|
import type { CordoErrorHandler } from "../core/files/error-boundary"
|
|
4
4
|
import type { CordoInteraction, RouteRequest } from "../core"
|
|
5
5
|
import { Hooks } from "../core/hooks"
|
|
6
|
+
import { LibLogger } from "../lib/logger"
|
|
6
7
|
import { CordoError } from "./cordo-error"
|
|
7
8
|
|
|
8
9
|
|
|
@@ -52,7 +53,7 @@ export namespace HandleErrors {
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
if (error instanceof CordoError)
|
|
55
|
-
|
|
56
|
+
LibLogger.error(`No error handler found for ${error.name}: ${error.message}\n${error.stack ?? ''}`)
|
|
56
57
|
else
|
|
57
58
|
throw error
|
|
58
59
|
}
|
|
@@ -66,7 +67,7 @@ export namespace HandleErrors {
|
|
|
66
67
|
await handler(initialError, request)
|
|
67
68
|
} catch (error) {
|
|
68
69
|
if (error instanceof Error)
|
|
69
|
-
handleErrorStack(error, handlers.slice(1), request)
|
|
70
|
+
await handleErrorStack(error, handlers.slice(1), request)
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
|
|
@@ -4,6 +4,9 @@ import { CordoMagic } from "../core/magic"
|
|
|
4
4
|
import { RoutingResolve } from "../core/routing/resolve"
|
|
5
5
|
import { LibIds } from "../lib/ids"
|
|
6
6
|
import { LibUtils } from "../lib/utils"
|
|
7
|
+
import { LibLimits } from "../lib/limits"
|
|
8
|
+
import { MaxLengthConstants } from "../lib/constants"
|
|
9
|
+
import { LibLogger } from "../lib/logger"
|
|
7
10
|
import { FunctInternals, type CordoFunct } from "./funct"
|
|
8
11
|
|
|
9
12
|
|
|
@@ -29,7 +32,7 @@ export namespace FunctCompiler {
|
|
|
29
32
|
|
|
30
33
|
if (CordoMagic.getConfig()?.headless) {
|
|
31
34
|
if (list.length > 0 && !CordoMagic.getConfig()?.omitWarnings.includes('headlessCustomIdGeneration'))
|
|
32
|
-
|
|
35
|
+
LibLogger.warn('You are rendering a component with functs assigned while in headless mode. Your functs will not register.')
|
|
33
36
|
return NoopIndicator + '_headless_' + randomBytes(6).toString('hex')
|
|
34
37
|
}
|
|
35
38
|
|
|
@@ -73,7 +76,7 @@ export namespace FunctCompiler {
|
|
|
73
76
|
// encode arguments
|
|
74
77
|
const lut = CordoMagic.getLockfile()?.lut
|
|
75
78
|
if (!lut)
|
|
76
|
-
|
|
79
|
+
LibLogger.warn('toCustomId called without a lockfile.')
|
|
77
80
|
// throw new MissingContextError('toCustomId failed, no lockfile found in context.')
|
|
78
81
|
|
|
79
82
|
let argusStr = ''
|
|
@@ -100,7 +103,26 @@ export namespace FunctCompiler {
|
|
|
100
103
|
|
|
101
104
|
// build final id
|
|
102
105
|
const flagsStr = flags.map(f => LibIds.stringify(f, 1)).join('')
|
|
103
|
-
|
|
106
|
+
const out = `${flagsStr}${FunctVersion}${command.join('')}${argusStr}${idc}`
|
|
107
|
+
if (out.length > MaxLengthConstants.CUSTOM_ID) {
|
|
108
|
+
// a cut off id would route to the wrong place, so we render a component that does nothing instead
|
|
109
|
+
LibLimits.warn('invalidCustomId', `A generated custom_id is ${out.length} characters long, Discord allows ${MaxLengthConstants.CUSTOM_ID}. Use shorter route params or values (or register long constants). The component will do nothing when used.`)
|
|
110
|
+
return NoopIndicator + idc
|
|
111
|
+
}
|
|
112
|
+
return out
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** checks a custom_id set through overrideCustomId: cuts it to 100 characters and replaces empty ones with a noop id */
|
|
116
|
+
export function checkCustomIdOverride(customId: string): string {
|
|
117
|
+
if (!customId) {
|
|
118
|
+
LibLimits.warn('invalidCustomId', 'An overridden custom_id is empty, Discord requires at least 1 character. The component will do nothing when used.')
|
|
119
|
+
return toCustomId([])
|
|
120
|
+
}
|
|
121
|
+
if (customId.length > MaxLengthConstants.CUSTOM_ID) {
|
|
122
|
+
LibLimits.warn('invalidCustomId', `An overridden custom_id is ${customId.length} characters long, Discord allows ${MaxLengthConstants.CUSTOM_ID}. Cordo cut it off.`)
|
|
123
|
+
return customId.slice(0, MaxLengthConstants.CUSTOM_ID)
|
|
124
|
+
}
|
|
125
|
+
return customId
|
|
104
126
|
}
|
|
105
127
|
|
|
106
128
|
function parseArg(arg: string, parsedArguments: string[]) {
|
package/src/lib/constants.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/** Limits set by Discord. See https://docs.discord.com/developers/components/reference */
|
|
3
2
|
export namespace MaxLengthConstants {
|
|
4
3
|
|
|
5
4
|
export const BUTTON_LABEL = 80
|
|
6
5
|
export const BUTTON_URL = 512
|
|
7
6
|
export const SELECT_OPTION_LABEL = 100
|
|
8
7
|
export const SELECT_OPTION_DESCRIPTION = 100
|
|
8
|
+
export const SELECT_OPTION_VALUE = 100
|
|
9
|
+
export const SELECT_PLACEHOLDER = 150
|
|
10
|
+
export const TEXT_INPUT_PLACEHOLDER = 100
|
|
11
|
+
export const TEXT_INPUT_VALUE = 4000
|
|
12
|
+
export const TEXT_DISPLAY_CONTENT = 4000
|
|
13
|
+
export const LABEL_LABEL = 45
|
|
14
|
+
export const LABEL_DESCRIPTION = 100
|
|
15
|
+
export const MODAL_TITLE = 45
|
|
16
|
+
export const MEDIA_DESCRIPTION = 1024
|
|
17
|
+
export const CUSTOM_ID = 100
|
|
9
18
|
export const COMMAND_NAME = 32
|
|
10
19
|
|
|
11
20
|
}
|
|
21
|
+
|
|
22
|
+
export namespace CountConstants {
|
|
23
|
+
|
|
24
|
+
/** total components in a message, nested ones included */
|
|
25
|
+
export const MESSAGE_COMPONENTS = 40
|
|
26
|
+
/** combined content of all text displays in a message or modal */
|
|
27
|
+
export const TOTAL_TEXT_LENGTH = 4000
|
|
28
|
+
export const ROW_BUTTONS = 5
|
|
29
|
+
export const SELECT_OPTIONS = 25
|
|
30
|
+
export const SELECT_VALUES = 25
|
|
31
|
+
export const RADIO_OPTIONS_MIN = 2
|
|
32
|
+
export const RADIO_OPTIONS_MAX = 10
|
|
33
|
+
export const CHECKBOX_OPTIONS_MIN = 1
|
|
34
|
+
export const CHECKBOX_OPTIONS_MAX = 10
|
|
35
|
+
export const FILE_UPLOAD_VALUES = 10
|
|
36
|
+
export const FILE_UPLOAD_TYPES = 10
|
|
37
|
+
export const SECTION_TEXTS = 3
|
|
38
|
+
/** top level components (labels and texts) in a modal */
|
|
39
|
+
export const MODAL_COMPONENTS_MIN = 1
|
|
40
|
+
export const MODAL_COMPONENTS_MAX = 5
|
|
41
|
+
export const GALLERY_ITEMS = 10
|
|
42
|
+
export const ACCENT_COLOR_MAX = 0xFFFFFF
|
|
43
|
+
|
|
44
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { CordoConfig } from '../core/files/config'
|
|
2
|
+
import { CordoMagic } from '../core/magic'
|
|
3
|
+
import { LibLogger } from './logger'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export type CordoWarning = CordoConfig['omitWarnings'][number]
|
|
7
|
+
|
|
8
|
+
/** Helpers to keep rendered components within Discord's limits, warning whenever Cordo has to change something */
|
|
9
|
+
export namespace LibLimits {
|
|
10
|
+
|
|
11
|
+
export function warn(key: CordoWarning, message: string) {
|
|
12
|
+
if (CordoMagic.getConfig()?.omitWarnings.includes(key))
|
|
13
|
+
return
|
|
14
|
+
LibLogger.warn(`[cordo:${key}] ${message}`)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** cuts text that is longer than max */
|
|
18
|
+
export function text<T extends string | undefined | null>(value: T, max: number, where: string): T {
|
|
19
|
+
if (typeof value !== 'string' || value.length <= max)
|
|
20
|
+
return value
|
|
21
|
+
warn('truncatedText', `${where} is ${value.length} characters long, Discord allows ${max}. Cordo cut it off.`)
|
|
22
|
+
return value.slice(0, max) as T
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** cuts lists that are longer than max */
|
|
26
|
+
export function list<T>(value: T[], max: number, where: string): T[] {
|
|
27
|
+
if (value.length <= max)
|
|
28
|
+
return value
|
|
29
|
+
warn('truncatedList', `${where} has ${value.length} entries, Discord allows ${max}. Cordo left out the rest.`)
|
|
30
|
+
return value.slice(0, max)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** clamps a number into [min, max]. Undefined stays undefined */
|
|
34
|
+
export function clamp(value: number | undefined, min: number, max: number, where: string): number | undefined {
|
|
35
|
+
if (value === undefined)
|
|
36
|
+
return undefined
|
|
37
|
+
const clamped = Math.min(Math.max(value, min), max)
|
|
38
|
+
if (clamped !== value)
|
|
39
|
+
warn('clampedValue', `${where} is ${value}, Discord allows ${min} to ${max}. Cordo changed it to ${clamped}.`)
|
|
40
|
+
return clamped
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** warns about urls longer than max but keeps them, a cut off url would be broken */
|
|
44
|
+
export function url<T extends string | undefined | null>(value: T, max: number, where: string): T {
|
|
45
|
+
if (typeof value === 'string' && value.length > max)
|
|
46
|
+
warn('longUrl', `${where} is ${value.length} characters long, Discord allows ${max}. Discord will reject this message.`)
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Brings min_values, max_values and required of selects, checkbox groups and file uploads into a state Discord accepts:
|
|
52
|
+
* - both within their limits
|
|
53
|
+
* - min not above max: if max was not set it is raised to min, otherwise min is lowered to max
|
|
54
|
+
* - (modals only) min 0 requires required to be false: if required was not set it becomes false, otherwise min becomes 1
|
|
55
|
+
*/
|
|
56
|
+
export function selectionRange(opts: {
|
|
57
|
+
where: string
|
|
58
|
+
min: number | undefined
|
|
59
|
+
/** 'all' means as many as the limit allows */
|
|
60
|
+
max: number | 'all' | undefined
|
|
61
|
+
/** the highest value allowed for min and max, e.g. the option count */
|
|
62
|
+
limit: number
|
|
63
|
+
/** what discord uses for max when it is omitted */
|
|
64
|
+
defaultMax: number
|
|
65
|
+
required: boolean | undefined
|
|
66
|
+
/** the rule about required only applies in modals */
|
|
67
|
+
inModal: boolean
|
|
68
|
+
}) {
|
|
69
|
+
const limit = Math.max(opts.limit, 1)
|
|
70
|
+
let max = (opts.max === 'all')
|
|
71
|
+
? limit
|
|
72
|
+
: clamp(opts.max, 1, limit, `max_values of ${opts.where}`)
|
|
73
|
+
let min = clamp(opts.min, 0, limit, `min_values of ${opts.where}`)
|
|
74
|
+
let required = opts.required
|
|
75
|
+
|
|
76
|
+
const effectiveMax = max ?? Math.min(opts.defaultMax, limit)
|
|
77
|
+
if (min !== undefined && min > effectiveMax) {
|
|
78
|
+
if (opts.max === undefined) {
|
|
79
|
+
max = min
|
|
80
|
+
} else {
|
|
81
|
+
warn('clampedValue', `min_values of ${opts.where} is ${min} but max_values is ${effectiveMax}. Cordo lowered min_values to ${effectiveMax}.`)
|
|
82
|
+
min = effectiveMax
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (opts.inModal && min === 0) {
|
|
87
|
+
if (required === undefined) {
|
|
88
|
+
required = false
|
|
89
|
+
} else if (required) {
|
|
90
|
+
warn('clampedValue', `min_values of ${opts.where} is 0 but it is required. Cordo raised min_values to 1.`)
|
|
91
|
+
min = 1
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { min, max, required }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CordoMagic } from '../core/magic'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The logger cordo writes to. Any object with these four methods works, which includes console, consola, winston, pino, bunyan, log4js and loglevel.
|
|
6
|
+
* Cordo always passes a single, already formatted string, as some loggers (e.g. pino) treat extra arguments as format values.
|
|
7
|
+
*/
|
|
8
|
+
export type CordoLogger = {
|
|
9
|
+
debug(message: string): void
|
|
10
|
+
info(message: string): void
|
|
11
|
+
warn(message: string): void
|
|
12
|
+
error(message: string): void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export namespace LibLogger {
|
|
16
|
+
|
|
17
|
+
/** the configured logger, or console if cordo is not mounted yet */
|
|
18
|
+
export function get(): CordoLogger {
|
|
19
|
+
return CordoMagic.getConfig()?.logger ?? console
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// always call through the logger object, loggers like winston and pino need `this`
|
|
23
|
+
export const debug = (message: string) => get().debug(message)
|
|
24
|
+
export const info = (message: string) => get().info(message)
|
|
25
|
+
export const warn = (message: string) => get().warn(message)
|
|
26
|
+
export const error = (message: string) => get().error(message)
|
|
27
|
+
|
|
28
|
+
}
|
package/src/plugin/djs.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Client, Events } from 'discord.js'
|
|
2
2
|
import { Cordo } from '../core'
|
|
3
3
|
import { PluginConfigurationError } from '../errors'
|
|
4
|
+
import { LibLogger } from '../lib/logger'
|
|
4
5
|
|
|
5
6
|
export function useWithDiscordJs(client: Client) {
|
|
6
7
|
if (!client)
|
|
@@ -13,6 +14,6 @@ export function useWithDiscordJs(client: Client) {
|
|
|
13
14
|
|
|
14
15
|
setTimeout(() => {
|
|
15
16
|
if (client.listeners(Events.InteractionCreate).length)
|
|
16
|
-
|
|
17
|
+
LibLogger.warn('You are using both the Cordo and Discord.js interaction listeners. This may cause issues with your routes.')
|
|
17
18
|
}, 50)
|
|
18
19
|
}
|
package/tsconfig.json
CHANGED
package/src/lib/ids.test.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test"
|
|
2
|
-
import { LibIds } from "./ids"
|
|
3
|
-
|
|
4
|
-
test("1000 random numbers with parse single", () => {
|
|
5
|
-
for (let i = 0; i < 1000; i++) {
|
|
6
|
-
const id = Math.floor(Math.random() * 64)
|
|
7
|
-
const encoded = LibIds.stringify(id, 1)
|
|
8
|
-
const decoded = LibIds.parseSingle(encoded)
|
|
9
|
-
expect(decoded).toBe(id)
|
|
10
|
-
}
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
test("1000 random numbers with normal parse and 1 digit", () => {
|
|
14
|
-
for (let i = 0; i < 1000; i++) {
|
|
15
|
-
const id = Math.floor(Math.random() * 64)
|
|
16
|
-
const encoded = LibIds.stringify(id, 1)
|
|
17
|
-
const decoded = LibIds.parse(encoded)
|
|
18
|
-
expect(decoded).toBe(id)
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test("1000 random numbers with normal parse and 2 digits", () => {
|
|
23
|
-
for (let i = 0; i < 1000; i++) {
|
|
24
|
-
const id = Math.floor(Math.random() * 64 * 64)
|
|
25
|
-
const encoded = LibIds.stringify(id, 2)
|
|
26
|
-
const decoded = LibIds.parse(encoded)
|
|
27
|
-
expect(decoded).toBe(id)
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test("1000 random numbers with normal parse and 3 digits", () => {
|
|
32
|
-
for (let i = 0; i < 1000; i++) {
|
|
33
|
-
const id = Math.floor(Math.random() * 64 * 64 * 64)
|
|
34
|
-
const encoded = LibIds.stringify(id, 3)
|
|
35
|
-
const decoded = LibIds.parse(encoded)
|
|
36
|
-
expect(decoded).toBe(id)
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("1000 random numbers with normal parse and 4 digits", () => {
|
|
41
|
-
for (let i = 0; i < 1000; i++) {
|
|
42
|
-
const id = Math.floor(Math.random() * 64 * 64 * 64 * 64)
|
|
43
|
-
const encoded = LibIds.stringify(id, 4)
|
|
44
|
-
const decoded = LibIds.parse(encoded)
|
|
45
|
-
expect(decoded).toBe(id)
|
|
46
|
-
}
|
|
47
|
-
})
|