diff options
author | WlodekM <[email protected]> | 2025-01-22 10:18:16 +0200 |
---|---|---|
committer | WlodekM <[email protected]> | 2025-01-22 10:18:16 +0200 |
commit | 2e3e1a555e97ab84d5b7e814ec75318e4bdf8809 (patch) | |
tree | 97820a5d5200590fa934e4ff33707fd66271fc14 | |
parent | 14c92b10915543aa760374ce398bc0b3b95f90f6 (diff) |
some fixes
-rw-r--r-- | commands.ts | 6 | ||||
-rw-r--r-- | logger.ts | 20 | ||||
-rw-r--r-- | server.ts | 19 |
3 files changed, 33 insertions, 12 deletions
diff --git a/commands.ts b/commands.ts index 961000d..960b8a8 100644 --- a/commands.ts +++ b/commands.ts @@ -82,9 +82,9 @@ export const commands: {[key: string]: Command} = { command({ user, server, args }) { if (args.length < 1) return user.socket.send("Please provide username"); if (![...server.users.entries()].find(([_, usr]) => usr.username == args[0])) return user.socket.send("User not found"); - const userFound = Object.values(server.users).find((usr) => usr.username == args[0]); - userFound.id = Object.keys(server.users).find((usr) => (server.users.get(usr) as User).username == args[0]); - user.socket.send(`${userFound.username}\nClient: ${userFound.client ?? "<Unknown>"}\nID: ${userFound.id}`); + const [id, userFound] = [...server.users.entries()].find(([_, usr]) => usr.username == args[0]) ?? []; + if (!userFound) return user.socket.send("User not found"); + user.socket.send(`${userFound.username}\nClient: ${userFound.client ?? "<Unknown>"}\nID: ${id}`); }, }, users: { diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..5228af5 --- /dev/null +++ b/logger.ts @@ -0,0 +1,20 @@ +import * as fs from 'node:fs' + +function logAndPrint(text: string, logShit: any[]) { + if (!fs.existsSync('server.log')) + fs.writeFileSync('server.log', ''); + fs.appendFileSync('server.log', '\n' + text); + console.log(...logShit) +} + +export function log(text: any) { + logAndPrint(String(text), [text]) +} + +export function info(text: any) { + logAndPrint('[INFO] ' + text, ['[INFO]', text]) +} + +export function error(text: any) { + logAndPrint('[ERR] ' + text, ['[ERR]', text]) +} \ No newline at end of file diff --git a/server.ts b/server.ts index f58d17b..68edc1f 100644 --- a/server.ts +++ b/server.ts @@ -6,6 +6,7 @@ import fs from "node:fs"; import User from "./user.ts"; import handleJsonMessage from './jsondata.js' import { EventEmitter } from "node:events"; +import * as logger from './logger.ts'; // TODO - actually make this class _Events { // JSON events for clients @@ -100,19 +101,19 @@ export default class Server { return; } socket.send(server.format(server.config.server.motd)); - console.info(`${user.name()}[${user.id}] joined the server!`); + logger.log(`${user.name()}[${user.id}] joined the server!`); server.sendInChannel(`${user.name()} joined.`, user.channel); server.updateUsers(); socket.on("close", function () { server.sendInChannel(`${user.name()} left.`, user.channel); server.updateUsers(); - console.info(`${user.name()}[${user.id}] left (close)`) + logger.log(`${user.name()}[${user.id}] left (close)`) server.users.delete(user.id); }); socket.on('error', function () { server.sendInChannel(`${user.name()} left.`, user.channel); server.updateUsers(); - console.info(`${user.name()}[${user.id}] left (error)`) + logger.log(`${user.name()}[${user.id}] left (error)`) server.users.delete(user.id); }) socket.on("message", function (rawData) { @@ -122,7 +123,7 @@ export default class Server { const command: string = args.shift() as string; // TODO: make command class const commandObj: Command = Object.values(commands).find((cmd) => cmd.name == command || cmd.aliases.includes(command)) as Command; - console.log(`${user.name()} used /${command}`); + logger.log(`${user.name()} used /${command}`); if (!commandObj) return socket.send(`Error: Command "${command}" not found!`); try { commandObj.command.call(server, { @@ -130,14 +131,14 @@ export default class Server { command, args, sendInChannel: function (msg: string, channel: string) { - console.log(msg, channel) + logger.log(msg + ' ' + channel) server.sendInChannel(msg, channel, server); }, server: server, commands, }); } catch (error) { - console.error(error); + logger.error(error); user.socket.send(`Unexpected error ocurred while running the command`); } return; @@ -163,7 +164,7 @@ export default class Server { if (data.length < 1) return socket.send("Error: message too short!"); if (data.length >= 2000) return socket.send("Error: message too long!"); server.sendInChannel(`${user.admin ? '[ADMIN] ' : ''}<${user.name()}${user.guest ? " (guest)" : ""}> ${data}`, user.channel); - console.log(`(#${user.channel}) <${user.name()}> ${data}`); + logger.log(`(#${user.channel}) <${user.name()}> ${data}`); }); } catch (error) { socket.send(`ERROR ${error}`); @@ -171,12 +172,12 @@ export default class Server { } }); this.ws.on("listening", () => { - console.info("Server started!"); + logger.info("Server started!"); }); } sendInChannel(msg: string, channel: string, server=this) { - // console.log('this is a', this) + // logger.log('this is a', this) for (const [userID] of server.users.entries()) { const user = server.users.get(userID) as User; if (user.channel == channel) user.socket.send(msg); |