forked from PaIIadium/nodejs-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.js
93 lines (88 loc) · 3.58 KB
/
queue.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
'use strict';
const fs = require('fs');
const answers = JSON.parse(fs.readFileSync('./answers.json'));
const { sendMessage, checkStatus, findSettings, setOptMsg } = require('./functions');
const { onNode, onStart, onStatus, onEnable, onGlobalEnable, onGlobalDisable, onMaxCharsUser, onMaxLinesUser, onDisable, onMaxCharsGroup, onMaxLinesGroup, onTimeout, onMaxTasksPerUser, onUpdate } = require('./commands');
const { defaultSettings, groupSettings, userSettings } = require('./collections');
const bot = require('./bot');
const globalSettings = defaultSettings.global;
const parse = msg => {
let command;
if (msg.entities[0].type === 'bot_command' && msg.entities[0].offset === 0) {
command = msg.text.match(/\/[^ \n@]+/)[0];
return command;
}
};
const queue = {
maxTasksPerUser: globalSettings[2],
arr: [],
search(id) {
const ind = [];
for (let i = 0; i < this.arr.length; i++) {
if (this.arr[i][0].from.id === id) {
ind.push(i);
if (ind.length === this.maxTasksPerUser) break;
}
}
return ind;
},
inQueue(msg) {
const optionsMsg = setOptMsg(msg);
const command = parse(msg);
const sets = findSettings(msg.chat.type, msg.chat.id, groupSettings, userSettings);
const status = checkStatus(msg, globalSettings[1], sets[2]);
if (command === '/status') {
onStatus(msg, status, sets[0], sets[1], globalSettings[0], globalSettings[2]);
}
if (command === '/update') {
onUpdate(msg, userSettings, groupSettings);
}
if (status === 'enabled') {
if (command === '/node') {
const sets = findSettings(msg.chat.type, msg.chat.id, groupSettings, userSettings);
const timeout = globalSettings[0];
const maxChars = sets[0];
const maxLines = sets[1];
if (this.arr.length) { //rewrite
const numbs = this.search(msg.from.id);
if (numbs.length === this.maxTasksPerUser) {
const str = answers.onInQueueBefore + numbs.join(', ') + answers.onInQueueAfter;
sendMessage(msg.chat.id, str, optionsMsg);
} else {
this.arr.push([msg, timeout, maxChars, maxLines]);
}
} else {
this.arr.push([msg, timeout, maxChars, maxLines]);
onNode(msg, this, timeout, maxChars, maxLines);
}
} else if (command === '/globaldisable') {
onGlobalDisable(msg, defaultSettings);
} else if (command === '/start') {
onStart(msg);
} else if (command === '/maxchars') {
if (msg.chat.type === 'private') onMaxCharsUser(msg, userSettings, defaultSettings);
else onMaxCharsGroup(msg, groupSettings, defaultSettings, bot);
} else if (command === '/maxlines') {
if (msg.chat.type === 'private') onMaxLinesUser(msg, userSettings, defaultSettings);
else onMaxLinesGroup(msg, groupSettings, defaultSettings);
} else if (command === '/disable' && msg.chat.type !== 'private') {
onDisable(msg, groupSettings, bot, defaultSettings);
} else if (command === '/timeout') {
onTimeout(msg, defaultSettings);
} else if (command === '/maxtasksperuser') {
onMaxTasksPerUser(msg, defaultSettings, this);
}
} else if (status === 'locally disabled' && command === '/enable') {
onEnable(msg, groupSettings, bot, defaultSettings);
} else if (status === 'globally disabled' && command === '/globalenable') {
onGlobalEnable(msg, defaultSettings);
}
},
fromQueue() {
if (this.arr.length) {
const pars = this.arr[0];
onNode(pars[0], this, pars[1], pars[2], pars[3]);
}
}
};
module.exports = queue;