-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
38 lines (32 loc) · 888 Bytes
/
user.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
const fs = require('fs');
const path = require('path');
const logger = require("./utils/logger");
const config = require('./config');
let chatIds = {};
fs.mkdirSync(path.dirname(config.user_db_path), { recursive: true });
try {
chatIds = JSON.parse(fs.readFileSync(config.user_db_path));
} catch (e) {
logger.warn(`Cannot read database: ${e}`);
}
function write() {
return fs.writeFile(config.user_db_path, JSON.stringify(chatIds), (e) => {
if (e) logger.warn(`Cannot read database: ${e}`);
});
}
function validate(msg) {
if (msg.chat.username === config.valid_username) {
chatIds[msg.chat.id] = msg.chat.username;
write();
return msg.chat.id;
}
return false;
}
function getChatIds() {
let ids = [];
for (let id in chatIds) {
ids.push(id);
}
return ids;
}
module.exports = { validate, getChatIds };