-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonlineMembersCount.js
310 lines (271 loc) · 10.9 KB
/
onlineMembersCount.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
registerPlugin({
name: "Online Members Count",
version: "0.2.2",
description: "Shows status and count of a specified members in a specified channel",
author: "DrWarpMan <[email protected]>",
backends: ["ts3"],
engine: ">= 1.0",
autorun: false,
enableWeb: false,
hidden: false,
requiredModules: [],
vars: [{
name: "onlineMembersCountList",
type: "array",
title: "Configuration:",
vars: [{
name: "channelID",
type: "string",
title: "Channel ID:",
placeholder: "69"
},
{
name: "groups",
type: "strings",
title: "Group IDs:"
},
{
name: "channelNameOn",
type: "string",
title: "Channel name when online [Placeholder: %count%]:",
placeholder: "Administrators online [%online%]"
},
{
name: "channelNameOff",
type: "string",
title: "Channel name when offline [Placeholder: %count%]:",
placeholder: "No administrators online"
}, {
name: 'awayIsOffline',
title: 'Check to count AFK members as offline',
type: 'checkbox'
}, {
name: 'description',
title: 'Channel description, leave empty to not use this feature [Placeholder: %members% OR %groupID%]:',
type: 'multiline'
}
]
},
{
name: "canHideGroups",
type: "strings",
title: "List of group IDs allowed to hide:"
},
{
name: "commandToggleHide",
type: "string",
title: "Command to make yourself hidden (bot will think, that you are offline):",
placeholder: "!togglehide",
default: "!togglehide"
}, {
name: "awayChannels",
title: "Channels (IDs), that if client is in, he is considered as AWAY",
type: "strings"
}, {
name: 'showTranslation',
title: 'Show translation?',
type: 'checkbox'
}, {
name: "messageHidden",
type: "string",
title: "MESSAGE: You are now hidden!",
default: "You are now hidden!",
placeholder: "Translate this message here!",
conditions: [{
field: 'showTranslation',
value: 1,
}]
}, {
name: "messageNotHidden",
type: "string",
title: "MESSAGE: You are not hidden anymore!",
default: "You are not hidden anymore!",
placeholder: "Translate this message here!",
conditions: [{
field: 'showTranslation',
value: 1,
}]
}, {
name: "messageNoRights",
type: "string",
title: "(To disable this message, write DISABLED) MESSAGE: You do not have permission to hide yourself!",
default: "You do not have permission to hide yourself!",
placeholder: "Translate this message here!",
conditions: [{
field: 'showTranslation',
value: 1,
}]
}
],
voiceCommands: []
}, function(_, config, meta) {
const backend = require("backend");
const engine = require("engine");
const store = require("store");
const event = require("event");
if (config.onlineMembersCountList === undefined)
return engine.log("ERROR: No configuration found!");
let scriptData = {};
config.onlineMembersCountList.forEach(i => {
if (!scriptData[i.channelID]) {
if (i.groups === undefined || i.channelNameOff === undefined || i.channelNameOn === undefined)
engine.log("WARN: Found undefined entry in configuration!");
scriptData[i.channelID] = {
groups: i.groups,
channelNameOn: i.channelNameOn,
channelNameOff: i.channelNameOff,
awayIsOffline: i.awayIsOffline,
description: i.description
};
} else engine.log("WARN: Found duplicate in configuration!");
});
setInterval(() => {
if (backend.isConnected()) {
Object.keys(scriptData).forEach(i => {
let data = scriptData[i];
let channel = backend.getChannelByID(i);
if (channel) {
let clients = getClientsWithGroups(data.groups, data.awayIsOffline);
clients = clients.sort(compare);
let count = clients.length;
if (count >= 1) {
channel.setName(data.channelNameOn.replace("%count%", count));
} else {
channel.setName(data.channelNameOff.replace("%count%", count));
}
if (data.description) {
if (data.description.length > 0) {
let newDescription = data.description;
// ALL MEMBERS LIST
let members = "";
clients.forEach(client => {
members += "[URL=client://0/" + client.uid() + "]" + client.nick() + "[/URL]\n";
});
// MEMBERS BY GROUP-ID LIST
let membersSeparate = data.description.match(/%\d+%/g); // Get all placeholders for groups
if (membersSeparate) {
membersSeparate.forEach(j => {
let groupID = j.slice(1, -1);
if (data.groups.includes(groupID)) {
let groupMembers = "";
clients.forEach(client => {
if (hasServerGroupWithID(client, groupID))
groupMembers += "[URL=client://0/" + client.uid() + "]" + client.nick() + "[/URL]\n";
});
newDescription = newDescription.replace(j, groupMembers);
}
});
}
channel.setDescription(newDescription.replace("%members%", members));
}
}
}
});
}
}, 30 * 1000);
event.on("chat", chatInfo => {
if (chatInfo.client.isSelf())
return;
let message = chatInfo.text.toLowerCase();
let client = chatInfo.client;
let clientUID = client.uid();
if (message.startsWith(config.commandToggleHide.toLowerCase())) {
let data = store.get(clientUID);
let allow = isMemberAllowedToHide(client);
if (allow) {
if (!data) {
store.set(clientUID, { hidden: true });
client.chat(config.messageHidden);
} else if (data.hidden === false) {
store.set(clientUID, { hidden: true });
client.chat(config.messageHidden);
} else if (data.hidden === true) {
store.set(clientUID, { hidden: false });
client.chat(config.messageNotHidden);
}
} else {
if (config.messageNoRights.toLowerCase() !== "disabled")
client.chat(config.messageNoRights);
}
}
});
/**
* Get all online clients with specified groups
*
* @param {Array} groups
* @param {Boolean} awayIsOffline Whether we count away as offline or not
*
* @return {Array} Array of online clients
*/
function getClientsWithGroups(groups, awayIsOffline) {
let clients = [];
backend.getClients().forEach(client => {
let clientGroups = client.getServerGroups().map(g => g.id());
let clientOffline = false;
if (awayIsOffline && (client.isAway() || checkArrays(client.getChannels().map(ch => ch.id()), config.awayChannels)))
clientOffline = true;
if (isHidden(client))
clientOffline = true;
if (checkArrays(clientGroups, groups) && clientOffline === false) {
clients.push(client);
}
});
return clients;
}
/**
* Whether client is or is not hidden
* @param {Client} client
* @return {Boolean}
*/
function isHidden(client) {
let data = store.get(client.uid());
if (data !== undefined) {
if (isMemberAllowedToHide(client))
return data.hidden;
}
}
/**
* Checks whether client has specified group or not
* @return {Boolean}
*/
function hasServerGroupWithID(client, id) {
return client.getServerGroups().some(function(group) {
return group.id() == id;
});
}
/**
* Checking whether the client has / doesn't have rights to hide
* @param {Client} client The client, that is being checked
* @return {boolean} Whether he has / doesn't have
*/
function isMemberAllowedToHide(client) {
if (config.canHideGroups === undefined)
return false;
let clientGroups = client.getServerGroups().map(g => g.id());
return checkArrays(clientGroups, config.canHideGroups);
}
/**
* Checking if two arrays have at least one same item
* @param {array} arr1 First array
* @param {array} arr2 Second array
* @return {boolean} Whether they have / don't have at least one same item
*/
function checkArrays(arr1, arr2) {
if (arr1 === arr2)
return true;
if (arr1 === undefined || arr2 === undefined)
return false;
return arr2.some(item => arr1.includes(item));
}
function compare(a, b) {
if (a.nick() < b.nick()) {
return -1;
}
if (a.nick() > b.nick()) {
return 1;
}
return 0;
}
// SCRIPT LOADED SUCCCESFULLY
engine.log("\n[Script] \"" + meta.name + "\" [Version] \"" + meta.version + "\" [Author] \"" + meta.author + "\"");
});