1
1
"use strict" ;
2
- Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3
- const discord_js_1 = require ( "discord.js" ) ;
4
- const base_1 = require ( "./base" ) ;
5
- /**
6
- * A pagination mode that uses a MessageEmbed with a field(s) containing the elements to paginate.
7
- * @extends [[PaginationEmbed]]
8
- * @noInheritDoc
9
- */
10
- class FieldsEmbed extends base_1 . PaginationEmbed {
11
- constructor ( ) {
12
- super ( ) ;
13
- this . elementsPerPage = 10 ;
14
- this . embed = new discord_js_1 . MessageEmbed ( ) ;
15
- }
16
- /** Elements in the current page. */
17
- get elementList ( ) {
18
- const begin = ( this . page - 1 ) * this . elementsPerPage ;
19
- const end = begin + this . elementsPerPage ;
20
- return this . array . slice ( begin , end ) ;
21
- }
22
- /**
23
- * Build the Pagination Fields Embed.
24
- *
25
- * ### Example
26
- * ```js
27
- * const { FieldsEmbed } = require('discord-paginationembed');
28
- *
29
- * // Under message event.
30
- * new FieldsEmbed()
31
- * .setAuthorizedUsers([message.author.id])
32
- * .setChannel(message.channel)
33
- * .setClientAssets({ prompt: 'Yo {{user}} wat peige?!?!?' })
34
- * .setArray([{ name: 'John Doe' }, { name: 'Jane Doe' }])
35
- * .setElementsPerPage(1)
36
- * .setPageIndicator(false)
37
- * .formatField('Name', el => el.name)
38
- * .setPage(1)
39
- * .setTimeout(69000)
40
- * .setNavigationEmojis({
41
- * back: '◀',
42
- * jump: '↗',
43
- * forward: '▶',
44
- * delete: '🗑'
45
- * })
46
- * .setFunctionEmojis({
47
- * '🔄': (user, instance) => {
48
- * const field = instance.embed.fields[0];
49
- *
50
- * if (field.name === 'Name')
51
- * field.name = user.tag;
52
- * else
53
- * field.name = 'Name';
54
- * }
55
- * })
56
- * .build();```
57
- */
58
- async build ( ) {
59
- this . pages = Math . ceil ( this . array . length / this . elementsPerPage ) ;
60
- await this . _verify ( ) ;
61
- const fields = this . embed . fields ;
62
- this . embed . fields = [ ] ;
63
- for ( const field of fields )
64
- if ( typeof field . value === 'function' )
65
- this . formatField ( field . name , field . value , field . inline ) ;
66
- else
67
- this . embed . addField ( field . name , field . value , field . inline ) ;
68
- const hasPaginateField = this . embed . fields . filter ( f => typeof f . value === 'function' ) . length ;
69
- if ( ! hasPaginateField )
70
- throw new Error ( 'Cannot invoke FieldsEmbed class without at least one formatted field to paginate.' ) ;
71
- if ( this . listenerCount ( 'start' ) )
72
- this . emit ( 'start' ) ;
73
- return this . _loadList ( ) ;
74
- }
75
- /**
76
- * Adds a field to the embed.
77
- * Same as MessageEmbed.addField, but value takes a function instead.
78
- * @param name - Name of the field.
79
- * @param value - Value of the field. Function for `Array.prototype.map().join('\n')`.
80
- * @param inline - Whether the field is inline with other field. Default: `true`
81
- */
82
- formatField ( name , value , inline = true ) {
83
- if ( typeof value !== 'function' )
84
- throw new TypeError ( 'formatField() value parameter only takes a function.' ) ;
85
- // @ts -ignore
86
- this . embed . fields . push ( { name, value, inline } ) ;
87
- return this ;
88
- }
89
- /**
90
- * Sets the maximum number of elements to be displayed per page.
91
- * @param max - Maximum number of elements to be displayed per page.
92
- */
93
- setElementsPerPage ( max ) {
94
- if ( typeof max !== 'number' )
95
- throw new TypeError ( 'setElementsPerPage() only accepts number type.' ) ;
96
- this . elementsPerPage = max ;
97
- return this ;
98
- }
99
- async _drawList ( ) {
100
- const embed = new discord_js_1 . MessageEmbed ( this . embed ) ;
101
- embed . fields = [ ] ;
102
- for ( const field of this . embed . fields )
103
- embed . addField ( field . name , typeof field . value === 'function'
104
- ? this . elementList . map ( field . value ) . join ( '\n' )
105
- : field . value , field . inline ) ;
106
- return embed ;
107
- }
108
- /** @ignore */
109
- async _loadList ( callNavigation = true ) {
110
- const embed = await this . _drawList ( ) ;
111
- const shouldIndicate = this . pageIndicator
112
- ? this . pages === 1
113
- ? undefined
114
- : `Page ${ this . page } of ${ this . pages } `
115
- : undefined ;
116
- if ( this . clientAssets . message )
117
- await this . clientAssets . message . edit ( shouldIndicate , { embed } ) ;
118
- else
119
- this . clientAssets . message = await this . channel . send ( shouldIndicate , { embed } ) ;
120
- return super . _loadList ( callNavigation ) ;
121
- }
122
- }
123
- exports . FieldsEmbed = FieldsEmbed ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : ! 0
5
+ } ) ;
6
+
7
+ const e = require ( "discord.js" ) , t = require ( "./base" ) ;
8
+
9
+ exports . FieldsEmbed = class extends t . PaginationEmbed {
10
+ constructor ( ) {
11
+ super ( ) , this . elementsPerPage = 10 , this . embed = new e . MessageEmbed ( ) ;
12
+ }
13
+ get elementList ( ) {
14
+ const e = ( this . page - 1 ) * this . elementsPerPage , t = e + this . elementsPerPage ;
15
+ return this . array . slice ( e , t ) ;
16
+ }
17
+ async build ( ) {
18
+ this . pages = Math . ceil ( this . array . length / this . elementsPerPage ) , await this . _verify ( ) ;
19
+ const e = this . embed . fields ;
20
+ this . embed . fields = [ ] ;
21
+ for ( const t of e ) "function" == typeof t . value ? this . formatField ( t . name , t . value , t . inline ) : this . embed . addField ( t . name , t . value , t . inline ) ;
22
+ if ( ! this . embed . fields . filter ( e => "function" == typeof e . value ) . length ) throw new Error ( "Cannot invoke FieldsEmbed class without at least one formatted field to paginate." ) ;
23
+ return this . listenerCount ( "start" ) && this . emit ( "start" ) , this . _loadList ( ) ;
24
+ }
25
+ formatField ( e , t , s = ! 0 ) {
26
+ if ( "function" != typeof t ) throw new TypeError ( "formatField() value parameter only takes a function." ) ;
27
+ return this . embed . fields . push ( {
28
+ name : e ,
29
+ value : t ,
30
+ inline : s
31
+ } ) , this ;
32
+ }
33
+ setElementsPerPage ( e ) {
34
+ if ( "number" != typeof e ) throw new TypeError ( "setElementsPerPage() only accepts number type." ) ;
35
+ return this . elementsPerPage = e , this ;
36
+ }
37
+ async _drawList ( ) {
38
+ const t = new e . MessageEmbed ( this . embed ) ;
39
+ t . fields = [ ] ;
40
+ for ( const e of this . embed . fields ) t . addField ( e . name , "function" == typeof e . value ? this . elementList . map ( e . value ) . join ( "\n" ) : e . value , e . inline ) ;
41
+ return t ;
42
+ }
43
+ async _loadList ( e = ! 0 ) {
44
+ const t = await this . _drawList ( ) , s = this . pageIndicator ? 1 === this . pages ? void 0 : `Page ${ this . page } of ${ this . pages } ` : void 0 ;
45
+ return this . clientAssets . message ? await this . clientAssets . message . edit ( s , {
46
+ embed : t
47
+ } ) : this . clientAssets . message = await this . channel . send ( s , {
48
+ embed : t
49
+ } ) , super . _loadList ( e ) ;
50
+ }
51
+ } ;
0 commit comments