This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore-provider.ts
249 lines (209 loc) · 7.88 KB
/
firestore-provider.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { ClientEvents, Guild } from 'discord.js';
import { Command, CommandGroup, CommandoClient, SettingProvider } from 'discord.js-commando';
import admin from "firebase-admin";
export interface GuildSettings {
guildId: string;
[field: string]: any;
}
const COLLECTION_NAME = 'server_config';
const GLOBAL = 'global';
const PREFIX_KEY = 'prefix';
function getCommandStatusKey(command: Command) {
return `cmd-${command.name}`;
}
function getGroupStatusKey(group: CommandGroup) {
return `grp-${group.id}`;
}
/**
* Uses an Firestore collection to store settings with guilds
* @extends {SettingProvider}
*/
export class FirestoreProvider extends SettingProvider {
/**
* Client that the provider is for (set once the client is ready, after using {@link CommandoClient#setProvider})
* @readonly
*/
private client!: CommandoClient;
/**
* Database that will be used for storing/retrieving settings
*/
private db: admin.firestore.Firestore;
/**
* Settings cached in memory, mapped by guild ID (or 'global')
*/
private settings: Map<string, GuildSettings>;
/**
* Listeners on the Client, mapped by the event name
*/
private listeners: Map<keyof ClientEvents, (...args: any[]) => void>;
/**
* @param {admin.app.App} app - Application from Firebase Admin
*/
constructor(app: admin.app.App) {
super();
this.db = app.firestore();
this.settings = new Map();
this.listeners = new Map();
}
async init(client: CommandoClient) {
this.client = client;
// Load or create the settings collection
const collection = this.db.collection(COLLECTION_NAME);
// Load all settings
const docs = await collection.get();
docs.forEach(doc => {
const data = doc.data() as GuildSettings;
const guildId = data.guildId;
this.settings.set(guildId, data);
// Guild is not global, and doesn't exist currently so lets skip it.
if (guildId !== GLOBAL && !client.guilds.cache.has(data.guildId)) return;
this.setupGuild(guildId, data);
});
// Listen for changes
this.listeners
.set('commandPrefixChange', (guild: Guild, prefix: string) => this.set(guild, PREFIX_KEY, prefix))
.set('commandStatusChange', (guild: Guild, command: Command, enabled: boolean) => this.set(guild, getCommandStatusKey(command), enabled))
.set('groupStatusChange', (guild: Guild, group: CommandGroup, enabled: boolean) => this.set(guild, getGroupStatusKey(group), enabled))
.set('guildCreate', (guild: Guild) => {
const settings = this.settings.get(guild.id);
if (!settings) return;
this.setupGuild(guild.id, settings);
})
.set('commandRegister', (command: Command) => {
for (const [guild, settings] of this.settings) {
if (guild !== GLOBAL && !client.guilds.cache.has(guild)) continue;
this.setupGuildCommand(guild, command, settings);
}
})
.set('groupRegister', (group: CommandGroup) => {
for (const [guild, settings] of this.settings) {
if (guild !== GLOBAL && !client.guilds.cache.has(guild)) continue;
this.setupGuildGroup(guild, group, settings);
}
});
for (const [event, listener] of this.listeners) client.on(event, listener);
}
async destroy() {
// Remove all listeners from the client
for (const [event, listener] of this.listeners) this.client.removeListener(event, listener);
this.listeners.clear();
}
get(guild: string | Guild, key: string, defVal?: any) {
const settings = this.settings.get(FirestoreProvider.getGuildID(guild));
return settings ? typeof settings[key] !== 'undefined' ? settings[key] : defVal : defVal;
}
async set(guild: string | Guild, key: string, val: any) {
const guildId = FirestoreProvider.getGuildID(guild);
let settings = this.settings.get(guildId);
if (!settings) {
settings = {
guildId
};
this.settings.set(guildId, settings);
}
settings[key] = val;
await this.updateGuild(guildId, settings);
if (guildId === GLOBAL) this.updateOtherShards(key, val);
return val;
}
async remove(guild: string | Guild, key: string) {
const guildId = FirestoreProvider.getGuildID(guild);
const settings = this.settings.get(guildId);
if (!settings || typeof settings[key] === 'undefined') return;
const val = settings[key];
delete settings[key];
await this.updateGuild(guildId, settings);
if (guildId === GLOBAL) this.updateOtherShards(key, undefined);
return val;
}
async clear(guild: string | Guild) {
const guildId = FirestoreProvider.getGuildID(guild);
if (!this.settings.has(guildId)) return;
this.settings.delete(guildId);
const doc = this.db.collection(COLLECTION_NAME).doc(guildId);
await doc.delete();
}
private async updateGuild(guildId: string, settings: GuildSettings) {
const doc = this.db.collection(COLLECTION_NAME).doc(guildId);
return doc.set(settings);
}
/**
* Loads all settings for a guild
* @param {string} guildId - Guild ID to load the settings of (or 'global')
* @param {Object} settings - Settings to load
* @private
*/
private setupGuild(guildId: string, settings: GuildSettings) {
if (typeof guildId !== 'string') throw new TypeError('The guildId must be a guild ID or "global".');
const guild = this.client.guilds.cache.get(guildId);
// Load the command prefix
if (typeof settings[PREFIX_KEY] !== 'undefined') {
if (guild) guild['_commandPrefix'] = settings[PREFIX_KEY];
else if (guildId === GLOBAL) this.client['_commandPrefix'] = settings[PREFIX_KEY];
}
// Load all command/group statuses
for (const command of this.client.registry.commands.values()) this.setupGuildCommand(guildId, command, settings);
for (const group of this.client.registry.groups.values()) this.setupGuildGroup(guildId, group, settings);
}
/**
* Sets up a command's status in a guild from the guild's settings
* @param {string} guildId - Guild ID to load the settings of (or 'global')
* @param {Command} command - Command to set the status of
* @param {Object} settings - Settings of the guild
* @private
*/
private setupGuildCommand(guildId: string, command: Command, settings: GuildSettings) {
if (typeof guildId !== 'string') throw new TypeError('The guildId must be a guild ID or "global".');
const guild = this.client.guilds.cache.get(guildId);
const status = settings[getCommandStatusKey(command)];
if (typeof status !== 'undefined') {
if (guild) {
if (!guild['_commandsEnabled']) guild['_commandsEnabled'] = {};
guild['_commandsEnabled'][command.name] = status;
} else if (guildId === GLOBAL) {
command['_globalEnabled'] = status;
}
}
}
/**
* Sets up a group's status in a guild from the guild's settings
* @param {string} guildId - Guild ID to load the settings of (or 'global')
* @param {CommandGroup} group - Group to set the status of
* @param {Object} settings - Settings of the guild
* @private
*/
private setupGuildGroup(guildId: string, group: CommandGroup, settings: GuildSettings) {
if (typeof guildId !== 'string') throw new TypeError('The guildId must be a guild ID or "global".');
const guild = this.client.guilds.cache.get(guildId);
const status = settings[getGroupStatusKey(group)];
if (typeof status !== 'undefined') {
if (guild) {
if (!guild['_groupsEnabled']) guild['_groupsEnabled'] = {};
guild['_groupsEnabled'][group.id] = status;
} else if (guildId === GLOBAL) {
group['_globalEnabled'] = status;
}
}
}
/**
* Updates a global setting on all other shards if using the {@link ShardingManager}.
* @param {string} key - Key of the setting to update
* @param {*} val - Value of the setting
* @private
*/
private updateOtherShards(key: string, val: any) {
if (!this.client.shard) return;
key = JSON.stringify(key);
val = typeof val !== 'undefined' ? JSON.stringify(val) : 'undefined';
this.client.shard.broadcastEval(`
if(this.provider && this.provider.settings) {
let global = this.provider.settings.get('global');
if(!global) {
global = {};
this.provider.settings.set('global', global);
}
global[${key}] = ${val};
}
`);
}
}