use camelcase file names from now on...
This commit is contained in:
95
src/managers/commandManager.ts
Normal file
95
src/managers/commandManager.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import * as readline from 'readline'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
class CommandManager {
|
||||
private commands: Map<string, Function> = new Map()
|
||||
private rl: readline.Interface
|
||||
private io: Server | null = null
|
||||
private rlClosed: boolean = false
|
||||
|
||||
constructor() {
|
||||
this.rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
|
||||
this.rl.on('close', () => {
|
||||
this.rlClosed = true
|
||||
})
|
||||
}
|
||||
|
||||
public async boot(io: Server) {
|
||||
this.io = io
|
||||
await this.loadCommands()
|
||||
console.log('[✅] Command manager loaded')
|
||||
this.startPrompt()
|
||||
}
|
||||
|
||||
private startPrompt() {
|
||||
if (this.rlClosed) return
|
||||
|
||||
this.rl.question('> ', (command: string) => {
|
||||
this.processCommand(command)
|
||||
this.startPrompt()
|
||||
})
|
||||
}
|
||||
|
||||
private async processCommand(command: string): Promise<void> {
|
||||
const [cmd, ...args] = command.trim().split(' ')
|
||||
if (this.commands.has(cmd)) {
|
||||
this.commands.get(cmd)?.(args, this.io as Server)
|
||||
} else {
|
||||
this.handleUnknownCommand(cmd)
|
||||
}
|
||||
}
|
||||
|
||||
private handleUnknownCommand(command: string) {
|
||||
switch (command) {
|
||||
case 'exit':
|
||||
console.log('Goodbye!')
|
||||
this.rl.close()
|
||||
break
|
||||
default:
|
||||
console.error(`Unknown command: ${command}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
private async loadCommands() {
|
||||
const commandsDir = path.resolve(__dirname, 'commands')
|
||||
try {
|
||||
const files: string[] = await fs.promises.readdir(commandsDir)
|
||||
|
||||
for (const file of files) {
|
||||
await this.loadCommand(commandsDir, file)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[❌] Failed to read commands directory:', error)
|
||||
}
|
||||
}
|
||||
|
||||
private async loadCommand(commandsDir: string, file: string) {
|
||||
try {
|
||||
const ext = path.extname(file)
|
||||
const commandName = path.basename(file, ext)
|
||||
const commandPath = path.join(commandsDir, file)
|
||||
const module = await import(commandPath)
|
||||
|
||||
this.registerCommand(commandName, module.default)
|
||||
} catch (error) {
|
||||
console.error('[❌] Failed to load command:', file, error)
|
||||
}
|
||||
}
|
||||
|
||||
private registerCommand(name: string, command: (args: string[], io: Server) => void) {
|
||||
if (this.commands.has(name)) {
|
||||
console.warn(`Command '${name}' is already registered. Overwriting...`)
|
||||
}
|
||||
this.commands.set(name, command)
|
||||
console.log(`Registered command: ${name}`)
|
||||
}
|
||||
}
|
||||
|
||||
export default new CommandManager()
|
Reference in New Issue
Block a user