From 714a4189eb02051ddd330513048b07b3be070e27 Mon Sep 17 00:00:00 2001 From: WlodekM Date: Sat, 30 Nov 2024 14:24:39 +0200 Subject: do stuff 3 --- commands.ts | 16 ++++++++++++---- commands/ban.js | 17 ----------------- commands/ban.ts | 18 ++++++++++++++++++ commands/kick.js | 11 ----------- commands/kick.ts | 13 +++++++++++++ commands/motd.js | 7 ------- commands/motd.ts | 9 +++++++++ lib.js | 15 --------------- lib.ts | 15 +++++++++++++++ main.ts | 5 +++++ newindex.js | 5 ----- oldindex.js | 2 +- ranks.js | 20 -------------------- ranks.ts | 20 ++++++++++++++++++++ user.ts | 2 +- 15 files changed, 94 insertions(+), 81 deletions(-) delete mode 100644 commands/ban.js create mode 100644 commands/ban.ts delete mode 100644 commands/kick.js create mode 100644 commands/kick.ts delete mode 100644 commands/motd.js create mode 100644 commands/motd.ts delete mode 100644 lib.js create mode 100644 lib.ts create mode 100644 main.ts delete mode 100644 newindex.js delete mode 100644 ranks.js create mode 100644 ranks.ts diff --git a/commands.ts b/commands.ts index fbde383..e416aad 100644 --- a/commands.ts +++ b/commands.ts @@ -3,14 +3,21 @@ import type User from "./user.ts"; import type Server from "./server.ts"; import { ChannelConfig } from "./server.ts"; +export type CommandFnArgs = { + user: User, + server: Server, + args: string[], + sendInChannel: (msg: string, channel: string, server?: Server) => void, + commands: {[key: string]: Command} +} + // NOTE - temporary, will make class later export type Command = { name: string, usage?: string, description: string, aliases: string[], - command: ({ user, server, args, sendInChannel, commands }: - { user: User, server: Server, args: string[], sendInChannel: (msg: string, channel: string, server?: Server) => void, commands: {[key: string]: Command} }) => void + command: ({ user, server, args, sendInChannel, commands }: CommandFnArgs) => void } export const commands: {[key: string]: Command} = { @@ -178,8 +185,9 @@ export function register(cmd: string, data: Command) { const commandFiles = fs .readdirSync("commands") - .filter((filename) => filename.endsWith(".js") || filename.endsWith(".ts")) - .map((file) => file.replace(/\.js$/gim, "")); + .filter((filename: string) => filename.endsWith(".js") || filename.endsWith(".ts")) + .map((file: string) => file.replace(/\.js$/gim, "")); + for (let i = 0; i < commandFiles.length; i++) { const cmdName = commandFiles[i]; const cmd = (await import(`./commands/${cmdName}.js`)).default; diff --git a/commands/ban.js b/commands/ban.js deleted file mode 100644 index 584b4d0..0000000 --- a/commands/ban.js +++ /dev/null @@ -1,17 +0,0 @@ -import fs from "node:fs"; - -export default { - name: "ban", - aliases: [], - command: function ({ user, server, args }) { - if(!user.admin) return user.socket.send("You are not admin"); - if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found"); - let theUser = Object.values(server.users).find(a => a.username == args[0]); - theUser.socket.send('you were kicked because you were banned, L'); - let ipBanList = JSON.parse(String(fs.readFileSync("db/bannedIps.json"))); - ipBanList[theUser.ip] = args[1] ?? 'reason'; - ipBanList['account:'+args[0]] = args[1] ?? 'reason'; - theUser.socket.close(1003, "Kicked"); - fs.writeFileSync('db/bannedIps.json', JSON.stringify(ipBanList)); - }, -}; diff --git a/commands/ban.ts b/commands/ban.ts new file mode 100644 index 0000000..77d23b5 --- /dev/null +++ b/commands/ban.ts @@ -0,0 +1,18 @@ +import fs from "node:fs"; +import { CommandFnArgs } from "../commands.ts" + +export default { + name: "ban", + aliases: [], + command: function ({ user, server, args }: CommandFnArgs) { + if(!user.admin) return user.socket.send("You are not admin"); + if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found"); + const theUser = Object.values(server.users).find(a => a.username == args[0]); + theUser.socket.send('you were kicked because you were banned, L'); + const ipBanList = JSON.parse(String(fs.readFileSync("db/bannedIps.json"))); + ipBanList[theUser.ip] = args[1] ?? 'reason'; + ipBanList['account:'+args[0]] = args[1] ?? 'reason'; + theUser.socket.close(1003, "Kicked"); + fs.writeFileSync('db/bannedIps.json', JSON.stringify(ipBanList)); + }, +}; diff --git a/commands/kick.js b/commands/kick.js deleted file mode 100644 index accadb0..0000000 --- a/commands/kick.js +++ /dev/null @@ -1,11 +0,0 @@ -export default { - name: "kick", - aliases: [], - command: function ({ user, server, args }) { - if(!user.admin) return user.socket.send("You are not admin"); - if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found"); - let theUser = Object.values(server.users).find(a => a.username == args[0]); - theUser.socket.send('you were kicked') - theUser.socket.close(1003, "Kicked"); - }, -}; diff --git a/commands/kick.ts b/commands/kick.ts new file mode 100644 index 0000000..60568ed --- /dev/null +++ b/commands/kick.ts @@ -0,0 +1,13 @@ +import { CommandFnArgs } from "../commands.ts" + +export default { + name: "kick", + aliases: [], + command: function ({ user, server, args }: CommandFnArgs) { + if(!user.admin) return user.socket.send("You are not admin"); + if(!server.accounts.checkAccount(args[0])) return user.socket.send("Account not found"); + const theUser = Object.values(server.users).find(a => a.username == args[0]); + theUser.socket.send('you were kicked') + theUser.socket.close(1003, "Kicked"); + }, +}; diff --git a/commands/motd.js b/commands/motd.js deleted file mode 100644 index a4e7fde..0000000 --- a/commands/motd.js +++ /dev/null @@ -1,7 +0,0 @@ -export default { - name: "motd", - aliases: [], - command: function ({ user, server }) { - user.socket.send("MOTD: " + server.format(server.config.motd)); - }, -}; diff --git a/commands/motd.ts b/commands/motd.ts new file mode 100644 index 0000000..43293c9 --- /dev/null +++ b/commands/motd.ts @@ -0,0 +1,9 @@ +import { CommandFnArgs } from "../commands.ts" + +export default { + name: "motd", + aliases: [], + command: function ({ user, server }: CommandFnArgs) { + user.socket.send("MOTD: " + server.format(server.config.server.motd)); + }, +}; diff --git a/lib.js b/lib.js deleted file mode 100644 index 9038650..0000000 --- a/lib.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Returns a random integer between min (inclusive) and max (inclusive). - * The value is no lower than min (or the next integer greater than min - * if min isn't an integer) and no greater than max (or the next integer - * lower than max if max isn't an integer). - * Using Math.round() will give you a non-uniform distribution! - * @param {Number} min Min number - * @param {Number} max Max number - * @returns {Number} - */ -export function getRandomInt(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; -} diff --git a/lib.ts b/lib.ts new file mode 100644 index 0000000..1103206 --- /dev/null +++ b/lib.ts @@ -0,0 +1,15 @@ +/** + * Returns a random integer between min (inclusive) and max (inclusive). + * The value is no lower than min (or the next integer greater than min + * if min isn't an integer) and no greater than max (or the next integer + * lower than max if max isn't an integer). + * Using Math.round() will give you a non-uniform distribution! + * @param min Min number + * @param max Max number + * @returns {Number} + */ +export function getRandomInt(min: number, max: number) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..3e07868 --- /dev/null +++ b/main.ts @@ -0,0 +1,5 @@ +import Server from './server.ts'; +import ini from "ini" +import fs from "node:fs" + +new Server(ini.parse(String(fs.readFileSync("config.ini")))) diff --git a/newindex.js b/newindex.js deleted file mode 100644 index 3e07868..0000000 --- a/newindex.js +++ /dev/null @@ -1,5 +0,0 @@ -import Server from './server.ts'; -import ini from "ini" -import fs from "node:fs" - -new Server(ini.parse(String(fs.readFileSync("config.ini")))) diff --git a/oldindex.js b/oldindex.js index 8acaa19..6e488e5 100644 --- a/oldindex.js +++ b/oldindex.js @@ -1,5 +1,5 @@ import { WebSocketServer } from "ws"; -import { getRandomInt } from "./lib.js"; +import { getRandomInt } from "./lib.ts"; import { profanity } from "@2toad/profanity"; import { commands } from "./commands.ts"; import * as accounts from "./accounts.ts"; diff --git a/ranks.js b/ranks.js deleted file mode 100644 index 78cab46..0000000 --- a/ranks.js +++ /dev/null @@ -1,20 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; -import * as accounts from './accounts.ts' -import { commands } from "./commands.ts"; - -export function getRankData(name) { - if (!/^[^\/\\]*$/g.exec(name)) return null; - if (!fs.existsSync(path.join('ranks', `${name}.json`))) return null; - return JSON.parse(fs.readFileSync(path.join('ranks', `${name}.json`))) -} - -export function canUserDoCommand(command, username, guest=false) { - let permissionLevel = 0; - if (!guest) { - const accountData = accounts.getAccountData(username); - if (getRankData(accountData?.admin ? 'admin' : accountData?.rank)) permissionLevel = getRankData(accountData?.admin ? 'admin' : accountData?.rank).level; - } - // Banned users can be given a rank with negative permissions so that no commands can be ran - return permissionLevel >= commands[command]?.level ?? 0 -} \ No newline at end of file diff --git a/ranks.ts b/ranks.ts new file mode 100644 index 0000000..78cab46 --- /dev/null +++ b/ranks.ts @@ -0,0 +1,20 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import * as accounts from './accounts.ts' +import { commands } from "./commands.ts"; + +export function getRankData(name) { + if (!/^[^\/\\]*$/g.exec(name)) return null; + if (!fs.existsSync(path.join('ranks', `${name}.json`))) return null; + return JSON.parse(fs.readFileSync(path.join('ranks', `${name}.json`))) +} + +export function canUserDoCommand(command, username, guest=false) { + let permissionLevel = 0; + if (!guest) { + const accountData = accounts.getAccountData(username); + if (getRankData(accountData?.admin ? 'admin' : accountData?.rank)) permissionLevel = getRankData(accountData?.admin ? 'admin' : accountData?.rank).level; + } + // Banned users can be given a rank with negative permissions so that no commands can be ran + return permissionLevel >= commands[command]?.level ?? 0 +} \ No newline at end of file diff --git a/user.ts b/user.ts index 910e8a4..c85c2f6 100644 --- a/user.ts +++ b/user.ts @@ -1,6 +1,6 @@ // because why not make *more files -import { getRandomInt } from "./lib.js"; +import { getRandomInt } from "./lib.ts"; import cuid2 from "@paralleldrive/cuid2"; import type { WebSocket } from 'ws' import type { IncomingMessage } from "node:http"; -- cgit 1.4.1-2-gfad0