@open-discord-bots/framework 0.2.16 → 0.3.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 (49) hide show
  1. package/dist/api/index.d.ts +16 -15
  2. package/dist/api/index.js +16 -15
  3. package/dist/api/main.d.ts +31 -23
  4. package/dist/api/main.js +3 -1
  5. package/dist/api/modules/action.d.ts +2 -2
  6. package/dist/api/modules/action.js +1 -5
  7. package/dist/api/modules/base.d.ts +2 -2
  8. package/dist/api/modules/builder.d.ts +2 -2
  9. package/dist/api/modules/builder.js +0 -4
  10. package/dist/api/modules/checker.d.ts +3 -3
  11. package/dist/api/modules/checker.js +2 -6
  12. package/dist/api/modules/component.d.ts +922 -0
  13. package/dist/api/modules/component.js +1344 -0
  14. package/dist/api/modules/config.d.ts +30 -1
  15. package/dist/api/modules/config.js +81 -0
  16. package/dist/api/modules/cooldown.d.ts +5 -5
  17. package/dist/api/modules/cooldown.js +1 -17
  18. package/dist/api/modules/database.d.ts +21 -13
  19. package/dist/api/modules/database.js +0 -23
  20. package/dist/api/modules/helpmenu.d.ts +9 -7
  21. package/dist/api/modules/helpmenu.js +22 -17
  22. package/dist/api/modules/language.d.ts +2 -2
  23. package/dist/api/modules/language.js +3 -7
  24. package/dist/api/modules/progressbar.d.ts +2 -1
  25. package/dist/api/modules/progressbar.js +1 -1
  26. package/dist/api/modules/responder.d.ts +2 -2
  27. package/dist/api/modules/responder.js +0 -4
  28. package/dist/api/modules/session.d.ts +1 -1
  29. package/dist/api/modules/session.js +1 -1
  30. package/dist/api/modules/startscreen.d.ts +2 -2
  31. package/dist/api/modules/startscreen.js +5 -3
  32. package/package.json +3 -2
  33. package/src/api/index.ts +16 -15
  34. package/src/api/main.ts +33 -24
  35. package/src/api/modules/action.ts +2 -4
  36. package/src/api/modules/base.ts +2 -2
  37. package/src/api/modules/builder.ts +2 -4
  38. package/src/api/modules/checker.ts +6 -7
  39. package/src/api/modules/component.ts +1822 -0
  40. package/src/api/modules/config.ts +78 -1
  41. package/src/api/modules/cooldown.ts +8 -13
  42. package/src/api/modules/database.ts +24 -32
  43. package/src/api/modules/helpmenu.ts +29 -22
  44. package/src/api/modules/language.ts +5 -7
  45. package/src/api/modules/progressbar.ts +2 -3
  46. package/src/api/modules/responder.ts +2 -4
  47. package/src/api/modules/session.ts +1 -1
  48. package/src/api/modules/startscreen.ts +6 -4
  49. package/src/api/modules/component.txt +0 -350
@@ -1,350 +0,0 @@
1
- ///////////////////////////////////////
2
- //COMPONENTS MODULE
3
- ///////////////////////////////////////
4
- import { ODId, ODValidId, ODSystemError, ODManagerData, ODNoGeneric, ODManager } from "./base.js"
5
- import * as discord from "discord.js"
6
- import { ODWorkerManager, ODWorkerCallback, ODWorker } from "./worker.js"
7
- import { ODDebugger } from "./console.js"
8
-
9
- /**## ODComponentFactoryInstance `class`
10
- * An Open Discord component factory instance.
11
- *
12
- * It will contain the final root component which is returned by an `ODComponentFactory`.
13
- * This component can be used in another `ODComponentFactory` or rendered to a message/modal.
14
- */
15
- export class ODComponentFactoryInstance<Component extends ODComponent<object,any>> {
16
- /**The root component of this factory. */
17
- #rootComponent: Component|null = null
18
-
19
- /**The root component of this factory. */
20
- getComponent(){
21
- return this.#rootComponent
22
- }
23
- /**Set the root component of this factory. */
24
- setComponent(c:Component|null){
25
- this.#rootComponent = c
26
- }
27
- }
28
-
29
- /**## ODComponentFactory `class`
30
- * An Open Discord component factory.
31
- *
32
- * It is a collection of functions/workers/hooks which will build a Discord message/modal component from scratch.
33
- * Plugins can intercept and modify these workers to replace default behaviour or layout.
34
- */
35
- export class ODComponentFactory<Component extends ODComponent<object,any>,Origin extends string,Params,WorkerIds extends string = string> extends ODManagerData {
36
- /**A collection of all workers for this component factory. */
37
- workers: ODWorkerManager<ODComponentFactoryInstance<Component>,Origin,Params,WorkerIds>
38
-
39
- constructor(id:ODValidId, callback?:ODWorkerCallback<ODComponentFactoryInstance<Component>,Origin,Params>, priority?:number, callbackId?:ODValidId){
40
- super(id)
41
- this.workers = new ODWorkerManager("ascending")
42
- if (callback) this.workers.add(new ODWorker(callbackId ? callbackId : id,priority ?? 0,callback))
43
- }
44
- /**Run all workers and return the resulting component. */
45
- async build(origin:Origin, params:Params): Promise<ODComponentInferBuildResult<Component>> {
46
- const instance = new ODComponentFactoryInstance<Component>()
47
- await this.workers.executeWorkers(instance,origin,params)
48
- const rootComponent = instance.getComponent()
49
- if (!rootComponent) throw new ODSystemError("ODComponentFactory.build() --> Failed to build component! (id: "+this.id.value+")")
50
- return rootComponent.build()
51
- }
52
- }
53
-
54
- /**## ODComponentManagerIdConstraint `type`
55
- * The constraint/layout for id mappings/interfaces of the `ODComponentManager` class.
56
- */
57
- export type ODComponentManagerIdConstraint = Record<string,{origin:string,params:object,workers:string}>
58
-
59
- /**## ODBaseComponentManager `class`
60
- * A generic Open Discord component manager.
61
- *
62
- * It contains a collection of all Open Discord component factories. You can:
63
- * - Add your own message/modal component factories
64
- * - Modify existing message/modal component factories
65
- *
66
- * Messages created using this system are not compatible with `ODBuilder` messages!
67
- */
68
- export class ODBaseComponentManager<IdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint,Component extends ODComponent<object,any> = ODComponent<object,any>> extends ODManager<ODComponentFactory<Component,string,{},string>> {
69
- get<FactoryId extends keyof ODNoGeneric<IdList>>(id:FactoryId): ODComponentFactory<Component,IdList[FactoryId]["origin"],IdList[FactoryId]["params"],IdList[FactoryId]["workers"]>
70
- get(id:ODValidId): ODComponentFactory<Component,string,{},string>|null
71
-
72
- get(id:ODValidId): ODComponentFactory<Component,string,{},string>|null {
73
- return super.get(id)
74
- }
75
-
76
- remove<FactoryId extends keyof ODNoGeneric<IdList>>(id:FactoryId): ODComponentFactory<Component,IdList[FactoryId]["origin"],IdList[FactoryId]["params"],IdList[FactoryId]["workers"]>
77
- remove(id:ODValidId): ODComponentFactory<Component,string,{},string>|null
78
-
79
- remove(id:ODValidId): ODComponentFactory<Component,string,{},string>|null {
80
- return super.remove(id)
81
- }
82
-
83
- exists(id:keyof ODNoGeneric<IdList>): boolean
84
- exists(id:ODValidId): boolean
85
-
86
- exists(id:ODValidId): boolean {
87
- return super.exists(id)
88
- }
89
- }
90
-
91
- /**## ODSharedComponentManager `class
92
- * A special class with types for shared message/modal `ODComponent`'s.
93
- * Create button, dropdown or any other layout component template to use them in messages & modals.
94
- */
95
- export class ODSharedComponentManager<IdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint> extends ODBaseComponentManager<IdList,ODComponent<object,any>> {
96
- constructor(debug:ODDebugger){
97
- super(debug,"shared component")
98
- }
99
- }
100
-
101
- /**## ODMessageComponentManager `class
102
- * A special class with types for `ODMessageComponent`'s.
103
- * Create message templates to use as replies and make use of shared components.
104
- */
105
- export class ODMessageComponentManager<IdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint> extends ODBaseComponentManager<IdList,TODO> {
106
- constructor(debug:ODDebugger){
107
- super(debug,"message component")
108
- }
109
- }
110
-
111
- /**## ODModalComponentManager `class
112
- * A special class with types for `ODModalComponent`'s.
113
- * Create modal templates to use as forms and make use of shared components.
114
- */
115
- export class ODModalComponentManager<IdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint> extends ODBaseComponentManager<IdList,TODO> {
116
- constructor(debug:ODDebugger){
117
- super(debug,"modal component")
118
- }
119
- }
120
-
121
- /**## ODComponentManager `class`
122
- * An Open Discord component manager.
123
- *
124
- * Create message & modal templates, re-use components and design all visual elements of the bot.
125
- *
126
- * Using the Open Discord component system has many advantages compared to vanilla `discord.js`:
127
- * - Plugins can extend, edit & replace messages & components
128
- * - Includes automatic error handling
129
- * - Independent workers/hooks (with priority)
130
- * - Fail-safe design using try-catch
131
- * - Automatic switch between components v2 and legacy components depending on message requirements
132
- * - Get to know the origin of the request (e.g. button, dropdown, modal, ...)
133
- * - And so much more!
134
- */
135
- export class ODComponentManager<
136
- SharedIdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint,
137
- MessageIdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint,
138
- ModalIdList extends ODComponentManagerIdConstraint = ODComponentManagerIdConstraint
139
- > {
140
- /**The manager for all shared components. */
141
- shared: ODSharedComponentManager<SharedIdList>
142
- /**The manager for all messages components. */
143
- messages: ODMessageComponentManager<MessageIdList>
144
- /**The manager for all modals components. */
145
- modals: ODModalComponentManager<ModalIdList>
146
-
147
- constructor(debug:ODDebugger){
148
- this.shared = new ODSharedComponentManager(debug)
149
- this.messages = new ODMessageComponentManager(debug)
150
- this.modals = new ODModalComponentManager(debug)
151
- }
152
- }
153
-
154
- ///////////////////////////////////////
155
- // GENERIC COMPONENT DEFINITIONS //
156
- ///////////////////////////////////////
157
-
158
- /**## ODComponentBuilderFunc `type`
159
- * The builder function of a component.
160
- */
161
- export type ODComponentBuilderFunc<BuildResult> = () => Promise<BuildResult|null>|BuildResult|null
162
-
163
- /**## ODComponentInferBuildResult `type`
164
- * Infer the build result of a certain component.
165
- */
166
- export type ODComponentInferBuildResult<Component> = Component extends ODComponent<object,infer BuildResult> ? BuildResult : never
167
-
168
- /**## ODComponent `class`
169
- * An Open Discord message/modal component.
170
- *
171
- * This class itself doesn't do anything, but is a blueprint for other
172
- * `ODComponent` classes which represent the new Discord message/modal components.
173
- */
174
- export class ODComponent<Data extends object,BuildResult> {
175
- /**The id of this message/modal component. */
176
- id: ODId
177
- /** */
178
- /**The data or configuration of this message/modal component. */
179
- readonly data: Data
180
- /**Build this component. Returns `null` when invalid. */
181
- readonly build: ODComponentBuilderFunc<BuildResult>
182
-
183
- constructor(id:ODValidId,data:Data,build:ODComponentBuilderFunc<BuildResult>){
184
- this.id = new ODId(id)
185
- this.data = data
186
- this.build = build
187
- }
188
- }
189
-
190
- /**## ODGroupComponent `class`
191
- * An Open Discord message/modal component with children.
192
- *
193
- * This class itself doesn't do anything, but is a blueprint for other
194
- * `ODGroupComponent` classes which represent the new Discord message/modal components.
195
- */
196
- export class ODGroupComponent<Data extends object,ChildComponent extends ODComponent<object,any>,BuildResult> extends ODComponent<Data,BuildResult> {
197
- /**The collection of child components. */
198
- readonly children: ChildComponent[] = []
199
-
200
- /**Add a new component to this group. There are multiple modes available:
201
- * - `start`: insert at the start of the list.
202
- * - `end`: insert at the end of the list.
203
- * - `before`: insert before an existing component `referenceId` (`start` if `referenceId` is invalid)
204
- * - `after`: insert after an existing component `referenceId` (`end` if `referenceId` is invalid)
205
- * - `index`: insert at a certain index `referenceIndex` (`end` if `referenceIndex` is invalid)
206
- */
207
- addComponent(c:ChildComponent,mode:"start"|"end"): void
208
- addComponent(c:ChildComponent,mode:"before"|"after",referenceId:ODValidId): void
209
- addComponent(c:ChildComponent,mode:"index",referenceIndex:number): void
210
- addComponent(c:ChildComponent,mode:"start"|"end"|"before"|"after"|"index" = "start",reference?:ODValidId|number){
211
- if (mode == "start") this.children.unshift(c)
212
- else if (mode == "end") this.children.push(c)
213
- else if (mode == "before"){
214
- if (!reference || !this.children.find((c) => c.id.value === new ODId(reference).value)) this.children.unshift(c)
215
- else{
216
- const referenceIndex = this.children.findIndex((c) => c.id.value === new ODId(reference).value)
217
- this.children.splice(referenceIndex,0,c) //insert at position 'referenceIndex' (before)
218
- }
219
- }else if (mode == "after"){
220
- if (!reference || !this.children.find((c) => c.id.value === new ODId(reference).value)) this.children.push(c)
221
- else{
222
- const referenceIndex = this.children.findIndex((c) => c.id.value === new ODId(reference).value)
223
- this.children.splice(referenceIndex+1,0,c) //insert at position 'referenceIndex+1' (after)
224
- }
225
- }else if (mode == "index"){
226
- if (!reference || typeof reference !== "number") this.children.push(c)
227
- else{
228
- this.children.splice(reference,0,c) //insert at position 'reference'
229
- }
230
- }
231
- }
232
- /**Get a component with a certain ID in this group. Returns `null` if non-existent. */
233
- getComponent(id:ODValidId){
234
- const component = this.children.find((c) => c.id.value === new ODId(id).value)
235
- return component ?? null
236
- }
237
- /**Get the position of a component with a certain ID in this group. Returns `-1` if non-existent. */
238
- getComponentPosition(id:ODValidId){
239
- return this.children.findIndex((c) => c.id.value === new ODId(id).value)
240
- }
241
- /**Returns if a component with a certain ID exists in this group. */
242
- existsComponent(id:ODValidId){
243
- const component = this.children.find((c) => c.id.value === new ODId(id).value)
244
- return component ? true : false
245
- }
246
- /**Remove a component with a certain ID from this group. Returns the removed component or `null if non-existent. */
247
- removeComponent(id:ODValidId){
248
- const index = this.children.findIndex((c) => c.id.value === new ODId(id).value)
249
- if (index < 0) return null
250
- else return this.children.splice(index,1)[0]
251
- }
252
- /**Moves an existing component to a new location in this group. There are multiple modes available:
253
- * - `start`: move to the start of the list.
254
- * - `end`: move to the end of the list.
255
- * - `before`: move before an existing component `referenceId` (`start` if `referenceId` is invalid)
256
- * - `after`: move after an existing component `referenceId` (`end` if `referenceId` is invalid)
257
- * - `index`: move to a certain index `referenceIndex` (`end` if `referenceIndex` is invalid)
258
- */
259
- moveComponent(id:ODValidId,mode:"start"|"end"): void
260
- moveComponent(id:ODValidId,mode:"before"|"after",referenceId:ODValidId): void
261
- moveComponent(id:ODValidId,mode:"index",referenceIndex:number): void
262
- moveComponent(id:ODValidId,mode:"start"|"end"|"before"|"after"|"index" = "start",reference?:ODValidId|number){
263
- const component = this.removeComponent(id)
264
- if (component){
265
- if (mode == "start" || mode == "end") this.addComponent(component,mode)
266
- if ((mode == "before" || mode == "after") && reference) this.addComponent(component,mode,reference)
267
- if (mode == "index" && typeof reference == "number") this.addComponent(component,mode,reference)
268
- return
269
- }
270
- }
271
- }
272
-
273
- /**## ODParentComponent `class`
274
- * An Open Discord message/modal component with a single child.
275
- *
276
- * This class itself doesn't do anything, but is a blueprint for other
277
- * `ODParentComponent` classes which represent the new Discord message/modal components.
278
- */
279
- export class ODParentComponent<Data extends object,ChildComponent extends ODComponent<object,any>,BuildResult> extends ODComponent<Data,BuildResult> {
280
- /**The child component of this parent. */
281
- #child: ChildComponent|null = null
282
-
283
- /**The child component of this parent. */
284
- get child(){
285
- return this.#child
286
- }
287
- /**Set the child component of this parent. */
288
- setComponent(c:ChildComponent|null){
289
- this.#child = c
290
- }
291
- }
292
-
293
- //////////////////////////////////////
294
- // GLOBAL COMPONENT DEFINITIONS //
295
- //////////////////////////////////////
296
-
297
-
298
-
299
- //////////////////////////////////////
300
- // LAYOUT COMPONENT DEFINITIONS //
301
- //////////////////////////////////////
302
-
303
-
304
-
305
- ///////////////////////////////////////
306
- // CONTENT COMPONENT DEFINITIONS //
307
- ///////////////////////////////////////
308
-
309
- /**## ODTextComponentData `type`
310
- * The configurable settings/options for the `ODTextComponent`.
311
- */
312
- export interface ODTextComponentData {
313
- /**The text to display. */
314
- content:string
315
- }
316
-
317
- /**## ODTextComponent `class`
318
- * A text component which renders markdown text in a message.
319
- */
320
- export class ODTextComponent extends ODComponent<ODTextComponentData,discord.TextDisplayBuilder> {
321
- constructor(id:ODValidId,data:Partial<ODTextComponentData>){
322
- const initData: ODTextComponentData = {content:"",...data}
323
- super(id,initData,() => {
324
- if (this.data.content.length < 1) return null
325
- return new discord.TextDisplayBuilder({
326
- content:this.data.content
327
- })
328
- })
329
- }
330
-
331
- /**Set the text to display. */
332
- setContent(value:string){
333
- this.data.content = value
334
- }
335
- }
336
-
337
- /* TODO
338
- * 🔴 ODFileComponent
339
- * 🔴 ODGalleryComponent
340
- * 🔴 ODThumbnailComponent
341
- * 🔴 ODContentComponent (simple msg)
342
- * 🔴 ODPollComponent (simple msg)
343
- * 🔴 ODEmbedComponent (simple msg)
344
- * 🔴 ODStickerComponent (simple msg)
345
- */
346
-
347
- ///////////////////////////////////////////
348
- // INTERACTIVE COMPONENT DEFINITIONS //
349
- ///////////////////////////////////////////
350
-