summary refs log tree commit diff
path: root/user.js
blob: d3254f7c8366245ee2e8e7401c319874f69892d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// because why not make *more files

import { getRandomInt } from "./lib.js";
import cuid from "cuid";

export default class User {
    /**
     * the user class
     * @param {import("http").IncomingMessage} request request
     * @param {WebSocket} socket socket
     * @param {import("./server.js").Server} server the server
     */
    constructor (request, socket, server) {
        this.id = cuid();
        let anonID = getRandomInt(0, 99999);
        let annonNum = "0".repeat(5 - anonID.toString().length) + anonID.toString()
        this.username = server.config.annonFormat ? server.config.annonFormat.replace('[num]', annonNum) : 'Anonymous' + annonNum;
        this.nickname = server.config.annonFormat ? server.config.annonFormat.replace('[num]', annonNum) : 'Anonymous' + annonNum;
        this.guest = true;
        this.socket = socket;
        this.joinReq = request;
        this.ip = request.headers["x-forwarded-for"] || request.socket.remoteAddress;
        this.t = {
            js: Number(new Date()),
            unix: Math.floor(new Date().getTime() / 1000),
            str: String(new Date()),
        };
        this.channel = "home";
        this.name = function () {
            return this.nickname != "" ? this.nickname : this.username;
        };
    }
}