forked from noxious/server
More work on commands / sending notifications
This commit is contained in:
parent
6a995a83ab
commit
e9d2eb905a
@ -1,14 +1,81 @@
|
||||
import * as readline from 'readline';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {Server} from "socket.io";
|
||||
|
||||
class CommandManager
|
||||
{
|
||||
class CommandManager {
|
||||
private commands: Map<string, Function> = new Map();
|
||||
private rl: readline.Interface;
|
||||
private io: Server|null = null;
|
||||
|
||||
public async boot() {
|
||||
console.log('[✅] Command manager loaded');
|
||||
public constructor() {
|
||||
this.rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
this.loadCommands();
|
||||
}
|
||||
|
||||
// Scan for files
|
||||
public async boot(io: Server) {
|
||||
this.io = io as Server;
|
||||
console.log('[✅] Command manager loaded');
|
||||
await this.startPrompt();
|
||||
}
|
||||
|
||||
private async startPrompt() {
|
||||
this.rl.question('> ', (command: string) => {
|
||||
this.processCommand(command);
|
||||
this.startPrompt();
|
||||
});
|
||||
}
|
||||
|
||||
private async processCommand(command: string): Promise<any> {
|
||||
const [cmd, ...args] = command.trim().toLowerCase().split(' ');
|
||||
if (this.commands.has(cmd)) {
|
||||
this.commands.get(cmd)?.(args, this.io as Server);
|
||||
} else {
|
||||
switch (cmd) {
|
||||
case 'exit':
|
||||
console.log('Goodbye!');
|
||||
this.rl.close();
|
||||
process.exit(0);
|
||||
break;
|
||||
default:
|
||||
console.log(`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) {
|
||||
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: any) {
|
||||
console.error('[❌] Failed to load command files:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
export default new CommandManager();
|
||||
|
9
src/app/commands/alert.ts
Normal file
9
src/app/commands/alert.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Server } from "socket.io";
|
||||
|
||||
type CommandInput = string[]
|
||||
|
||||
export default function(input: CommandInput, io: Server) {
|
||||
let message: string = input.join(' ') ?? null;
|
||||
if (!message) return console.log('message is required');
|
||||
io.emit('notification', {message: message});
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { Socket, Server } from "socket.io";
|
||||
import { Server } from "socket.io";
|
||||
import {TSocket} from "../utilities/Types";
|
||||
import {Character} from "@prisma/client";
|
||||
import CharacterRepository from "../repositories/CharacterRepository";
|
||||
|
@ -66,7 +66,7 @@ export class Server
|
||||
await ZoneManager.boot();
|
||||
|
||||
// Load command manager
|
||||
await CommandManager.boot();
|
||||
await CommandManager.boot(this.io);
|
||||
|
||||
// Listen for socket connections
|
||||
this.io.on('connection', this.handleConnection.bind(this));
|
||||
|
Loading…
x
Reference in New Issue
Block a user