-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
165 lines (145 loc) · 4.26 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express');
const rateLimit = require('express-rate-limit');
const fs = require('fs');
const config = require('./config.json');
const { Client } = require('revolt.js');
const DB = require('./db');
const bots = new DB({ file: './data/bots.json' });
const client = new Client();
const app = express();
let commands = new Map();
global.client = client;
global.voted = new DB({ file: './data/voted.json' });
global.sessions = new DB({ file: './data/sessions.json' });
global.loginRequests = {};
global.log = (embed = {}) => {
try {
client.channels.get(config.LOG_CHANNEL).sendMessage({ embeds: embed });
} catch (_) { }
};
global.submit = (embed = {}) => {
try {
client.channels.get(config.SUBMIT_CHANNEL).sendMessage({ embeds: embed });
} catch (_) { }
};
fs.readFileSync('.env', 'utf8')
.split('\n')
.filter((evar) => evar && !evar.startsWith('#'))
.forEach((evar) => {
const [key, value] = evar.split('=');
process.env[key] = value;
});
setInterval(() => {
bots.data = Object.fromEntries(Object.entries(bots.data).sort((a, b) => b[1].v - a[1].v));
bots.save();
log([
{
title: '🧹 LOG • Data sorted',
description: `Data sorted based on votes!`
}
]);
}, 6.6e7);
if (voted.data.bot.length != 0) {
voted.data.bot.forEach((_) => {
setTimeout(() => {
voted.data.bot.shift();
voted.save();
}, 4.32e7);
});
}
if (Object.keys(sessions.data).length != 0) {
Object.keys(sessions.data).forEach((key) => {
setTimeout(() => {
delete sessions.data[key];
sessions.save();
}, 8.64e7);
});
}
const limiter = rateLimit({
windowMs: config.WINDOW_MS,
max: config.MAX_REQUESTS,
standardHeaders: true,
legacyHeaders: false,
handler: (_, response, _1, _2) => {
response.status(429).json({
error: true,
status: 429,
message: 'Too many requests!'
});
}
});
app.set('view engine', 'ejs');
app.set('json spaces', 4);
app.use(limiter);
app.use((_, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
next();
});
fs.readdirSync('./commands', {
withFileTypes: true
}).forEach((dir) => {
if (dir.isFile()) return;
const commandFiles = fs.readdirSync(`${'./commands'}/${dir.name}/`).filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${dir.name}/${file}`);
const cmdName = file.split('.')[0];
commands.set(cmdName, command);
if (command.alias) {
if (Array.isArray(command.alias)) {
command.alias.forEach((e) => {
commands.set(e, command);
});
} else {
commands.set(command.alias, command);
}
}
console.log('\x1b[94m[+] Loaded ' + cmdName + '\x1b[0m');
}
});
app.get('/', (_, res) => {
res.json({
api: 'working'
});
});
client.on('ready', async () => {
console.info(`Logged in as ${client.user.username}!`);
app.use('/user', require('./routes/user'));
app.use('/server', require('./routes/server'));
app.use('/botlist', require('./routes/botlist'));
app.use('/requestlogin', require('./routes/requestlogin'));
app.use('/checklogin', require('./routes/checklogin'));
app.use('/submit-bot', require('./routes/submit-bot'));
app.use('/vote', require('./routes/vote'));
app.use('/getsession', require('./routes/getsession'));
app.use('/edit-bot', require('./routes/edit-bot'));
log([{ title: '🟢 LOG • Server started' }]);
});
client.on('messageCreate', async (message) => {
if (message.authorId == client.user.id || message.author.bot != undefined) return;
if (String(message.content).startsWith('r!')) {
const args = String(message.content).split(' ');
const cmdName = args.shift().replace('r!', '');
const userMsg = String(message.content)
.replace('r!' + cmdName, '')
.replace(' ', '');
const cmd = commands.get(cmdName);
if (!cmd) return;
cmd.run(client, message, args, userMsg);
}
});
app.listen(config.PORT, () => {
console.log(`Server is running on port ${config.PORT}`);
});
process.on('uncaughtException', (err) => {
log([{ title: '🔴 LOG • Server uncaughtException' }]);
console.error(err);
});
process.on('SIGINT', () => {
log([{ title: '🔴 LOG • Server SIGINT' }]);
setTimeout(() => {
process.exit();
}, 1000);
});
client.loginBot(process.env['TOKEN']);