-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPermission.ts
162 lines (137 loc) · 3.73 KB
/
Permission.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { TeamSpeak } from "../TeamSpeak"
export class Permission<T extends {} = any> {
private teamspeak: TeamSpeak
private _perm?: string|number
private _value: number = 0
private _skip: boolean = false
private _negate: boolean = false
private withSkipNegate: boolean
private cmdUpdate: string
private cmdRemove: string
private context: T
constructor(config: Permission.IConfig<T>) {
this.cmdUpdate = config.update
this.cmdRemove = config.remove
this.teamspeak = config.teamspeak
this.withSkipNegate = config.allowSkipNegate === false ? false : true
this.context = config.context
}
/** retrieves the current permission */
getPerm() {
return this._perm
}
/** retrieves the permission value */
getValue() {
return this._value
}
/** retrieves wether skip has been set */
getSkip() {
return this._skip || false
}
/** retrieves wether negate has been set */
getNegate() {
return this._negate || false
}
/** sets/gets the permid or permsid */
perm(perm: string|number): Permission<T> {
this._perm = perm
return this
}
/** sets/gets the value for the permission */
value(value: number): Permission<T> {
this._value = value
return this
}
/** sets/gets the skip value */
skip(skip: boolean): Permission<T> {
this._skip = skip
return this
}
/** sets/gets the negate value */
negate(negate: boolean): Permission<T> {
this._negate = negate
return this
}
/** retrieves the permission object */
get(): Permission.PermId|Permission.PermSid {
if (!this._perm) throw new Error("Permission#perm has not been called yet")
if (typeof this._perm === "string") return this.getAsPermSid()
return this.getAsPermId()
}
/** retrieves skip and negate flags */
private getFlags() {
if (!this.withSkipNegate) return {}
return { permskip: this._skip, permnegated: this._negate }
}
/** retrieves a raw object with permid */
private getAsPermId(): Permission.PermId {
if (typeof this._perm !== "number")
throw new Error(`permission needs to be a number but got '${this._perm}'`)
return {
permid: this._perm,
permvalue: this._value,
...this.getFlags()
}
}
/** retrieves a raw object with permsid */
private getAsPermSid(): Permission.PermSid {
if (typeof this._perm !== "string")
throw new Error(`permission needs to be a string but got '${this._perm}'`)
return {
permsid: this._perm,
permvalue: this._value,
...this.getFlags()
}
}
/** retrieves the correct permission name */
private getPermName() {
return typeof this._perm === "string" ? "permsid" : "permid"
}
/** updates or adds the permission to the teamspeak server */
update() {
return this.teamspeak.execute(this.cmdUpdate, {
...this.context,
...this.get()
})
}
/** removes the specified permission */
remove() {
return this.teamspeak.execute(this.cmdRemove, {
...this.context,
[this.getPermName()]: this._perm
})
}
static getDefaults(perm: Partial<Permission.PermType>): Partial<Permission.PermType> {
return {
permskip: false,
permnegated: false,
...perm
}
}
}
export namespace Permission {
export interface IConfig<T> {
teamspeak: TeamSpeak
remove: string,
update: string,
context: T
allowSkipNegate?: boolean
}
export interface BasePermission {
permvalue: number
skip?: boolean
negate?: boolean
}
export interface PermSid extends BasePermission {
permsid: string
}
export interface PermId extends BasePermission {
permid: number
}
export type PermType = {
permname: string|number
permvalue: number
permskip?: boolean
permnegated?: boolean
}
}