1+ const fs = require ( 'node:fs' ) ;
2+ const readline = require ( 'readline' ) ;
3+
4+ async function ProcessDOS ( client ) {
5+ const cmd = readline . createInterface ( process . stdin , process . stdout ) ;
6+
7+ let drive = "S"
8+ let dir = [ "\\" ]
9+ let drvlabel = "SERVERS"
10+ let depth = 0
11+ let curServer = null
12+ let curC = null
13+ let drvS = { }
14+ let drvC = { }
15+ let inRepl = false
16+ let object = { }
17+ let pm2 = false
18+ if ( process . env . PM2_USAGE ) pm2 = true
19+ console . log ( "The object 'object' is used for setting a variable inside for use later with the eval command" )
20+ drvS . rootdirs = client . guilds . cache
21+ drvS . subdirs = null
22+ drvC . rootdirs = fs . readdirSync ( `${ __dirname } /../commands` ) ;
23+ drvC . subdirs = null
24+ //console.log(paths)
25+ cmd . setPrompt ( `${ drive } :${ dir . join ( "" ) } >` )
26+ cmd . prompt ( ) ;
27+
28+ cmd . on ( 'line' , async line => {
29+ if ( inRepl ) return
30+ switch ( line . trim ( ) . split ( " " ) [ 0 ] ) {
31+ case 'exit' : {
32+ if ( ! pm2 ) {
33+ cmd . close ( )
34+ await client . destroy ( )
35+ process . exit ( 0 )
36+ }
37+ else if ( pm2 ) console . log ( "You are using pm2, to exit the command line, use Ctrl+C.\nIf you want to reload command file, use reload in the C: drive" )
38+ break
39+ }
40+ case 'eval' : {
41+ try {
42+ console . log ( eval ( line . substring ( 4 ) ) )
43+ } catch ( error ) {
44+ console . error ( error )
45+ }
46+ break
47+ }
48+ case 'help' : case 'man' : {
49+ const log = console . log
50+ log ( "Process DOS help" )
51+ log ( "Commands:\n" )
52+ log ( "eval <code> Evaluates code" )
53+ log ( "status <status> <activity> <description> Changes the status of the bot" )
54+ log ( "send <channel-id> <message> Sends a message on the specified channel" )
55+ log ( "dir Lists the current directory" )
56+ log ( "tail <channel-id> [Amount-of-messages] Lists message in the channel given" )
57+ log ( "type <file> Show the contents of a file" )
58+ log ( "cd [directory] Change directory" )
59+ log ( "cls Clears the screen" )
60+ log ( "echo <text> Displays text on the screen" )
61+ log ( "help Displays this help" )
62+ log ( "exit Terminates the bot and console" )
63+ log ( "reload Reload Command or server, depending on the drive in which executed" )
64+ log ( "deploy Deploy slash (/) commands" )
65+ break
66+ }
67+ case 'status' : {
68+ if ( line . trim ( ) == "status" ) { client . user . setActivity ( ) }
69+ try {
70+ let status = line . split ( " " ) [ 1 ]
71+ let activity = line . split ( " " ) [ 2 ] . toUpperCase ( )
72+ let description = line . split ( " " ) . slice ( 3 ) . join ( " " )
73+ client . user . setActivity ( description , { type : activity } ) ;
74+ client . user . setStatus ( status ) ;
75+ } catch ( error ) {
76+ console . log ( "Invalid status" )
77+ console . error ( error )
78+ }
79+ break
80+ }
81+ case 'dir' : case 'ls' : {
82+ console . log ( `\n Volume in drive ${ drive } is ${ drvlabel } ` )
83+ console . log ( ` Volume Serial Number is 298A-E8CC` )
84+ console . log ( ` Directory of ${ drive } :${ dir . join ( "" ) } \n` )
85+ switch ( drive ) {
86+ case 'S' : {
87+ if ( depth == 0 ) {
88+ drvS . rootdirs . forEach ( async i => {
89+ if ( i . name == undefined ) await i . fetch ( )
90+ console . log ( i . name . padEnd ( 20 ) + ` (${ i . id } )` . padEnd ( 23 ) + "<DIR> " )
91+ } )
92+ } else {
93+ drvS . subdirs = client . guilds . cache . get ( `${ curServer } ` )
94+ drvS . subdirs . channels . cache . forEach ( c => console . log ( `${ c . id . padEnd ( 19 ) } ${ c . name . substring ( 0 , 15 ) . padEnd ( 16 ) } CHN` ) )
95+ }
96+ }
97+ break
98+ case 'C' : {
99+ if ( depth == 0 ) {
100+ drvC . rootdirs . forEach ( i => {
101+ console . log ( i . padEnd ( 20 ) + "<DIR> " )
102+ } )
103+ } else {
104+ drvC . subdirs = fs . readdirSync ( `${ __dirname } /../commands/${ curC } ` ) ;
105+ drvC . subdirs . forEach ( c => console . log ( `${ c . substring ( 0 , 15 ) . padEnd ( 16 ) } FILE` ) )
106+ }
107+ }
108+ }
109+ break
110+ }
111+ case 'cd' : case 'cd..' : {
112+ let newDir = line . split ( " " ) . slice ( 1 ) . join ( " " )
113+ if ( newDir === ".." || line . split ( " " ) [ 0 ] === "cd.." ) {
114+ if ( depth == 0 ) break
115+ depth --
116+ dir . pop ( )
117+ curServer = null
118+ curC = null
119+ break
120+ }
121+ switch ( drive ) {
122+ case 'S' : {
123+ if ( drvS . rootdirs . find ( i => i . name === newDir ) || drvS . rootdirs . find ( i => i . id === newDir ) ) {
124+ depth ++
125+ if ( ! isNaN ( + newDir ) ) {
126+ let server = client . guilds . cache . find ( g => g . id === newDir )
127+ curServer = server . id
128+ dir . push ( server . name )
129+ }
130+ else if ( isNaN ( + newDir ) ) {
131+ let server = client . guilds . cache . find ( g => g . name === newDir )
132+ curServer = server . id
133+ dir . push ( server . name )
134+ }
135+ } else {
136+ console . log ( "Invalid directory" )
137+ }
138+ break
139+ }
140+ case 'C' : {
141+ if ( drvC . rootdirs . find ( i => i === newDir ) ) {
142+ dir . push ( newDir )
143+ depth ++
144+ curC = newDir
145+ } else {
146+ console . log ( "Invalid directory" )
147+ }
148+ }
149+ }
150+ break
151+ }
152+ case 'cls' : case 'clear' : {
153+ console . clear ( ) ;
154+ break
155+ }
156+ case 'send' : {
157+ client . channels . cache . get ( line . split ( " " ) [ 1 ] ) . send ( line . split ( " " ) . slice ( 2 ) . join ( " " ) )
158+ break
159+ }
160+ case 'echo' : {
161+ console . log ( line . split ( " " ) . slice ( 1 ) . join ( "" ) )
162+ break
163+ }
164+ case 'tail' : {
165+ if ( drive === "C" || curServer == null ) { console . log ( "Please, enter a server in the 'S' drive" ) ; break }
166+ let channel = line . split ( " " ) [ 1 ]
167+ if ( ! channel ) { console . log ( 'A channel id is required' ) ; break }
168+ let MessageAmount = line . split ( " " ) [ 2 ] || 10
169+ let Messages = await client . guilds . cache . get ( curServer ) . channels . cache . get ( channel ) . messages . fetch ( { limit : MessageAmount } )
170+ Messages . forEach ( e => {
171+ console . log ( `${ e . id . padEnd ( 20 ) } ${ e . author . tag . padEnd ( 13 ) } ${ e . content } ` )
172+ } )
173+ break
174+ }
175+ case 'type' : case 'cat' : {
176+ let file = line . split ( " " ) . slice ( 1 ) . join ( " " )
177+ if ( ! file ) { console . log ( 'Required parameter missing' ) ; break }
178+ cmd . setPrompt ( " " )
179+ switch ( drive ) {
180+ case 'S' : {
181+ console . log ( 'General failure reading drive S:' )
182+ console . log ( 'Abort, Retry, Fail?a' )
183+ break
184+ }
185+ case 'C' :
186+ fs . readFile ( `${ __dirname } /../commands/${ curC } /${ file . toLowerCase ( ) } ` , 'utf8' , ( err , data ) => {
187+ if ( err ) { console . log ( `File not found - ${ file . toLowerCase ( ) } ` ) ; return }
188+ console . log ( data )
189+ } )
190+ }
191+ console . log ( )
192+ break
193+ }
194+ case "reload" : {
195+ switch ( drive ) {
196+ case "S" : {
197+ console . log ( "Reloading servers, Please wait..." )
198+ await client . guilds . fetch ( )
199+ drvS . rootdirs = client . guilds . cache
200+ console . log ( "Reload finished" )
201+ break
202+ }
203+ case "C" : {
204+ const { ReloadJsFile } = require ( './GetJSFile.js' ) ;
205+ console . log ( "Reloading commands, Please wait..." ) ;
206+ await ReloadJsFile ( client ) ;
207+ console . log ( "Reload finished." ) ;
208+ break
209+ }
210+ } ;
211+ break
212+ }
213+ case "deploy" : {
214+ const { send } = require ( "../deploy-commands.js" ) ;
215+ const { beta } = require ( "../config.json" ) ;
216+
217+ let all = [ ]
218+ client . commands . forEach ( e => { if ( ! beta && e . name !== "test" || beta ) all . push ( e . data . toJSON ( ) ) } )
219+ client . contextMenu . forEach ( e => all . push ( e . data . toJSON ( ) ) )
220+
221+ send ( all , client . token , client . user . id ) ;
222+ break ;
223+ }
224+ case 'c:' : case 'C:' : {
225+ drive = 'C'
226+ drvlabel = 'COMMANDS'
227+ depth = 0
228+ dir = [ "\\" ]
229+ break
230+ }
231+ case 'S:' : case 's:' : {
232+ drive = 'S'
233+ drvlabel = 'SERVERS'
234+ depth = 0
235+ dir = [ "\\" ]
236+ break
237+ }
238+ case '' : break
239+ default : console . log ( "Bad command or file name" )
240+ } ;
241+ cmd . setPrompt ( `${ drive } :${ dir . join ( "" ) } >` )
242+ cmd . prompt ( ) ;
243+ } )
244+ cmd . on ( "SIGINT" , ( ) => {
245+ cmd . prompt ( )
246+ } ) ;
247+ } ;
248+
249+ module . exports = { ProcessDOS }
0 commit comments