-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.js
139 lines (123 loc) · 4.17 KB
/
users.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
'use strict';
const cache_db = require("./cache-db.js");
class User {
constructor(name) {
this.name = (/[a-zA-Z0-9]/i.test(name.charAt(0)) ? name : name.slice(1));
this.userid = toId(name);
this.ranks = new Map();
this.botRank = Db("ranks").get(this.userid, " ");
this.globalRank = " ";
this.isStaff = Config.ranks[this.botRank] >= 2 || this.isDev();
}
getRank(roomid){
return this.ranks.get(roomid) || " ";
}
updateGlobalRank(rank) {
this.globalRank = rank;
this.isStaff = Config.ranks[this.botRank] >= 2 || Config.ranks[this.globalRank] >= 2 || this.isDev();
}
update(room, name) {
if (name.charAt(0) in Config.ranks) {
this.ranks.set(room.id || toId(room, true), name.charAt(0));
this.name = (/[a-zA-Z0-9]/i.test(name.charAt(0)) ? name : name.slice(1));
}
Plugins.mail.receive(this);
Users.seen.set(this.userid, [Date.now(), room.name]);
}
onLeave(room) {
this.ranks.delete(room.id);
Users.seen.set(this.userid, [Date.now(), room.name]);
}
botPromote(rank) {
this.botRank = rank;
Db("ranks").set(this.userid, rank);
}
sendTo(text) {
if (!text) return false;
return send("|/pm " + this.userid + ", " + text);
}
can(action, targetRoom, targetUser, details) {
if (this.isDev()) return true;
if (action === "promote") {
if (!targetUser) return false;
let userRank = Config.permissions[this.botRank];
let targetRank = targetUser.botRank || Db("ranks").get(toId(targetUser), " ");
if (!userRank.promote) return false;
if (userRank.promote.includes(targetRank) && userRank.promote.includes(details) && targetRank !== details) {
return true;
}
return false;
}
if (["ban", "lock", "mute"].includes(action)) {
if (!targetUser) return false;
let userRank = Config.permissions[this.botRank];
let targetRank = targetUser.botRank || Db("ranks").get(toId(targetUser), " ");
if (!userRank[action]) return false;
if (userRank[action].includes(targetRank)) {
return true;
}
return false;
}
if (["set"].includes(action)) {
if (!this.hasRank(targetRoom, "#")) return false;
return true;
}
if (["autoban", "banword"].includes(action)){
if (this.hasRank(targetRoom, targetRoom ? Db("settings").get([targetRoom.id, action], "#") : "#")) return true;
return false;
}
let commandRank = targetRoom ? Db("settings").get([targetRoom.id, action], (Config.customRank && Config.customRank[action] ? Config.customRank[action] : Config.defaultRank)) : Config.customRank && Config.customRank[action] ? Config.customRank[action] : Config.defaultRank;
if (!this.hasRank(targetRoom, commandRank)) return false;
return true;
}
hasRank(room, rank) {
if (rank === "off" && !this.isDev()) return false;
if (rank === "on") return true;
let roomRank = room ? this.getRank(room.id) : this.globalRank;
if (((Config.ranks[roomRank] || 0) >= Config.ranks[rank]) || this.hasBotRank(rank)) return true;
return false;
}
hasBotRank(rank) {
if (this.isDev()) return true;
if ((Config.ranks[this.botRank] || 0) >= Config.ranks[rank]) return true;
return false;
}
isDev() {
return Config.developers && Config.developers.includes(this.userid);
}
}
class _Users {
constructor() {
this.seen = new cache_db();
this.users = new Map();
this.seen.load("config/seen");
}
add(username){
let userid = toId(username);
if (this.users.has(userid)) return this.get(username);
this.users.set(userid, new User(username));
return this.users.get(userid);
}
get(username){
let userid = toId(username);
if (!this.users.has(userid)) return this.add(username);
return this.users.get(userid);
}
rename(oldId, newName){
if (!this.users.has(oldId)) return false; //already renamed
if (toId(oldId) !== toId(newName)) {
this.users.set(toId(newName), this.get(oldId));
Monitor.transferRecords(oldId, toId(newName));
this.users.delete(oldId);
}
let tarUser = this.get(newName);
//change attributes of the new user
tarUser.name = newName.slice(1);
tarUser.userid = toId(newName);
tarUser.botRank = Db("ranks").get(tarUser.userid, " ");
tarUser.globalRank = " ";
tarUser.isStaff = Config.ranks[tarUser.botRank] >= 2;
}
}
exports.Users = new _Users();
/*globals Monitor Config toId Db Plugins send Users*/