11const events = require ( 'events' ) ;
2- const { chattools, time } = require ( '../utils' ) ;
2+ const { chattools, Time } = require ( '../utils' ) ;
33
44const emitter = new events . EventEmitter ( ) ;
5- let threads = [ ] ;
6- let counter = 0 ;
75
86function Thread ( id , text , message , timer ) {
97 this . id = id ;
@@ -19,152 +17,125 @@ function Thread(id, text, message, timer) {
1917 } ) ;
2018}
2119
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-
3520class Cron {
36- constructor ( data , message ) {
21+ constructor ( ) {
3722 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>]_
4128
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_
4638
47- this . timer = time . timer ( seconds , minutes , hours , days ) ;
39+ ⚠️ *o uso indevido dessa função resultará em ban de 3 dias* ⚠️
40+ ` . trim ( ) ;
4841 }
4942
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 ) ;
5345
5446 if ( isAdm ) {
55- message . reply ( this . runsArg ( args ) ) ;
47+ message . reply ( this . runs ( data , message ) ) ;
5648 return ;
5749 }
5850
5951 message . reply ( 'staff only.' ) ;
6052 }
6153
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.' ) ;
6565 }
6666
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 ) ;
7170
72- return `thread created using id: ${ thread . id } ` ;
71+ return `Thread criada usando o id ${ thread . id } ` ;
7372 }
7473
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. ' ;
7877 }
7978
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 ) ;
8281
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 ) ;
8584
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. ' ;
8887 }
8988
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 } ` ;
9493 }
9594
9695 return 'thread not found' ;
9796 }
9897
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 } ... ` ;
103102 }
104103
105- return 'thread not found ' ;
104+ return 'Thread não encontrada. ' ;
106105 }
107106
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.' ;
115110 }
116111
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` ;
121116 } ) ;
122117
123118 return output . trim ( ) ;
124119 }
125120
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 ) ,
138128 } ;
139129
140- if ( seila [ args [ 0 ] ] ) {
141- return seila [ args [ 0 ] ] ( ) ;
130+ if ( methods [ data . args [ 0 ] ] ) {
131+ return methods [ data . args [ 0 ] ] ( ) ;
142132 }
143133
144- return Cron . default ( ) ;
145- }
146-
147- static isIdValid ( id ) {
148- return threads . some ( ( t ) => t . id === Number ( id ) ) ;
134+ return this . defaultMessage ;
149135 }
150136
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 ) ) ;
168139 }
169140}
170141
0 commit comments