-
Notifications
You must be signed in to change notification settings - Fork 270
/
reporter.js
96 lines (71 loc) · 2.7 KB
/
reporter.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
/*
Reports events (and their data) to user list.
Additional action method properties:
{
skipReport: true // Skips report
}
*/
module.exports = {
id: 'reporter',
defaultConfig: {
events: [],
to: []
},
plugin(bot, pluginConfig) {
// If no module options
if (typeof pluginConfig != 'object') {
return console.error('[report] no config data');
}
// Get lists
let toList = Array.isArray(pluginConfig.to) ? pluginConfig.to : [];
let eventList = Array.isArray(pluginConfig.events) ? pluginConfig.events : [];
// Check lists
if (!toList.length) return console.error('[report] no user list');
if (!eventList.length) return console.error('[report] no event list');
// Create events handler
bot.on(eventList, (event = {}, props, info) => {
// Skip event with "skipReport: true" option key
if (
Object.prototype.toString.call(event) == '[object Arguments]' &&
(Array.prototype.slice.call(event).slice(-1)[0]).skipReport === true
) {
return;
}
const type = info.type;
const prefix = type.split('.')[0];
// Stringify object data
const jsonData = s(JSON.stringify(event, (k, v) => {
return v.value instanceof Buffer ? '[Buffer]' : v;
}));
// Send to every user in list
for (let userId of toList) {
if (prefix == 'error') {
// Error event
const {data, error} = event;
bot.sendMessage(userId,
`👤 <b>User:</b> ${data.from.id} (${data.chat.id})\n` +
`⚠ <b>Error:</b> ${error.message || error}\n` +
`${error.stack ? `🚧 <b>Stack:</b>\n${s(error.stack)}\n` : ''}` +
`⏰ <b>Event:</b> ${type}\n` +
`💾 <b>Data:</b> ${jsonData}`,
{parseMode: 'html', skipReport: true}
);
} else {
// Another type of event
bot.sendMessage(userId,
`⏰ <b>Event:</b> ${type}\n` +
(jsonData && jsonData != '{}' ? `💾 <b>Data:</b> ${jsonData}` : ''),
{parseMode: 'html', skipReport: true}
);
}
}
});
}
};
// Safe string function
function s(str) {
return String(str).replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}