/
PluginBuilder.ts
147 lines (116 loc) · 4.35 KB
/
PluginBuilder.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
import { IBZDBSetting } from './IBZDBSetting';
import { ICallback } from './ICallback';
import { IEvent } from './IEvent';
import { IFlag } from './IFlag';
import { IMapObject } from './IMapObject';
import IPlugin from './IPlugin';
import { IPollType } from './IPollType';
import { ISlashCommand } from './ISlashCommand';
import { NullLicense } from './ILicense';
export default class PluginBuilder {
public definition: IPlugin;
constructor() {
this.definition = {
name: 'Sample Plugin',
author: {
copyright: '',
callsign: '',
},
license: NullLicense,
codeStyle: {
useIfStatement: false,
bracesOnNewLine: true,
spacingType: 'fourSpace',
showDocBlocks: true,
showComments: true,
},
makesUrlCalls: false,
events: {},
slashCommands: {},
callbacks: {},
mapObjects: {},
flags: {},
bzdbSettings: {},
pollTypes: {},
};
Object.seal(this.definition);
}
addEvent(event: IEvent) {
this.definition.events[event.name] = event;
}
removeEvent(event: IEvent | String | string) {
this.safeRemove('events', event, 'name');
}
addSlashCommand(command: ISlashCommand) {
PluginBuilder.normalizeSlashCommand(command);
this.definition.slashCommands[command.name] = command;
}
removeSlashCommand(command: ISlashCommand | String | string) {
this.safeRemove('slashCommands', command, 'name');
}
addCallback(callback: ICallback) {
this.definition.callbacks[callback.name] = callback;
}
removeCallback(callback: ICallback | String | string) {
this.safeRemove('callbacks', callback, 'name');
}
addMapObject(mapObject: IMapObject) {
PluginBuilder.normalizeMapObject(mapObject);
this.definition.mapObjects[mapObject.name] = mapObject;
}
removeMapObject(mapObject: IMapObject | String | string) {
this.safeRemove('mapObjects', mapObject, 'name');
}
addFlag(flag: IFlag) {
PluginBuilder.normalizeFlag(flag);
this.definition.flags[flag.abbreviation] = flag;
}
removeFlag(flag: IFlag | String | string) {
this.safeRemove('flags', flag, 'abbreviation');
}
addBZDBSetting(bzdbSetting: IBZDBSetting) {
PluginBuilder.normalizeBZDBSetting(bzdbSetting);
this.definition.bzdbSettings[bzdbSetting.name] = bzdbSetting;
}
removeBZDBSetting(bzdbSetting: IBZDBSetting | String | string) {
this.safeRemove('bzdbSettings', bzdbSetting, 'name');
}
addPollType(pollType: IPollType) {
PluginBuilder.normalizePollType(pollType);
this.definition.pollTypes[pollType.name] = pollType;
}
removePollType(pollType: IPollType | String | string) {
this.safeRemove('pollTypes', pollType, 'name');
}
private safeRemove(namespace: string, key: { [key: string]: any } | String | string, secondaryKey: string) {
let targetToRemove!: string;
if (typeof key === 'string') {
targetToRemove = key;
} else if (key instanceof String) {
targetToRemove = key.valueOf();
} else {
targetToRemove = key[secondaryKey];
}
if (targetToRemove === null) {
throw Error('Cannot safely remove a value if the target key is null');
}
delete this.definition[namespace][targetToRemove];
}
public static normalizeSlashCommand(value: ISlashCommand): void {
value.name = value.name.toLowerCase().replace(/[^a-z0-9]/, '');
}
public static normalizeMapObject(value: IMapObject): void {
value.name = value.name.toLowerCase().replace(/[^a-z0-9]/, '');
}
public static normalizeFlag(value: IFlag): void {
value.name = value.name.substring(0, 32);
value.abbreviation = value.abbreviation.toUpperCase().substring(0, 2);
value.helpString = value.helpString.substring(0, 128);
}
public static normalizeBZDBSetting(value: IBZDBSetting): void {
value.name = '_' + value.name.replace(/[^a-zA-Z0-9]/, '');
}
public static normalizePollType(value: IPollType): void {
value.name = value.name.toLowerCase().replace(/[^a-z0-9]/, '');
}
}