From 6cb6085e508b3b72697924490bad517546bdec71 Mon Sep 17 00:00:00 2001 From: WlodekM Date: Sat, 30 Nov 2024 12:10:08 +0200 Subject: do stuff --- commands.ts | 12 ++++++++---- commands/ban.js | 2 +- server.ts | 24 ++++++++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/commands.ts b/commands.ts index 095c534..8a93c8a 100644 --- a/commands.ts +++ b/commands.ts @@ -1,6 +1,7 @@ import fs from "node:fs"; import type User from "./user.ts"; import type Server from "./server.ts"; +import { ChannelConfig } from "./server.ts"; // NOTE - temporary, will make class later export type Command = { @@ -23,10 +24,13 @@ export const commands: {[key: string]: Command} = { if (!args[0].startsWith("#")) return user.socket.send("Error: Channel not found, run /channels to see a list of channels."); if (!server.channels.includes(args[0].replace("#", ""))) return user.socket.send("Error: Channel not found, run /channels to see a list of channels."); sendInChannel(`${user.name()} left #${user.channel}.`, user.channel); - user.channel = args[0].replace("#", ""); + const newChannel = args[0].replace("#", "") + console.info(`${user.name()} went to #${newChannel}`); + sendInChannel(`${user.name()} joined #${newChannel}!`, newChannel); + user.channel = newChannel; server.updateUsers(); - console.info(`${user.name()} went to #${user.channel}`); - sendInChannel(`${user.name()} joined #${user.channel}!`, user.channel); + const channelConfig: ChannelConfig = server.config['channels-'+newChannel] as ChannelConfig; + user.socket.send(channelConfig?.motd ?? `You joined #${newChannel}`) }, }, channels: { @@ -174,7 +178,7 @@ export function register(cmd: string, data: Command) { const commandFiles = fs .readdirSync("commands") - .filter((filename) => filename.endsWith(".js")) + .filter((filename) => filename.endsWith(".js") || filename.endsWith(".ts")) .map((file) => file.replace(/\.js$/gim, "")); for (let i = 0; i < commandFiles.length; i++) { const cmdName = commandFiles[i]; diff --git a/commands/ban.js b/commands/ban.js index db681d1..584b4d0 100644 --- a/commands/ban.js +++ b/commands/ban.js @@ -1,4 +1,4 @@ -import fs from "fs"; +import fs from "node:fs"; export default { name: "ban", diff --git a/server.ts b/server.ts index f3bc077..f58d17b 100644 --- a/server.ts +++ b/server.ts @@ -12,7 +12,7 @@ class _Events { // JSON events for clients } -type ServerConfig = { +export type ServerConfig = { port: number name: string motd: string @@ -20,27 +20,28 @@ type ServerConfig = { max: number } -type AccountsConfig = { +export type AccountsConfig = { owner: string saveIP: boolean requireLogin: boolean annonFormat: string } -type ProfanityConfig = { +export type ProfanityConfig = { filter: boolean removeWords: string addWords: string } -type ChannelsConfig = { +export type ChannelsConfig = { channels: string } -type ChannelConfig = { +export type ChannelConfig = { slowmode?: number requireLogin?: boolean profanity?: boolean + motd?: string } type addPrefixToObject = { @@ -105,8 +106,15 @@ export default class Server { socket.on("close", function () { server.sendInChannel(`${user.name()} left.`, user.channel); server.updateUsers(); + console.info(`${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)`) + server.users.delete(user.id); + }) socket.on("message", function (rawData) { let data: string = rawData.toString() if (data.toString().startsWith("/")) { @@ -144,7 +152,11 @@ export default class Server { } if(handleJsonMessage(server, data, user)) return; const thisChannelConfig: ChannelConfig | undefined = server.config['channel-'+user.channel] as ChannelConfig; - if (server.config.accounts.requireLogin && user.guest && thisChannelConfig?.requireLogin != undefined && !thisChannelConfig.requireLogin) + if (server.config.accounts.requireLogin && + user.guest && + !(thisChannelConfig?.requireLogin != undefined && + thisChannelConfig?.requireLogin) || + thisChannelConfig?.requireLogin && user.guest) return socket.send("This server requires you to log in, use /login to log in or /register to make an account."); profanity.options.grawlixChar = "*"; if (server.config.profanity.filter) data = profanity.censor(String(data)); -- cgit 1.4.1-2-gfad0