@@ -4,80 +4,73 @@ const fs = require("fs");
4
4
// The function has 2 parts to it. The first part is that which deletes all the channels from the saved file that are deleted in the channel.
5
5
// In the second part, we delete all the temporary channels that have no user in it.
6
6
7
- /* eslint-disable */
8
-
9
7
module . exports = {
10
8
name : "ready" ,
11
9
once : true ,
12
10
execute ( client ) {
13
11
const timer = setInterval ( function ( ) {
14
- // const temp_data = [];
15
12
// Reading data from the file
16
13
fs . readFile ( "./data/createvc.json" , "utf-8" , ( err , jsonString ) => {
17
14
if ( err ) {
18
15
console . log ( "Error reading file from disk:" , err ) ;
19
16
return ;
20
17
} else {
21
- // Converting the data to a dictionary
22
- const data = JSON . parse ( jsonString ) ;
23
-
24
- // Deleting all the channels, that should have been deleted
25
- const b = data . channels . filter ( ( e ) => e . delete == true ) ;
26
- b . forEach ( ( f ) =>
27
- data . channels . splice (
28
- data . channels . findIndex ( ( e ) => e . delete === f . delete ) ,
29
- 1 ,
30
- ) ,
31
- ) ;
32
-
33
- fs . writeFileSync (
34
- "./data/createvc.json" ,
35
- JSON . stringify ( { users : data . users , channels : data . channels } , null , 4 ) ,
36
- ) ;
37
-
38
- // console.log(data);
39
-
40
- data . channels . forEach ( ( item ) => {
41
- // item here is the channel id
42
- if ( item . delete == false ) {
43
- client . channels
44
- . fetch ( item . channel_id )
45
- . then ( ( channel ) => {
46
- channel
47
- . fetch ( )
48
- . then ( ( channel ) => {
49
- if ( channel . members . size == 0 ) {
50
- // console.log(channel.name);
51
- // console.log(channel.members.size);
52
-
53
- item . delete = true ;
54
- fs . writeFileSync (
55
- "./data/createvc.json" ,
56
- JSON . stringify (
57
- {
58
- users : data . users ,
59
- channels : data . channels ,
60
- } ,
61
- null ,
62
- 4 ,
63
- ) ,
64
- ) ;
65
- channel
66
- . delete ( )
67
- . then ( console . log )
68
- . catch ( console . error ) ;
69
- }
70
- } )
71
- . catch ( console . error ) ;
72
- } )
73
- . catch ( console . error ) ;
74
- }
75
- } ) ;
76
-
77
- // console.log(data.channels);
18
+ deleteExistentChannels ( client , jsonString ) ;
78
19
}
79
20
} ) ;
80
21
// Write back to the file
81
22
} , 60 * 60 * 1000 ) ;
82
23
} ,
83
24
} ;
25
+
26
+ function deleteExistentChannels ( client , jsonString ) {
27
+ // Converting the data to a dictionary
28
+ const data = JSON . parse ( jsonString ) ;
29
+
30
+ // Deleting all the channels, that should have been deleted
31
+ const b = data . channels . filter ( ( e ) => e . delete == true ) ;
32
+ b . forEach ( ( f ) =>
33
+ data . channels . splice (
34
+ data . channels . findIndex ( ( e ) => e . delete === f . delete ) ,
35
+ 1 ,
36
+ ) ,
37
+ ) ;
38
+
39
+ fs . writeFileSync (
40
+ "./data/createvc.json" ,
41
+ JSON . stringify ( { users : data . users , channels : data . channels } , null , 4 ) ,
42
+ ) ;
43
+
44
+ // console.log(data);
45
+
46
+ data . channels . forEach ( ( item ) => {
47
+ // item here is the channel id
48
+ if ( item . delete == false ) {
49
+ client . channels
50
+ . fetch ( item . channel_id )
51
+ . then ( ( channel ) => {
52
+ channel
53
+ . fetch ( )
54
+ . then ( ( vcChannel ) => {
55
+ if ( vcChannel . members . size == 0 ) {
56
+ item . delete = true ;
57
+ fs . writeFileSync (
58
+ "./data/createvc.json" ,
59
+ JSON . stringify (
60
+ {
61
+ users : data . users ,
62
+ channels : data . channels ,
63
+ } ,
64
+ null ,
65
+ 4 ,
66
+ ) ,
67
+ ) ;
68
+ vcChannel . delete ( ) . then ( console . log ) . catch ( console . error ) ;
69
+ }
70
+ } )
71
+ . catch ( console . error ) ;
72
+ } )
73
+ . catch ( console . error ) ;
74
+ }
75
+ } ) ;
76
+ }
0 commit comments