summaryrefslogtreecommitdiff
path: root/ws/ws.js
blob: 81f01596493b2da56f6db2129a9aef9e9a196645 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express');
const hbs = require('express-handlebars');
const bodyParser = require('body-parser');
const path = require('path');

class WebSocket {
    constructor(token, port, client) {
        this.token = token;
        this.client = client;

        this.app = express();
        this.app.engine('hbs', hbs({
            extname: 'hbs',
            defaultLayout: 'layout',
            layoutsDir: __dirname + '/layouts'
        }));
        this.app.set('views', path.join(__dirname, 'views'));
        this.app.set('view engine', 'hbs');
        this.app.use(express.static(path.join(__dirname, 'public')));
        this.app.use(bodyParser.urlencoded({ extended: false }));
        this.app.use(bodyParser.json());

        this.registerRoots();

        this.server = this.app.listen(port, () => {
            console.log(`Websocket listening on port ${this.server.address().port}`);
        });
    }

    checkToken(_token) {
        return (_token == this.token);
    }
    
    checkUwufier() {
        let status = this.client.users.cache.get('699473263998271489').presence.status;
        let presence = this.client.user.presence.activities[0].name;
        //console.log(this.client.users.cache.get('699473263998271489').presence)
        var statusEnd;
        if (presence == 'maintenance') {
            return statusEnd = 'maintenance';
        } else if (status == 'online') {
            return statusEnd = 'online';
        } else {
            return statusEnd = 'offline';
        }
    }

    // http://localhost:port?token=123456
    registerRoots() {
        this.app.all('*', function (req, res, next) {
            res.header('Access-Control-Allow-Origin', '*');
            res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
            res.header('Access-Control-Allow-Headers', 'Content-Type');
            next();
        });
        this.app.get('/', (req, res) => {
            var _token = req.query.token;
            if (this.checkToken(_token)) {
                if (this.checkUwufier() == 'online') {
                    res.render('index', {
                        token: _token,
                        statustype: 'online'
                    });
                    return;
                } else if (this.checkUwufier() == 'maintenance') {
                    res.render('index', {
                        token: _token,
                        statustype: 'maintenance'
                    });
                    return;
                } else {
                    res.render('index', {
                        statustype: 'offline'
                    });
                    return;
                }
            } else {
                res.render('index', {
                    statustype: 'invalid token'
                });
                return;
            }
        });
    }
}

module.exports = WebSocket;