forked from KamilaBorowska/Pokemon-Showdown-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.ts
97 lines (80 loc) · 2.56 KB
/
users.ts
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
/**
* 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.
*/
/// <reference path="typings/map.d.ts" />
import {toId} from './utils'
import {Config, send} from './main'
import {settings} from './parser'
import {getRoom} from './rooms'
export const users = Object.create(null)
export class User {
name: string
id: string
rooms = new Map<string, string>()
constructor (username: string) {
this.name = username.substr(1)
this.id = toId(this.name)
}
isExcepted () {
return Config.excepts.indexOf(this.id) !== -1
}
isWhitelisted () {
return Config.whitelist.indexOf(this.id) !== -1
}
isRegexWhitelisted () {
return Config.regexautobanwhitelist.indexOf(this.id) !== -1
}
hasRank (roomid: string, tarGroup: string) {
if (this.isExcepted()) return true
const group = getRoom(roomid).users.get(this.id) || roomid // PM messages use the roomid parameter as the user's group
return Config.groups[group] >= Config.groups[tarGroup]
}
canUse (cmd: string, roomid: string) {
if (this.isExcepted()) return true
const commandSettings = settings[cmd]
if (!commandSettings || !commandSettings[roomid]) {
return this.hasRank(roomid, (cmd === 'autoban' || cmd === 'blacklist') ? '#' : Config.defaultrank)
}
const setting = commandSettings[roomid]
if (setting === true) return true
return this.hasRank(roomid, setting)
}
rename (username: string) {
const 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) {
const room = getRoom(roomid)
room.users.delete(this.id)
})
this.rooms.clear()
delete users[this.id]
}
say(message: string) {
send('|/pm ' + this.id + ', ' + message)
}
}
export function getUser(username: string) {
const userid = toId(username)
return users[userid]
}
export function addUser(username: string) {
const user = getUser(username)
if (user) {
return user
}
const newUser = new User(username)
users[newUser.id] = newUser
return newUser
}
const botId = ' ' + toId(Config.nick)
export const self = getUser(botId) || addUser(botId)