This repository has been archived by the owner on May 17, 2020. It is now read-only.
forked from Slayer95/Pokemon-Showdown-Bot
-
Notifications
You must be signed in to change notification settings - Fork 64
/
users.js
86 lines (72 loc) · 2.04 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
/**
* This is where users are stored.
*
* New users are processed when joining new rooms and on receiving join
* messages from the server. User chat data is processed here for use
* in command permissions and automatic moderation.
*/
var Users = Object.create(null);
var users = Users.users = Object.create(null);
class User {
constructor (username, roomid) {
this.name = username.substr(1);
this.id = toId(this.name);
this.rooms = new Map();
if (roomid) this.rooms.set(roomid, username.charAt(0));
}
isExcepted () {
return Config.excepts.includes(this.id);
}
isWhitelisted () {
return Config.whitelist.includes(this.id);
}
isRegexWhitelisted () {
return Config.regexautobanwhitelist.includes(this.id);
}
hasRank (roomid, tarGroup) {
if (this.isExcepted()) return true;
var group = this.rooms.get(roomid) || roomid; // PM messages use the roomid parameter as the user's group
return Config.groups[group] >= Config.groups[tarGroup];
}
canUse (cmd, roomid) {
if (this.isExcepted()) return true;
var settings = Parse.settings[cmd];
if (!settings || !settings[roomid]) {
return this.hasRank(roomid, (cmd === 'autoban' || cmd === 'blacklist') ? '#' : Config.defaultrank);
}
var setting = settings[roomid];
if (setting === true) return true;
return this.hasRank(roomid, setting);
}
rename (username) {
var oldid = this.id;
delete users[oldid];
this.id = toId(username);
this.name = username.substr(1);
users[this.id] = this;
return this;
}
destroy () {
this.rooms.forEach(function (group, roomid) {
var room = Rooms.get(roomid);
room.users.delete(this.id);
});
this.rooms.clear();
delete users[this.id];
}
}
var getUser = Users.get = function (username) {
var userid = toId(username);
return users[userid];
};
var addUser = Users.add = function (username, room) {
var user = getUser(username);
if (!user) {
user = new User(username, room);
users[user.id] = user;
}
return user;
};
var botId = ' ' + toId(Config.nick);
Users.self = getUser(botId) || addUser(botId);
module.exports = Users;