-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUser.js
87 lines (76 loc) · 2.41 KB
/
User.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
class User {
constructor(name, idle = false) {
this.rooms = {};
this.name = name.substring(1);
this.id = toId(name);
this.checkmail();
this.isIdle = idle;
}
send(message) {
if (typeof message === typeof {}) {
for (let i in message) {
Sendpm(this.name, message[i]);
}
return;
}
Sendpm(this.name, message);
}
checkmail() {
let self = this;
FS.readFile(`mail/${self.id}.json`, (err, data) => {
let maildata = [];
if (err) {
return;
}
try {
maildata = JSON.parse(data);
} catch (e) {
maildata = ["[mailerror] Your mail data crashed. Some mail may have gotten lost."];
FS.unlinkSync(`mail/${self.id}.json`);
}
if (!maildata.length) return;
while (maildata.length) {
let mail = maildata.shift();
self.send(mail);
}
FS.writeFile(`mail/${self.id}.json`, JSON.stringify(maildata, null, 4), (err) => {
if (err) throw err;
});
});
}
join(room, name) {
this.rooms[room] = name.charAt(0);
Rooms[room].users[this.id] = this;
}
leave(room) {
delete this.rooms[room];
delete Rooms[room].users[this.id];
if (!Object.keys(this.rooms).length) bot.emit("dereg", "user", this.id);
}
rename(name, idle = false) {
this.id = toId(name);
this.name = name.substring(1);
this.isIdle = idle;
this.checkmail();
}
can(room, rank) {
if (this.id === "staff") return true;
if (!this.rooms[room] && room !== this) return false;
if (Config.devs.indexOf(this.id) !== -1) return true;
if (rank === "all") return false;
if (this.id === toId(Config.username) && rank !== "*") return false;
if (!room) return false;
if (room.id) room = room.id;
return Ranks[this.rooms[room]] <= Ranks[rank];
}
}
User.prototype.toString = function () {
return this.id;
};
exports.add = function (name, idle = false) {
let id = toId(name);
this[id] = new User(name, idle);
};
exports[toId(Config.username)] = new User(" " + Config.username);
exports.self = exports[toId(Config.username)];
exports.staff = new User(" Staff");