summary refs log tree commit diff
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2024-11-30 12:10:08 +0200
committerWlodekM <[email protected]>2024-11-30 12:10:08 +0200
commit6cb6085e508b3b72697924490bad517546bdec71 (patch)
treefea72feea7b524514a21a95bdf9153acc6cd4257
parent7bf34fb819f12e551ee8e50dcfd7e0369a68eb8e (diff)
do stuff
-rw-r--r--commands.ts12
-rw-r--r--commands/ban.js2
-rw-r--r--server.ts24
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<T, P extends string> = {
@@ -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 <username> <password> to log in or /register <username> <password> to make an account.");
                     profanity.options.grawlixChar = "*";
                     if (server.config.profanity.filter) data = profanity.censor(String(data));