diff options
author | WlodekM <[email protected]> | 2024-07-02 20:55:17 +0300 |
---|---|---|
committer | WlodekM <[email protected]> | 2024-07-02 20:55:17 +0300 |
commit | 908b023f60ca2eec347f68bb4f0c8a9c778278ae (patch) | |
tree | 4bc4a3518492a76898a95997ab4b85afd0d74228 /commands.js | |
parent | 1a4ac8f8015ff22a9d0e19ad523a85d3c01a2139 (diff) |
make 1 global object instead of doing OOP
Diffstat (limited to 'commands.js')
-rw-r--r-- | commands.js | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/commands.js b/commands.js index 9a34409..8aa80a3 100644 --- a/commands.js +++ b/commands.js @@ -1,11 +1,13 @@ +import fs from "fs" + export const commands = { 'join': { name: 'join', aliases: [], - command: function({user, channels, args, sendInChannel}) { + command: function({user, server, args, sendInChannel}) { if(args.length < 1) return user.socket.send("Error: You need to specify a channel (example: /join #home)."); if(!args[0].startsWith("#")) return user.socket.send("Error: Channel not found, run /channels to see a list of channels."); - if(!channels.includes(args[0].replace("#", ""))) 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.username} left #${user.channel}.`, user.channel) user.channel = args[0].replace("#", ""); sendInChannel(`${user.username} joined #${user.channel}!`, user.channel) @@ -14,33 +16,51 @@ export const commands = { 'channels': { name: 'channels', aliases: [], - command: function({user, channels}) { - user.socket.send(`Channels:\n${channels.map(ch => ` * #${ch}`).join("\n")}`) + command: function({user, server}) { + user.socket.send(`Channels:\n${server.channels.map(ch => ` * #${ch}`).join("\n")}`) } }, 'nick': { name: 'nick', aliases: ['nickname', 'name'], - command: function({user, users, args, sendInChannel}) { + command: function({user, server, args, sendInChannel}) { if(args.length < 1) return user.socket.send("Error: You need to specify a nick (example: /nick WlodekM)."); - if(args[0].length < 3 ) return user.socket.send("Error: Nick too long."); - if(args[0].length > 20) return user.socket.send("Error: Nick too short."); - if(Object.values(users).find(usr => usr.username == args[0])) return user.socket.send("Error: Nick already used."); + if(args[0].length < 3 ) return user.socket.send("Error: Nick too short."); + if(args[0].length > 20) return user.socket.send("Error: Nick too long."); + if(Object.values(server.users).find(usr => usr.username == args[0])) return user.socket.send("Error: Nick already used."); sendInChannel(`${user.username} changed their nick to ${args[0]}!`, user.channel) user.username = args[0]; } }, + 'about': { + name: 'about', + aliases: [], + command: function({user, args, sendInChannel}) { + user.socket.send(`wsChat v${JSON.parse(String(fs.readFileSync("package.json"))).version}\nGithub: https://github.com/WlodekM/wsChat`) + } + }, + 'whois': { + name: 'whois', + aliases: [], + command: function({user, server, args}) { + if(args.length < 1) return user.socket.send('Please provide username'); + if(Object.values(server.users).find(usr => usr.username == args[0])) return user.socket.send('User not found'); + let userFound = Object.values(server.users).find(usr => usr.username == args[0]) + userFound.id = Object.keys(server.users).find(usr => server.users[usr].username == args[0]) + user.socket.send(`${userFound.username}\nClient: ${userFound.client ?? "<Unknown>"}\nID: ${userFound.id}`) + } + }, 'users': { name: 'users', aliases: [], - command: function({user, users, args}) { - user.socket.send(`Users${args[0] != "global" ? ` in ${user.channel}` : ""}:\n${Object.values(users).filter(usr => (usr.channel == user.channel) || args[0] == "global").map(usr => ` * ${usr.username}`).join("\n")}`) + command: function({user, server, args}) { + user.socket.send(`Users${args[0] != "global" ? ` in ${user.channel}` : ""}:\n${Object.values(server.users).filter(usr => (usr.channel == user.channel) || args[0] == "global").map(usr => ` * ${usr.username}`).join("\n")}`) } }, 'help': { name: 'help', aliases: ['?'], - command: function({user, users, args, commands}) { + command: function({user, args, commands}) { user.socket.send(`Commands available:\n${Object.values(commands).map(cmd => `* /${cmd.name} (Aliases: ${(cmd.aliases.join(", ")) || "<None>"})`).join("\n")}`) } }, |