summary refs log tree commit diff
path: root/ranks.ts
blob: 78cab4687c42540b5a574e0f51ac10b4b9f193f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
}