forked from Konard/vk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
106 lines (94 loc) · 2.83 KB
/
utils.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
const { DateTime } = require('luxon');
const fs = require('fs');
function getRandomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
const hasSticker = (context, stickersIds) => {
for (const attachment of context?.attachments || []) {
if (attachment?.id) {
const stickerId = attachment?.id;
// console.log('stickerId', stickerId);
return stickersIds.includes(stickerId);
} else {
const stickerId = attachment?.sticker?.sticker_id;
// console.log('stickerId', stickerId);
return stickersIds.includes(stickerId);
}
}
return false;
}
const sleep = ms => new Promise(resolve => {
console.log(`Sleeping for ${ms} ms...`);
setTimeout(() => {
console.log(`Wake up after ${ms} ms.`)
resolve();
}, ms);
});
function eraseMetadata(obj) {
return JSON.parse(JSON.stringify(obj));
}
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === null || obj[propName] === undefined || obj[propName]?.length === 0) {
delete obj[propName];
}
// if(typeof obj[propName] === 'object'){
// clean(obj[propName]); //recursive for nested objects
// }
}
return obj;
}
const defaultEncoding = 'utf-8';
function readTextSync(path) {
return fs.readFileSync(path, { encoding: defaultEncoding });
}
function readJsonSync(path) {
return JSON.parse(readTextSync(path));
}
function saveTextSync(path, text) {
return fs.writeFileSync(path, text, { encoding: defaultEncoding })
}
function saveJsonSync(path, obj) {
return saveTextSync(path, JSON.stringify(obj, null, 2));
}
async function executeTrigger(trigger, context) {
if (!trigger) {
return;
}
let peerState;
const peerId = context?.request?.peerId;
if (context?.states && peerId) {
peerState = context?.states[peerId];
}
const currentContext = { ...context, state: peerState };
console.log(`Checking for '${trigger.name}' trigger...`);
if (!trigger.condition || (await trigger.condition(currentContext))) {
try {
console.log(`'${trigger.name}' trigger selected to be executed.`);
await trigger.action(currentContext);
console.log(`'${trigger.name}' trigger is executed.`);
if (peerState && trigger.name) {
const triggers = peerState.triggers ??= {};
const triggerState = triggers[trigger.name] ??= {};
triggerState.lastTriggered = DateTime.now();
console.log(`'${trigger.name}' trigger has updated the state for user ${peerId}:`, JSON.stringify(peerState, null, 2));
}
} catch (e) {
console.error(`Execution of '${trigger.name}' trigger is failed:`, e);
}
} else {
console.log(`No need to execute '${trigger.name}' trigger.`);
}
}
module.exports = {
getRandomElement,
hasSticker,
sleep,
executeTrigger,
eraseMetadata,
clean,
readTextSync,
readJsonSync,
saveTextSync,
saveJsonSync,
};