1
+ const { addImport } = require ( "erisify/helper/prototype" ) ;
2
+ const { truncate } = require ( "erisify/helper/formatting" ) ;
3
+
4
+ const { preventErrors } = require ( "erisify/index.js" ) . options ;
5
+
6
+ /** TODO:
7
+ * Turn values into strings when possible.
8
+ * Stricter checking for HEX strings is
9
+ * Take a look at `setTimestamp` again
10
+ * Check the types of values, a boolean parameter shouldn't receive a number for example
11
+ */
12
+
1
13
class MessageEmbed {
2
- constructor ( ) {
14
+ /**
15
+ * @param {Object } options
16
+ * @param {Boolean } [options.preventErrors] [Overrides initial options of erisify] Try and fix errors such as making strings shorter instead of throwing an error if the string is too long for this specific embed
17
+ * @param {String } [options.truncate] If a present values that are too long will get truncated and will end into the string passed in this option
18
+ */
19
+ constructor ( options = { preventErrors } ) {
20
+ this . options = options ;
3
21
this . fields = [ ] ;
4
22
5
23
return this ;
6
24
}
7
25
8
- setAuthor ( name , icon_url , url ) {
9
- this . author = { name, icon_url, url } ;
26
+ /**
27
+ * @param {String } name
28
+ * @param {String } [iconUrl]
29
+ * @param {String } [url]
30
+ */
31
+ setAuthor ( name , iconUrl , url ) {
32
+ const tempAuthor = { } ;
33
+
34
+ if ( this . options . truncate || this . options . preventErrors ) {
35
+ const suffix = this . options . truncate || "" ;
36
+ tempAuthor . name = truncate ( name , 256 , suffix ) ;
37
+ if ( iconUrl ) tempAuthor . icon_url = truncate ( iconUrl , 2048 , suffix ) ;
38
+ if ( url ) tempAuthor . url = truncate ( url , 2048 , suffix ) ;
39
+ } else {
40
+ if ( name . length > 0 && name . length <= 256 ) tempAuthor . name = name ;
41
+ else throw new RangeError ( "[MessageEmbed.setAuthor] 'name' must be a string longer than 0 characters and shorter than or equal to 256 characters" ) ;
42
+
43
+ if ( iconUrl ) {
44
+ if ( iconUrl . length > 0 && iconUrl . length <= 2048 ) tempAuthor . icon_url = iconUrl ;
45
+ else throw new RangeError ( "[MessageEmbed.setAuthor] 'iconUrl' must be a string longer than 0 characters and shorter than or equal to 2048 characters" ) ;
46
+ }
47
+
48
+ if ( url ) {
49
+ if ( url . length > 0 && url . length <= 2048 ) tempAuthor . url = url ;
50
+ else throw new RangeError ( "[MessageEmbed.setAuthor] 'url' must be a string longer than 0 characters and shorter than or equal to 2048 characters" ) ;
51
+ }
52
+ }
53
+
54
+ this . author = tempAuthor ;
10
55
11
56
return this ;
12
57
}
13
58
59
+ /**
60
+ * @param {Number|Array|String } color Either a HEX string, array with RGB values or number
61
+ */
14
62
setColor ( color ) {
15
- if ( color . startsWith ( "#" ) ) this . color = parseInt ( color , 16 ) ;
16
- else this . color = color ;
63
+ if ( typeof color == "number" ) {
64
+ if ( color > 0 && color < 16777215 )
65
+ this . color = color ;
66
+ else if ( ! this . options . preventErrors )
67
+ throw new RangeError ( "[MessageEmbed.setColor] 'color' must be a number higher than 0 and lower than or equal to 16777216777215" ) ;
68
+ } else if ( Array . isArray ( color ) ) {
69
+ if ( color . length < 3 && ! this . options . preventErrors )
70
+ throw new RangeError ( "[MessageEmbed.setColor] RGB arrays must be at least 3 items long" ) ;
71
+ else if ( color . length < 3 )
72
+ return this ;
73
+
74
+ if ( color . some ( item => ! Number . isInteger ( item ) ) && ! this . options . preventErrors )
75
+ throw new TypeError ( "[MessageEmbed.setColor] RGB values must be integers" ) ;
76
+ else if ( color . some ( item => ! Number . isInteger ( item ) ) )
77
+ return this ;
78
+
79
+ const [ r , g , b ] = color ;
80
+
81
+ this . color = parseInt ( ( ( 1 << 24 ) + ( r << 16 ) + ( g << 8 ) + b ) . toString ( 16 ) . slice ( 1 ) , 16 ) ;
82
+ } else if ( color . startsWith ( "#" ) ) {
83
+ this . color = parseInt ( color . slice ( 1 , color . length ) , 16 ) ;
84
+ } else if ( ! this . options . preventErrors ) throw new Error ( "[MessageEmbed.setColor] Invalid HEX string passed as 'color'" ) ;
17
85
18
86
return this ;
19
87
}
20
88
89
+ /**
90
+ * @param {String } title Prefers a string, but numbers or arrays are turned into strings too
91
+ */
21
92
setTitle ( title ) {
22
- this . title = title . toString ( ) . slice ( 256 ) ;
93
+ if ( this . options . truncate ) this . title = truncate ( title , 256 , this . options . truncate ) ;
94
+ else if ( this . options . preventErrors ) his . title = truncate ( title , 256 ) ;
95
+ else if ( title . length > 0 && title . length <= 256 ) this . title = title ;
96
+ else throw new RangeError ( "[MessageEmbed.setTitle] 'title' must be a string longer than 0 and shorter or equal to 256 characters" ) ;
23
97
24
98
return this ;
25
99
}
26
100
27
- setUrl ( url ) {
28
- this . url = url ;
101
+ /**
102
+ * @param {String } url
103
+ */
104
+ setURL ( url ) {
105
+ // No clue why url limits are not documented...
106
+ if ( url . length > 0 && url . length <= 2048 ) this . url = url ;
107
+ else throw new RangeError ( "[MessageEmbed.setURL] 'url' must be a well-formed URL longer than 0 and shorter or equal to 2048 characters" ) ;
29
108
30
109
return this ;
31
110
}
32
111
112
+ /**
113
+ * @param {String } description
114
+ */
33
115
setDescription ( description ) {
34
- this . description = description . toString ( ) . slice ( 2048 ) ;
116
+ if ( this . options . truncate || this . options . preventErrors ) this . description = truncate ( description . toString ( ) , 4096 , this . options . truncate || "" ) ;
117
+ else if ( description . length > 0 && description . length <= 4096 ) this . description = description ;
118
+ else throw new RangeError ( "[MessageEmbed.setDescription] 'description' must be a string longer than 0 and shorter or equal to 4096 characters" ) ;
35
119
36
120
return this ;
37
121
}
38
122
123
+ /**
124
+ * @param {String } url An url pointing to an image asset
125
+ */
39
126
setThumbnail ( url ) {
40
- this . thumbnail = { url } ;
127
+ if ( url . length > 0 && url . length <= 2048 ) this . thumbnail = { url } ;
128
+ else throw new RangeError ( "[MessageEmbed.setThumbnail] 'url' must be a well-formed URL longer than 0 and shorter or equal to 2048 characters" ) ;
41
129
42
130
return this ;
43
131
}
44
132
133
+ /**
134
+ * @param {String } url An url pointing to an image asset
135
+ */
45
136
setImage ( url ) {
46
- this . image = { url } ;
137
+ if ( url . length > 0 && url . length <= 2048 ) this . image = { url } ;
138
+ else throw new RangeError ( "[MessageEmbed.setImage] 'url' must be a well-formed URL longer than 0 and shorter or equal to 2048 characters" ) ;
47
139
48
140
return this ;
49
141
}
50
142
143
+ /**
144
+ * @param {DateConstructor } [time]
145
+ */
51
146
setTimestamp ( time = new Date ( ) ) {
52
- this . timestamp = time ;
147
+ if ( Number . isNaN ( new Date ( time ) . getTime ( ) ) ) {
148
+ if ( ! this . options . preventErrors ) throw new Error ( "[MessageEmbed.setTimestamp] Invalid data passed" ) ;
149
+ else time = new Date ( ) ;
150
+ }
151
+
152
+ this . timestamp = new Date ( time ) ;
53
153
54
154
return this ;
55
155
}
56
156
57
- setFooter ( text , icon_url ) {
58
- this . footer = { text : text . toString ( ) . slice ( 2048 ) , icon_url } ;
157
+ /**
158
+ * @param {String } text
159
+ * @param {String } [iconUrl]
160
+ */
161
+ setFooter ( text , iconUrl ) {
162
+ const tempFooter = { } ;
163
+
164
+ if ( this . options . truncate || this . options . preventErrors ) {
165
+ tempFooter . text = truncate ( text , 2048 , this . options . truncate || "" ) ;
166
+ if ( iconUrl ) tempFooter . icon_url = truncate ( iconUrl , 2048 , this . options . truncate || "" ) ;
167
+ } else {
168
+ if ( text . length > 0 && text . length <= 2048 ) tempFooter . text = text ;
169
+ else throw new RangeError ( "[MessageEmbed.setFooter] 'text' must be a string longer than 0 characters and shorter than or equal to 2048 characters" ) ;
170
+
171
+ if ( iconUrl ) {
172
+ if ( iconUrl . length > 0 && iconUrl . length <= 2048 ) tempFooter . icon_url = iconUrl ;
173
+ else throw new RangeError ( "[MessageEmbed.setFooter] 'iconUrl' must be a string longer than 0 characters and shorter than or equal to 2048 characters" ) ;
174
+ }
175
+ }
176
+
177
+ this . footer = tempFooter ;
59
178
60
179
return this ;
61
180
}
62
181
182
+ /**
183
+ * @param {String } name
184
+ * @param {String } value
185
+ * @param {Boolean } [inline=true]
186
+ */
63
187
addField ( name , value , inline = false ) {
188
+ if ( Array . isArray ( value ) ) value = value . join ( "\n" ) ;
189
+
64
190
if ( this . fields . length >= 25 ) {
65
- console . warn ( "Limit of 25 fields exceeded, no field was added" ) ;
66
- return this ;
67
- } else if ( ! name || ! value ) {
68
- console . warn ( "Both a name & value are needed, no field was addded" ) ;
69
- return this ;
191
+ if ( this . options . preventErrors ) {
192
+ console . warn ( "[MessageEmbed.addField] A max of 25 fields are allowed, current field couldn't be added" ) ;
193
+ return this ;
194
+ } else {
195
+ throw new Error ( "[MessageEmbed.addField] A max of 25 fields are allowed, current field would exceed limit" ) ;
196
+ }
70
197
}
71
198
72
- this . fields . push ( { name : toString ( ) . slice ( 256 ) , value : value . toString ( ) . slice ( 256 ) , inline } ) ;
199
+ if ( ! name || ! value && ! this . options . preventErrors ) throw new Error ( "[MessageEmbed.addField] both 'name' & 'value' are required" ) ;
200
+
201
+ const tempField = { name : "Not set" , value : "Not set" , inline } ;
202
+
203
+ if ( this . options . truncate || this . options . preventErrors ) {
204
+ tempField . name = truncate ( name , 256 , this . options . truncate || "" ) ;
205
+ tempField . value = truncate ( value , 1024 , this . options . truncate || "" ) ;
206
+ } else {
207
+ if ( name . length > 0 && name . length <= 256 ) tempField . name = name ;
208
+ else throw new RangeError ( "[MessageEmbed.addField] 'name' must be a string longer than 0 characters and shorter than or equal 256 characters" ) ;
209
+
210
+ if ( value . length > 0 && value . length <= 1048 ) tempField . value = value ;
211
+ else throw new RangeError ( "[MessageEmbed.addField] 'value' must be a string longer than 0 characters and shorter than or equal 1048 characters" ) ;
212
+ }
213
+
214
+ this . fields . push ( tempField ) ;
215
+
216
+ return this ;
73
217
}
74
218
219
+ /**
220
+ * @description Clears all fields
221
+ */
75
222
clearFields ( ) {
76
223
this . fields = [ ] ;
224
+
225
+ return this ;
77
226
}
78
227
}
79
228
80
- module . exports = Eris => {
81
- Eris . MessageEmbed = MessageEmbed ;
229
+ module . exports . init = Eris => {
230
+ addImport ( Eris , MessageEmbed ) ;
82
231
} ;
0 commit comments