-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsubscriptions.js
313 lines (289 loc) · 14.3 KB
/
subscriptions.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* This file defines several functions used in subscribing or unsubscribing to server alerts and outfit activity
* @module subscriptions
*/
/**
* @typedef {import('pg').Client} pg.Client
* @typedef {import('discord.js').CommandInteraction} discord.Interaction
*/
const config = require('./subscriptionConfig.js');
const { censusRequest, badQuery, faction } = require('./utils.js')
const { PermissionFlagsBits, ChannelType } = require('discord.js');
const i18n = require('i18n');
/**
* Case insensitive way of getting server names
* @param {string} server - server name to standardize
* @returns capitalized server name
*/
const standardizeName = function(server){
switch(server.toLowerCase()){
case "osprey":
return "Osprey";
case "wainwright":
return "Wainwright";
case "jaeger":
return "Jaeger";
case "soltech":
return "SolTech";
case "genudine":
return "Genudine";
case "ceres":
return "Ceres";
}
}
/**
* Get an overview of outfit information
* @param {string} tag - the tag of the outfit to check
* @param {string} platform - the platform of the outfit
* @returns {Promise<{ID: string, faction: string, alias: string, name: string}>}
* @throws if `tag` is not a valid outfit tag
*/
const outfitInfo = async function(tag, platform){
const response = await censusRequest(platform, 'outfit_list', `/outfit?alias_lower=${tag.toLowerCase()}&c:join=character^on:leader_character_id^to:character_id`);
if(typeof(response[0]) != undefined && response[0]){
let resObj = {
ID: response[0].outfit_id,
faction: response[0].leader_character_id_join_character.faction_id,
alias: response[0].alias,
name: response[0].name
};
return resObj;
}
throw `${tag} not found`;
}
/**
* environment: platform
* @example
* "ps2:v2": "pc",
* "ps2ps4us:v2": "ps4us",
* "ps2ps4eu:v2": "ps4eu"
*/
const environmentToPlatform = {
"ps2:v2": "pc",
"ps2ps4us:v2": "ps4us",
"ps2ps4eu:v2": "ps4eu"
}
module.exports = {
/**
* Subscribes to outfit member login and logouts
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to update
* @param {string} tag - the tag of the outfit to subscribe to
* @param {string} environment - the platform of the outfit
* @returns a message of the outcome of the subscription
* @throws if `tag` contains invalid characters
*/
subscribeActivity: async function(pgClient, channel, tag, environment, locale="en-US"){
//pgClient is the pgClient object from main
//channel is the discord channel ID
//tag is the outfit tag
//environment is ps2:v2, ps2ps4us:v2, or ps2ps4eu:v2
if(badQuery(tag)){
throw i18n.__({phrase: "Outfit search contains disallowed characters", locale: locale});
}
let outfit = await outfitInfo(tag, environment);
let platform = environmentToPlatform[environment];
let count = await pgClient.query('SELECT COUNT(channel) FROM outfitactivity WHERE id=$1 AND channel=$2 AND platform=$3', [outfit.ID, channel, platform]);
if(count.rows[0].count > 0){
throw i18n.__mf({phrase: "alreadySubscribedActivity", locale: locale}, {outfit: outfit.alias});
}
const color = faction(outfit.faction).color
pgClient.query("INSERT INTO outfitactivity (id, alias, color, channel, platform) VALUES ($1, $2, $3, $4, $5)", [outfit.ID, outfit.alias, color, channel, platform]);
try{
await config.initializeConfig(channel, pgClient);
return i18n.__mf({phrase: "subscribedActivity", locale: locale}, {outfit: outfit.alias});
}
catch(err){
return i18n.__mf({phrase: "subscribedActivityConfigFailed", locale: locale}, {outfit: outfit.alias});
}
},
/**
* Unsubscribes from outfit member login and logouts
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to unsubscribe from
* @param {string} tag - the tag of the outfit to unsubscribe from
* @param {string} environment - the platform of the outfit
* @returns a message of the outcome of the unsubscription
* @throws if `tag` contains invalid characters or if the outfit is not subscribed to
*/
unsubscribeActivity: async function(pgClient, channel, tag, environment, locale="en-US"){
if(badQuery(tag)){
throw i18n.__({phrase: "Outfit search contains disallowed characters", locale: locale});
}
let outfit = await outfitInfo(tag, environment);
let platform = environmentToPlatform[environment];
let count = await pgClient.query('SELECT COUNT(channel) FROM outfitactivity WHERE id=$1 AND channel=$2 AND platform=$3;', [outfit.ID, channel, platform]);
if(count.rows[0].count == 0){
throw i18n.__mf({phrase: "notSubscribedActivity", locale: locale}, {outfit: outfit.alias});
}
pgClient.query('DELETE FROM outfitactivity WHERE channel=$1 AND id=$2 AND platform=$3;', [channel, outfit.ID, platform]);
return i18n.__mf({phrase: "unsubscribedActivity", locale: locale}, {outfit: outfit.alias});
},
/**
* Subscribe to alerts on a server
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to send messages to
* @param {string} server - the server of the alerts to get
* @returns a message of the outcome of the subscription
* @throws if already subscribed to the `server` alert
*/
subscribeAlert: async function(pgClient, channel, server, locale="en-US"){
let count = await pgClient.query("SELECT count(*) FROM alerts WHERE channel=$1 AND world=$2;", [channel, server]);
if(count.rows[0].count == 0){
pgClient.query("INSERT INTO alerts (channel, world) VALUES ($1, $2);", [channel, server]);
try{
await config.initializeConfig(channel, pgClient);
return i18n.__mf({phrase: "subscribedAlerts", locale}, {server: standardizeName(server)});
}
catch(err){
return i18n.__mf({phrase: "subscribedAlertsConfigFailed", locale}, {server: standardizeName(server)});
}
}
throw i18n.__mf({phrase: "alreadySubscribedAlerts", locale}, {server: standardizeName(server)});
},
/**
* Unsubscribe from alerts on a Server
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to unsubscribe from
* @param {string} server - the server of the alerts to unsubscribe from
* @returns the message of the outcome of the unsubscription
* @throws if not subscribed to the `server` alert
*/
unsubscribeAlert: async function(pgClient, channel, server, locale="en-US"){
let count = await pgClient.query("SELECT COUNT(*) FROM alerts WHERE channel = $1 AND world=$2", [channel, server]);
if(count.rows[0].count == 0){
return i18n.__mf({phrase: "notSubscribedAlerts", locale}, {server: standardizeName(server)});
}
pgClient.query("DELETE FROM alerts WHERE channel=$1 AND world=$2", [channel, server]);
return i18n.__mf({phrase: "unsubscribedAlerts", locale}, {server: standardizeName(server)});
},
/**
* Get updates on when an outfit captures a base
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to send messages to
* @param {string} tag - the tag of the outfit
* @param {string} environment - the platform of the outfit
* @returns the message of the outcome of the subscription
* @throws if `tag` contains invalid characters or if the outfit is already subscribed to
*/
subscribeCaptures: async function(pgClient, channel, tag, environment, locale="en-US"){
if(badQuery(tag)){
throw i18n.__({phrase: "Outfit search contains disallowed characters", locale: locale});
}
let outfit = await outfitInfo(tag, environment);
let platform = environmentToPlatform[environment];
let count = await pgClient.query('SELECT COUNT(channel) FROM outfitcaptures WHERE id=$1 AND channel=$2 AND platform=$3;', [outfit.ID, channel, platform]);
if (count.rows[0].count > 0){
return i18n.__mf({phrase: "alreadySubscribedCaptures", locale: locale}, {outfit: outfit.alias});
}
await pgClient.query("INSERT INTO outfitcaptures (id, alias, channel, name, platform) VALUES ($1, $2, $3, $4, $5)", [outfit.ID, outfit.alias, channel, outfit.name, platform]);
try{
await config.initializeConfig(channel, pgClient)
return i18n.__mf({phrase: "subscribedCaptures", locale: locale}, {outfit: outfit.alias});
}
catch(err){
return i18n.__mf({phrase: "subscribedCapturesConfigFailed", locale: locale}, {outfit: outfit.alias});
}
},
/**
* Unsubscribe from updates on when an outfit captures a base
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to unsubscribe from
* @param {string} tag - the tag of the outfit
* @param {string} environment - the platform of the outfit
* @returns the message of the outcome of the unsubscription
* @throws if `tag` contains invalid characters or if the outfit is not subscribed to
*/
unsubscribeCaptures: async function(pgClient, channel, tag, environment, locale="en-US"){
if(badQuery(tag)){
throw i18n.__({phrase: "Outfit search contains disallowed characters", locale: locale});
}
let outfit = await outfitInfo(tag, environment);
let platform = environmentToPlatform[environment];
let count = await pgClient.query('SELECT COUNT(channel) FROM outfitcaptures WHERE id=$1 AND channel=$2 AND platform=$3;', [outfit.ID, channel, platform]);
if(count.rows[0].count == 0){
return i18n.__mf({phrase: "notSubscribedCaptures", locale: locale}, {outfit: outfit.alias});
}
pgClient.query('DELETE FROM outfitcaptures WHERE channel=$1 AND id=$2 AND platform=$3;', [channel, outfit.ID, platform]);
return i18n.__mf({phrase: "unsubscribedCaptures", locale: locale}, {outfit: outfit.alias});
},
/**
* Subscribes to updates when a continent is unlocked on a server
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to send messages to
* @param {string} server - the server to subscribe to
* @returns the message of the outcome of the subscription
* @throws if already subscribed to `server`
*/
subscribeUnlocks: async function(pgClient, channel, server, locale="en-US"){
let count = await pgClient.query("SELECT count(*) FROM unlocks WHERE channel=$1 AND world=$2;", [channel, server]);
if(count.rows[0].count == 0){
pgClient.query("INSERT INTO unlocks (channel, world) VALUES ($1, $2);", [channel, server]);
try{
await config.initializeConfig(channel, pgClient)
return i18n.__mf({phrase: "subscribedUnlocks", locale: locale}, {server: standardizeName(server)});
}
catch(err){
return i18n.__mf({phrase: "subscribedUnlocksConfigFailed", locale: locale}, {server: standardizeName(server)});
}
}
throw i18n.__mf({phrase: "alreadySubscribedUnlocks", locale: locale}, {server: standardizeName(server)});
},
/**
* Unsubscribe from updates when a continent is unlocked on a server
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channel - the id of the channel to unsubscribe from
* @param {string} server - the server to unsubscribe from
* @returns the outcome of the unsubscription
* @throws if not subscribed to `server`
*/
unsubscribeUnlocks: async function(pgClient, channel, server, locale="en-US"){
let count = await pgClient.query("SELECT COUNT(*) FROM unlocks WHERE channel = $1 AND world=$2", [channel, server]);
if(count.rows[0].count == 0){
throw i18n.__mf({phrase: "notSubscribedUnlocks", locale: locale}, {server: standardizeName(server)});
}
pgClient.query("DELETE FROM unlocks WHERE channel=$1 AND world=$2", [channel, server]);
return i18n.__mf({phrase: "unsubscribedUnlocks", locale: locale}, {server: standardizeName(server)});
},
/**
* Unsubscribe from all subscriptions
* @param {pg.Client} pgClient - Postgres client to use
* @param {string} channelId - the id of the channel to unsubscribe from
* @returns a message of the outcome of the unsubscription
*/
unsubscribeAll: async function(pgClient, channelId, locale="en-US"){
const commands = [
"DELETE FROM alerts WHERE channel = $1",
"DELETE FROM outfitactivity WHERE channel = $1",
"DELETE FROM outfitcaptures WHERE channel = $1",
"DELETE FROM news WHERE channel = $1",
"DELETE FROM subscriptionConfig WHERE channel = $1",
"DELETE FROM unlocks WHERE channel = $1"
];
for(const command of commands){
pgClient.query(command, [channelId])
.catch(err => console.log(err));
}
return i18n.__({phrase: "unsubscribedAll", locale: locale});
},
/**
* Ensure that the channel has the correct permissions to allow the bot to send messages
* @param {discord.Interaction} interaction - the interaction to check
* @param {string} user - Auraxis Bot id
* @param {string} locale - the locale of the channel
*/
permissionCheck: async function(interaction, user, locale="en-US"){
if(interaction.channel.type == ChannelType.DM){
return;
}
let channel = interaction.channel;
if(channel.isThread()){
channel = interaction.channel.parent;
}
if(!await channel.permissionsFor(user).has([PermissionFlagsBits.ViewChannel, PermissionFlagsBits.SendMessages])){
await interaction.followUp({
content: i18n.__({phrase: "insufficientPermissions", locale: locale})
});
}
}
}