-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.js
56 lines (50 loc) · 1.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
let nicknameChanges = 0;
module.exports = {
getMemberCount(guild, client) {
if (!guild)
return 0;
return new Promise((resolve, reject) => {
client.db.getGuildConfig(guild).then((cfg) => {
let membs = guild.members.filter((m) => cfg.countBots || !m.user.bot);
let all = membs.size;
let online = membs.filter((m) => m.presence.status != 'offline').size;
let offline = all - online;
resolve({all, online, offline});
}).catch(reject);
});
},
formatCount(countObj, format) {
if (!format)
format = "%all%";
Object.keys(countObj)
.forEach((k) => {
format = format.replace(`%${k}%`, countObj[k]);
});
return format;
},
setNickname(guild, client, cb) {
this.getMemberCount(guild, client).then((count) => {
client.db.getGuildConfig(guild).then((cfg) => {
let formated = this.formatCount(count, cfg.format);
guild.fetchMember(client.user).then(guildMe => {
guildMe.setNickname(formated)
.then(() => nicknameChanges++);
});
if (cb) cb(formated);
});
});
},
updateNicknameChanges(client) {
if (nicknameChanges > 0) {
let statsChannel = client.guilds.get(client.config.supportGuild).channels.find(c => c.name.startsWith("Nickname changes:"));
let changedBefore = parseInt(statsChannel.name.split(": ").slice(1), 10);
statsChannel.setName(`Nickname changes: ${changedBefore + nicknameChanges}`);
nicknameChanges = 0;
}
},
encapsuleString(data) {
if (typeof data == 'string')
return '"' + data + '"'
return data;
}
};