-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
203 lines (179 loc) · 6.47 KB
/
index.js
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
const { SettingProvider } = require('discord.js-commando');
const { query } = require('faunadb');
const {
Update,
Select,
Get,
Match,
Index,
Remove,
Lambda,
Documents,
Collection: FaunaCollection,
Map: FaunaMap,
Paginate,
CreateIndex,
Exists,
CreateCollection,
Create,
} = query;
module.exports = class FaunaProvider extends SettingProvider {
constructor(db, name) {
super();
this.db = db;
this.client = null;
this.settings = new Map();
this.listeners = new Map();
}
async init(client) {
this.client = client;
if (!(await this.db.query(Exists(FaunaCollection('guilds'))))) {
await this.db.query(CreateCollection({ name: 'guilds' }));
}
if (!(await this.db.query(Exists(Index('guild_by_id'))))) {
await this.db.query(CreateIndex({ name: 'guild_by_id', source: FaunaCollection('guilds'), terms: [{ field: ['data', 'id'] }] }));
}
// this could take a while if it is a big collection
const guilds = (await this.db.query(FaunaMap(
Paginate(Documents(FaunaCollection('guilds'))),
Lambda((x) => Get(x)),
))).data;
guilds.forEach(({ data }) => {
const guild = data.id !== '0' ? data.id : 'global';
const settings = data;
this.settings.set(guild, settings);
if (guild !== 'global' && !client.guilds.cache.has(guild)) return;
this.setupGuild(guild, settings);
});
this.listeners
.set('commandPrefixChange', (guild, prefix) => this.set(guild.id, 'prefix', prefix))
.set('commandStatusChange', (guild, command, enabled) => this.set(guild.id, `cmd-${command.name}`, enabled))
.set('groupStatusChange', (guild, group, enabled) => this.set(guild.id, `grp-${group.id}`, enabled))
.set('guildCreate', (guild) => {
const settings = this.settings.get(guild.id);
if (!settings) return;
this.setupGuild(guild.id, settings);
})
.set('commandRegister', (command) => {
this.settings.forEach(([id, settings]) => {
if (id !== 'global' && !client.guilds.cache.has(id)) return;
const guild = client.guilds.cache.get(id);
this.setupGuildCommand(guild, command, settings);
});
})
.set('groupRegister', (group) => {
this.settings.forEach(([guild, settings]) => {
if (guild !== 'global' && !client.guilds.cache.has(guild)) return;
this.setupGuildGroup(client.guilds.cache.get(guild), group, settings);
});
});
Array.from(this.listeners).forEach(([event, listener]) => client.on(event, listener));
}
async destroy() {
Array.from(this.listeners)
.forEach(([event, listener]) => this.client.removeListener(event, listener));
this.listeners.clear();
}
async get(guild, key, defVal) {
const settings = this.settings.get(guild);
if (settings) {
return typeof settings[key] !== 'undefined' ? settings[key] : defVal;
}
return defVal;
}
async createOrUpdate(guildID, settings) {
if ((await this.db.query(Exists(Match(Index('guild_by_id'), guildID.toString()))))) {
return this.db.query(
Update(
Select(['ref'], Get(Match(Index('guild_by_id'), guildID.toString()))),
{ data: { id: guildID, ...settings } },
),
);
}
return this.db.query(
Create(FaunaCollection('guilds'), { data: { id: guildID, ...settings } }),
);
}
async set(guild, key, val) {
const guildId = guild;
let settings = this.settings.get(guildId);
if (!settings) {
settings = {};
this.settings.set(guildId, settings);
}
settings[key] = val;
await this.createOrUpdate(guildId !== 'global' ? guildId : 0, settings);
if (guild === 'global') this.updateOtherShards(key, val);
return val;
}
async remove(guild, key) {
const guildId = guild;
const settings = this.settings.get(guildId);
if (!settings || typeof settings[key] === 'undefined') return undefined;
const val = settings[key];
settings[key] = undefined;
await this.createOrUpdate(guildId !== 'global' ? guildId : 0, settings);
if (guildId === 'global') this.updateOtherShards(key, undefined);
return val;
}
async clear(guild) {
const guildID = guild;
if (!this.settings.has(guildID)) return;
this.settings.delete(guildID);
await this.db.query(
Remove(
Select(['ref'], Get(Match(Index('guild_by_id'), guildID !== 'global' ? guildID : 0))),
1,
'create',
),
);
}
setupGuild(guildId, settings) {
if (typeof guildId !== 'string') throw new TypeError('The guild must be a guild ID or "global".');
const guild = this.client.guilds.cache.get(guildId) || null;
// Load the command prefix
if (typeof settings.prefix !== 'undefined') {
if (guild) guild._commandPrefix = settings.prefix;
else this.client._commandPrefix = settings.prefix;
}
// Load all command/group statuses
(Array.from(this.client.registry.commands.values()))
.forEach((command) => this.setupGuildCommand(guild, command, settings));
(Array.from(this.client.registry.groups.values()))
.forEach((group) => this.setupGuildGroup(guild, group, settings));
}
// eslint-disable-next-line class-methods-use-this
setupGuildCommand(guild, command, settings) {
if (typeof settings[`cmd-${command.name}`] === 'undefined') return;
if (guild) {
if (!guild._commandsEnabled) guild._commandsEnabled = {};
guild._commandsEnabled[command.name] = settings[`cmd-${command.name}`];
} else {
command._globalEnabled = settings[`cmd-${command.name}`];
}
}
// eslint-disable-next-line class-methods-use-this
setupGuildGroup(guild, group, settings) {
if (typeof settings[`grp-${group.id}`] === 'undefined') return;
if (guild) {
if (!guild._groupsEnabled) guild._groupsEnabled = {};
guild._groupsEnabled[group.id] = settings[`grp-${group.id}`];
} else {
group._globalEnabled = settings[`grp-${group.id}`];
}
}
updateOtherShards(key, val) {
if (!this.client.shard) return;
this.client.shard.broadcastEval(`
const ids = [${this.client.shard.ids.join(',')}];
if(!this.shard.ids.some(id => ids.includes(id)) && this.provider && this.provider.settings) {
let global = this.provider.settings.get('global');
if(!global) {
global = {};
this.provider.settings.set('global', global);
}
global[${JSON.stringify(key)}] = ${typeof val !== 'undefined' ? JSON.stringify(val) : 'undefined'};
}
`);
}
}