1
1
const events = require ( 'events' ) ;
2
- const { chattools, time } = require ( '../utils' ) ;
2
+ const { chattools, Time } = require ( '../utils' ) ;
3
3
4
4
const emitter = new events . EventEmitter ( ) ;
5
- let threads = [ ] ;
6
- let counter = 0 ;
7
5
8
6
function Thread ( id , text , message , timer ) {
9
7
this . id = id ;
@@ -19,152 +17,125 @@ function Thread(id, text, message, timer) {
19
17
} ) ;
20
18
}
21
19
22
- function toPositiveNumber ( value ) {
23
- const number = Number . parseFloat ( value ) ;
24
-
25
- if ( number >= 0 ) {
26
- return number ;
27
- }
28
- if ( Number . isNaN ( number ) ) {
29
- return 0 ;
30
- }
31
-
32
- return - number ;
33
- }
34
-
35
20
class Cron {
36
- constructor ( data , message ) {
21
+ constructor ( ) {
37
22
this . name = 'cron' ;
38
- this . data = data ;
39
- this . text = data . text ;
40
- this . message = message ;
23
+ this . threads = [ ] ;
24
+ this . counter = 0 ;
25
+ this . defaultMessage = `
26
+ *criação*: _!cron --create --[time]=<int>_
27
+ *outros*: _!cron [--flag] [<int>]_
41
28
42
- const seconds = toPositiveNumber ( data . kwargs . s ) ;
43
- const minutes = toPositiveNumber ( data . kwargs . m ) ;
44
- const hours = toPositiveNumber ( data . kwargs . h ) ;
45
- const days = toPositiveNumber ( data . kwargs . d ) ;
29
+ *argumentos*:
30
+ _--create -> cria uma nova thread_
31
+ _--destroy -> apaga uma thread_
32
+ _--start -> inicia uma thread_
33
+ _--stop -> para uma thread_
34
+ _--s -> define um intervalor de segundos_
35
+ _--m -> define um intervalor de minutos_
36
+ _--h -> define um intervalor de horas_
37
+ _--d -> define um intervalor de dias_
46
38
47
- this . timer = time . timer ( seconds , minutes , hours , days ) ;
39
+ ⚠️ *o uso indevido dessa função resultará em ban de 3 dias* ⚠️
40
+ ` . trim ( ) ;
48
41
}
49
42
50
- async execute ( _ , message ) {
51
- const { args } = this . data ;
52
- const isAdm = await chattools . isAdm ( this . message ) ;
43
+ async execute ( data , message ) {
44
+ const isAdm = await chattools . isAdm ( message ) ;
53
45
54
46
if ( isAdm ) {
55
- message . reply ( this . runsArg ( args ) ) ;
47
+ message . reply ( this . runs ( data , message ) ) ;
56
48
return ;
57
49
}
58
50
59
51
message . reply ( 'staff only.' ) ;
60
52
}
61
53
62
- create ( ) {
63
- if ( ! ( this . timer > 0 ) ) {
64
- return 'you must add a valid time' ;
54
+ create ( data , message ) {
55
+ const { kwargs, text } = data ;
56
+ const timer = Time . timer (
57
+ Math . abs ( kwargs . s || 0 ) ,
58
+ Math . abs ( kwargs . m || 0 ) ,
59
+ Math . abs ( kwargs . h || 0 ) ,
60
+ Math . abs ( kwargs . d || 0 )
61
+ ) ;
62
+
63
+ if ( timer <= 0 ) {
64
+ throw new Error ( 'Time invalid.' ) ;
65
65
}
66
66
67
- counter ++ ;
68
- const { message, text, timer } = this ;
69
- const thread = new Thread ( counter , text , message , timer ) ;
70
- threads . push ( thread ) ;
67
+ this . counter += 1 ;
68
+ const thread = new Thread ( this . counter , text , message , timer ) ;
69
+ this . threads . push ( thread ) ;
71
70
72
- return `thread created using id: ${ thread . id } ` ;
71
+ return `Thread criada usando o id ${ thread . id } ` ;
73
72
}
74
73
75
- destroy ( ) {
76
- if ( ! Cron . isIdValid ( this . text ) ) {
77
- return 'thread not found ' ;
74
+ destroy ( id ) {
75
+ if ( ! this . isIdValid ( id ) ) {
76
+ return 'Thread não encontrada. ' ;
78
77
}
79
78
80
- const thread = threads . find ( ( t ) => t . id === Number ( this . text ) ) ;
81
- this . stop ( ) ;
79
+ const thread = this . threads . find ( ( t ) => t . id === Number ( id ) ) ;
80
+ this . stop ( id ) ;
82
81
83
- emitter . removeAllListeners ( `start-cron${ this . text } ` , thread . start ) ;
84
- emitter . removeAllListeners ( `stop-cron${ this . text } ` , thread . stop ) ;
82
+ emitter . removeAllListeners ( `start-cron${ id } ` , thread . start ) ;
83
+ emitter . removeAllListeners ( `stop-cron${ id } ` , thread . stop ) ;
85
84
86
- threads = threads . filter ( ( t ) => ! ( t . id === Number ( this . text ) ) ) ;
87
- return 'thread destroyed successfully ' ;
85
+ this . threads = this . threads . filter ( ( t ) => ! ( t . id === Number ( id ) ) ) ;
86
+ return 'Thread destruida com sucesso. ' ;
88
87
}
89
88
90
- start ( ) {
91
- if ( Cron . isIdValid ( this . text ) ) {
92
- emitter . emit ( `start-cron${ this . text } ` ) ;
93
- return `starting thread ${ this . text } ` ;
89
+ start ( id ) {
90
+ if ( this . isIdValid ( id ) ) {
91
+ emitter . emit ( `start-cron${ id } ` ) ;
92
+ return `starting thread ${ id } ` ;
94
93
}
95
94
96
95
return 'thread not found' ;
97
96
}
98
97
99
- stop ( ) {
100
- if ( Cron . isIdValid ( this . text ) ) {
101
- emitter . emit ( `stop-cron${ this . text } ` ) ;
102
- return `stopping thread ${ this . text } ` ;
98
+ stop ( id ) {
99
+ if ( this . isIdValid ( id ) ) {
100
+ emitter . emit ( `stop-cron${ id } ` ) ;
101
+ return `Parando a thread de id ${ id } ... ` ;
103
102
}
104
103
105
- return 'thread not found ' ;
104
+ return 'Thread não encontrada. ' ;
106
105
}
107
106
108
- static log ( ) {
109
- let output = '' ;
110
-
111
- if ( threads . length === 0 ) {
112
- output += 'thread not open' ;
113
- } else {
114
- output += 'threads open:\n\n' ;
107
+ log ( ) {
108
+ if ( this . threads . length === 0 ) {
109
+ return 'Nenhuma thread aberta.' ;
115
110
}
116
111
117
- threads . forEach ( ( thread ) => {
118
- const id = `id: ${ thread . id } \n` ;
119
- const description = `desc: ${ thread . description } \n` ;
120
- output += `${ id } ${ description } \n` ;
112
+ let output = 'Threads abertas:\n\n' ;
113
+
114
+ this . threads . forEach ( ( t ) => {
115
+ output += `id: ${ t . id } \ndesc: ${ t . description } \n \n` ;
121
116
} ) ;
122
117
123
118
return output . trim ( ) ;
124
119
}
125
120
126
- static killall ( ) {
127
- return null ;
128
- }
129
-
130
- runsArg ( args ) {
131
- const seila = {
132
- log : ( ) => Cron . log ( ) ,
133
- killall : ( ) => 'sorry, this function is not done yet' ,
134
- create : ( ) => this . create ( ) ,
135
- destroy : ( ) => this . destroy ( ) ,
136
- start : ( ) => this . start ( ) ,
137
- stop : ( ) => this . stop ( ) ,
121
+ runs ( data , message ) {
122
+ const methods = {
123
+ log : ( ) => this . log ( ) ,
124
+ create : ( ) => this . create ( data , message ) ,
125
+ destroy : ( ) => this . destroy ( data . text ) ,
126
+ start : ( ) => this . start ( data . text ) ,
127
+ stop : ( ) => this . stop ( data . text ) ,
138
128
} ;
139
129
140
- if ( seila [ args [ 0 ] ] ) {
141
- return seila [ args [ 0 ] ] ( ) ;
130
+ if ( methods [ data . args [ 0 ] ] ) {
131
+ return methods [ data . args [ 0 ] ] ( ) ;
142
132
}
143
133
144
- return Cron . default ( ) ;
145
- }
146
-
147
- static isIdValid ( id ) {
148
- return threads . some ( ( t ) => t . id === Number ( id ) ) ;
134
+ return this . defaultMessage ;
149
135
}
150
136
151
- static default ( ) {
152
- return `
153
- *criação*: _!cron --create --[time]=<int>_
154
- *outros*: _!cron [--flag] [<int>]_
155
-
156
- *argumentos*:
157
- _--create -> cria uma nova thread_
158
- _--destroy -> apaga uma thread_
159
- _--start -> inicia uma thread_
160
- _--stop -> para uma thread_
161
- _--s -> define um intervalor de segundos_
162
- _--m -> define um intervalor de minutos_
163
- _--h -> define um intervalor de horas_
164
- _--d -> define um intervalor de dias_
165
-
166
- ⚠️ *o uso indevido dessa função resultará em ban de 3 dias* ⚠️
167
- ` . trim ( ) ;
137
+ isIdValid ( id ) {
138
+ return this . threads . some ( ( t ) => t . id === Number ( id ) ) ;
168
139
}
169
140
}
170
141
0 commit comments