-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.ts
55 lines (42 loc) · 1.56 KB
/
data.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
import * as files from 'fs';
export interface Entry {
channel: string;
emoji: string;
role: string;
}
export class Data {
private entries: Array<Entry>;
public constructor() {
if (!files.existsSync('data.json')) {
files.writeFileSync('data.json', '[]');
}
this.entries = JSON.parse(files.readFileSync('data.json').toString());
}
public hasChannel(channel: string): boolean {
return this.entries.filter(entry => entry.channel === channel).length !== 0;
}
public getChannel(channel: string): Array<Entry> {
return this.entries.filter(entry => entry.channel === channel);
}
public hasEmoji(channel: string, emoji: string): boolean {
return this.entries.filter(entry => entry.channel === channel && entry.emoji === emoji).length !== 0;
}
public getRole(channel: string, emoji: string): string {
return this.entries.filter(entry => entry.channel === channel && entry.emoji === emoji)[0].role;
}
public removeEmoji(channel: string, emoji: string): void {
this.entries = this.entries.filter(entry => !(entry.channel === channel && entry.emoji === emoji));
files.writeFileSync('data.json', JSON.stringify(this.entries));
}
public get(): Array<Entry> {
return this.entries;
}
public addEmoji(channel: string, emoji: string, role: string): void {
this.entries.push({
channel,
emoji,
role
});
files.writeFileSync('data.json', JSON.stringify(this.entries));
}
}