1
- import type { Client } from 'discord.js' ;
1
+ /* eslint-disable max-len */
2
+ import type {
3
+ Client ,
4
+ CommandInteractionOption ,
5
+ ApplicationCommandType ,
6
+ } from 'discord.js' ;
2
7
import { Logger } from './logger/Logger' ;
3
8
import type { GClient } from '../GClient' ;
4
9
import { Plugins } from '../managers/PluginManager' ;
5
10
11
+ /**
12
+ * Includes many useful functions
13
+ */
6
14
export class Util {
7
15
/**
8
- * @deprecated We don't support arguments in object/array
9
- * @link https://discord.js.org/#/docs/main/stable/class/CommandInteractionOptionResolver
16
+ * Converts [CommandInteractionOptionResolveer](https://discord.js.org/#/docs/discord.js/stable/class/CommandInteractionOptionResolver) to an array
17
+ * @param {import('discord.js').CommandInteractionOptionResolver[] } options The options to convert
18
+ * @deprecated We don't support arguments in object
19
+ * @returns {string[] }
10
20
*/
11
- static argumentsToArray ( options : Array < any > ) : Array < string > {
21
+ static argumentsToArray ( options : CommandInteractionOption [ ] ) : string [ ] {
12
22
const args = [ ] ;
13
23
14
24
const check = options => {
@@ -26,10 +36,12 @@ export class Util {
26
36
}
27
37
28
38
/**
29
- * @deprecated We don't support arguments in object/array
30
- * @link https://discord.js.org/#/docs/main/stable/class/CommandInteractionOptionResolver
39
+ * Converts [CommandInteractionOptionResolveer](https://discord.js.org/#/docs/discord.js/stable/class/CommandInteractionOptionResolver) to an object
40
+ * @param {import('discord.js').CommandInteractionOptionResolver[] } options The options to convert
41
+ * @deprecated We don't support arguments in object
42
+ * @returns {any }
31
43
*/
32
- static argumentsToObject ( options : Array < any > ) {
44
+ static argumentsToObject ( options : CommandInteractionOption [ ] ) : any {
33
45
const args = { } ;
34
46
35
47
const check = ( options , object ) => {
@@ -47,10 +59,12 @@ export class Util {
47
59
}
48
60
49
61
/**
62
+ * Check if the type is a sub command or sub command group
63
+ * @param {ApplicationCommandType } type The type to check
50
64
* @deprecated We don't support arguments in object/array
51
- * @link https://discord.js.org/#/docs/main/stable/class/CommandInteractionOptionResolver
65
+ * @returns { boolean }
52
66
*/
53
- static checkIfSubOrGroup ( type : string ) {
67
+ static checkIfSubOrGroup ( type : ApplicationCommandType ) : boolean {
54
68
// Why? Because discord.js v14 (:
55
69
return ! ! [
56
70
'SUB_COMMAND' ,
@@ -60,6 +74,11 @@ export class Util {
60
74
] . includes ( type ) ;
61
75
}
62
76
77
+ /**
78
+ * Check if the input is a class
79
+ * @param {any } input The input to check
80
+ * @returns {boolean }
81
+ */
63
82
static isClass ( input : any ) : boolean {
64
83
return (
65
84
typeof input === 'function' &&
@@ -68,7 +87,15 @@ export class Util {
68
87
) ;
69
88
}
70
89
71
- static resolveArgumentOptions ( options : any ) : any {
90
+ /**
91
+ * Converts option names from camelCase to snake_case
92
+ * @param {Object<string, string> } options The options to convert
93
+ * @deprecated This method is no longer used anywhere
94
+ * @returns {Object<string, string> }
95
+ */
96
+ static resolveArgumentOptions (
97
+ options : [ key : string , value : string ] ,
98
+ ) : [ key : string , value : string ] {
72
99
for ( const [ key , value ] of Object . entries ( options ) ) {
73
100
const option = key . match ( / [ A - Z ] / g) ?. [ 0 ]
74
101
? key . replace (
@@ -87,6 +114,12 @@ export class Util {
87
114
return options ;
88
115
}
89
116
117
+ /**
118
+ * The method that resolves the file for directoryLoader
119
+ * @param {any } file The file that will be resolved
120
+ * @param {string } fileType The file type
121
+ * @returns {any }
122
+ */
90
123
static resolveFile ( file : any , fileType : string ) : any {
91
124
if ( fileType === '.ts' ) return file . default || Object . values ( file ) [ 0 ] ;
92
125
if ( fileType === '.js' ) {
@@ -97,28 +130,55 @@ export class Util {
97
130
return file ;
98
131
}
99
132
100
- static stringToBoolean ( string : string ) : boolean {
133
+ /**
134
+ * The method that converts a string to a boolean
135
+ * @param {string } text The text to convert
136
+ * @deprecated This method is no longer used anywhere
137
+ * @returns
138
+ */
139
+ static stringToBoolean ( text : string ) : boolean {
101
140
const regex = / ^ \s * ( t r u e | 1 | o n ) \s * $ / i;
102
- return regex . test ( string ) ;
141
+ return regex . test ( text ) ;
103
142
}
104
143
105
- static resolveValidationErrorTrace ( array : Array < any > ) : string {
144
+ /**
145
+ * The method that solves the error validation trace
146
+ * @param {any[] } array
147
+ * @returns
148
+ */
149
+ static resolveValidationErrorTrace ( array : any [ ] ) : string {
106
150
array = array . filter ( item => typeof item === 'string' ) ;
107
151
return `(${ array . join ( ' -> ' ) || 'unknown' } )` ;
108
152
}
109
153
154
+ /**
155
+ * The method that modifies numbers and adds `0` before numbers that are less than 10
156
+ * @param {number } number The number to modify
157
+ * @returns {string }
158
+ */
110
159
static pad ( number : number ) : string {
111
160
return ( number < 10 ? '0' : '' ) + number ;
112
161
}
113
162
163
+ /**
164
+ * The method that throws an error to the console
165
+ * @param {string } error The error to throw
166
+ * @param {string } name The name of the class or file path that threw the error
167
+ */
114
168
static throwError ( error , name ) : void {
115
169
const trace = Util . resolveValidationErrorTrace ( [ name ] ) ;
116
170
117
171
Logger . error ( error , trace ) ;
118
172
}
119
173
120
- static toPascalCase ( input : string ) : string {
121
- return input
174
+ /**
175
+ * The method that converts case to PascalCase
176
+ * @param {string } text The text to convert
177
+ * @deprecated This method is no longer used anywhere
178
+ * @returns {string }
179
+ */
180
+ static toPascalCase ( text : string ) : string {
181
+ return text
122
182
. replace ( new RegExp ( / [ - _ ] + / , 'g' ) , ' ' )
123
183
. replace ( new RegExp ( / [ ^ \w \s ] / , 'g' ) , '' )
124
184
. replace (
@@ -128,6 +188,12 @@ export class Util {
128
188
. replace ( new RegExp ( / \w / ) , s => s . toUpperCase ( ) ) ;
129
189
}
130
190
191
+ /**
192
+ * The method that uses `@gcommands/plugin-language` to get a message in a specific language
193
+ * @param {string } value The value to get
194
+ * @param {{client: import('discord.js').Client | import('../GClient').GClient} } client The client to get the default response
195
+ * @returns {Promise<string> }
196
+ */
131
197
static async getResponse (
132
198
value : string ,
133
199
interaction : { client : Client | GClient } ,
0 commit comments