-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscord.ts
More file actions
464 lines (406 loc) · 13.9 KB
/
discord.ts
File metadata and controls
464 lines (406 loc) · 13.9 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { eq, and } from "drizzle-orm";
import { Platform } from "../types/types.d";
import { db } from "./db";
import {
dbGuildYouTubeSubscriptionsTable,
dbGuildTwitchSubscriptionsTable,
dbDiscordTable,
dbYouTubeTable,
dbTwitchTable,
} from "./schema";
// Check if the guild is tracking the user already
export async function checkIfGuildIsTrackingUserAlready(
platform: Platform,
userId: string,
guildId: string,
): Promise<
| {
success: true;
data:
| (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[]
| (typeof dbGuildTwitchSubscriptionsTable.$inferSelect)[]
| null;
}
| { success: false; data: null }
> {
console.log(
`Checking if guild ${guildId} is tracking user ${userId} on platform ${platform}`,
);
try {
let result: any[] = [];
if (platform === Platform.YouTube) {
result = await db
.select()
.from(dbGuildYouTubeSubscriptionsTable)
.where(
and(
eq(
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
userId,
),
eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId),
),
);
} else if (platform === Platform.Twitch) {
result = await db
.select()
.from(dbGuildTwitchSubscriptionsTable)
.where(
and(
eq(
dbGuildTwitchSubscriptionsTable.twitchChannelId,
userId,
),
eq(dbGuildTwitchSubscriptionsTable.guildId, guildId),
),
);
} else {
console.error("Invalid platform provided for tracking check.");
return { success: false, data: null };
}
return {
success: true,
data: result.length > 0 ? result : null,
};
} catch (error) {
console.error("Error checking if guild is tracking user:", error);
return { success: false, data: null };
}
}
export async function discordAddGuildTrackingUser(
guildId: string,
platform: Platform,
platformUserId: string,
guildChannelId: string,
roleId: string | null,
isDm: boolean,
// YouTube specific tracking options
youtubeTrackVideos?: boolean | null,
youtubeTrackShorts?: boolean | null,
youtubeTrackLive?: boolean | null,
): Promise<{ success: boolean; data: [] }> {
console.log(
`Adding guild ${guildId} tracking for user ${platformUserId} on platform ${platform}`,
);
try {
if (platform === Platform.YouTube) {
if (
youtubeTrackVideos == null ||
youtubeTrackShorts == null ||
youtubeTrackLive == null
) {
console.error(
"YouTube tracking options must be provided for YouTube subscriptions.",
);
return { success: false, data: [] };
}
await db.insert(dbGuildYouTubeSubscriptionsTable).values({
youtubeChannelId: platformUserId,
guildId,
notificationChannelId: guildChannelId,
notificationRoleId: roleId,
isDm,
trackVideos: youtubeTrackVideos,
trackShorts: youtubeTrackShorts,
trackStreams: youtubeTrackLive,
});
} else if (platform === Platform.Twitch) {
await db.insert(dbGuildTwitchSubscriptionsTable).values({
twitchChannelId: platformUserId,
guildId,
notificationChannelId: guildChannelId,
notificationRoleId: roleId,
isDm,
});
} else {
console.error("Invalid platform provided.");
return { success: false, data: [] };
}
return { success: true, data: [] };
} catch (error) {
console.error("Error adding guild tracking user:", error);
return { success: false, data: [] };
}
}
// Get all the Discord guilds that are tracking either YouTube or Twitch channels
export async function discordGetAllGuildsTrackingChannel(
platform: Platform,
platformUserId: string,
): Promise<
| {
success: true;
data: (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[];
}
| {
success: true;
data: (typeof dbGuildTwitchSubscriptionsTable.$inferSelect)[];
}
| { success: false; data: [] }
> {
try {
if (platform === Platform.YouTube) {
const result = await db
.select()
.from(dbGuildYouTubeSubscriptionsTable)
.where(
eq(
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
platformUserId,
),
);
return {
success: true,
data: result,
};
} else if (platform === Platform.Twitch) {
const result = await db
.select()
.from(dbGuildTwitchSubscriptionsTable)
.where(
eq(
dbGuildTwitchSubscriptionsTable.twitchChannelId,
platformUserId,
),
);
return {
success: true,
data: result,
};
} else {
console.error("Invalid platform provided for tracking guilds.");
return { success: false, data: [] };
}
} catch (error) {
console.error("Error getting all guilds tracking channels:", error);
return { success: false, data: [] };
}
}
// Get all tracked in the guild
export async function discordGetAllTrackedInGuild(guildId: string): Promise<
| {
success: true;
data: {
youtubeSubscriptions: {
subscription: typeof dbGuildYouTubeSubscriptionsTable.$inferSelect;
youtubeChannel: typeof dbYouTubeTable.$inferSelect;
discord: typeof dbDiscordTable.$inferSelect;
}[];
twitchSubscriptions: {
subscription: typeof dbGuildTwitchSubscriptionsTable.$inferSelect;
twitchChannel: typeof dbTwitchTable.$inferSelect;
discord: typeof dbDiscordTable.$inferSelect;
}[];
};
}
| { success: false; data: null }
> {
try {
const youtubeSubscriptions = await db
.select({
subscription: dbGuildYouTubeSubscriptionsTable,
youtubeChannel: dbYouTubeTable,
discord: dbDiscordTable,
})
.from(dbGuildYouTubeSubscriptionsTable)
.where(eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId))
.innerJoin(
dbYouTubeTable,
eq(
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
dbYouTubeTable.youtubeChannelId,
),
)
.innerJoin(
dbDiscordTable,
eq(
dbGuildYouTubeSubscriptionsTable.guildId,
dbDiscordTable.guildId,
),
);
const twitchSubscriptions = await db
.select({
subscription: dbGuildTwitchSubscriptionsTable,
twitchChannel: dbTwitchTable,
discord: dbDiscordTable,
})
.from(dbGuildTwitchSubscriptionsTable)
.where(eq(dbGuildTwitchSubscriptionsTable.guildId, guildId))
.innerJoin(
dbTwitchTable,
eq(
dbGuildTwitchSubscriptionsTable.twitchChannelId,
dbTwitchTable.twitchChannelId,
),
)
.innerJoin(
dbDiscordTable,
eq(
dbGuildTwitchSubscriptionsTable.guildId,
dbDiscordTable.guildId,
),
);
return {
success: true,
data: {
youtubeSubscriptions,
twitchSubscriptions,
},
};
} catch (error) {
console.error(
"Error getting all tracked subscriptions in guild:",
error,
);
return { success: false, data: null };
}
}
// Remove tracking for a specific channel in a guild
// TODO: Make it so that if the channel is no longer tracked by any guilds, it is removed from the db entirely
export async function discordRemoveGuildTrackingChannel(
trackingId: string,
): Promise<{ success: boolean; data: [] }> {
console.log(`Removing tracking for ID: ${trackingId}`);
const parts = trackingId.split(".");
if (parts.length !== 2) {
console.error(
"Invalid trackingId format. Expected format: 'platform.id'",
);
return { success: false, data: [] };
}
const [platform, platformTrackingId] = parts;
try {
if (platform === Platform.YouTube) {
await db
.delete(dbGuildYouTubeSubscriptionsTable)
.where(
eq(
dbGuildYouTubeSubscriptionsTable.id,
Number(platformTrackingId),
),
);
} else if (platform === Platform.Twitch) {
await db
.delete(dbGuildTwitchSubscriptionsTable)
.where(
eq(
dbGuildTwitchSubscriptionsTable.id,
Number(platformTrackingId),
),
);
} else {
console.error("Invalid platform provided for removal.");
return { success: false, data: [] };
}
return { success: true, data: [] };
} catch (error) {
console.error("Error removing guild tracking channel:", error);
return { success: false, data: [] };
}
}
// Add a new guild to track
export async function discordAddNewGuild(
guildId: string,
isDm?: boolean,
): Promise<{ success: boolean; data: [] }> {
console.log(`Adding new guild to track: ${guildId}`);
try {
await db.insert(dbDiscordTable).values({
guildId,
allowedPublicSharing: false,
isInServer: true,
memberCount: 0,
isDm: isDm ?? false,
});
return { success: true, data: [] };
} catch (error) {
console.error("Error adding new guild to track:", error);
return { success: false, data: [] };
}
}
// "Remove" a guild from tracking
// Basically just set isInServer to false for archival purposes
export async function discordRemoveGuildFromTracking(
guildId: string,
): Promise<{ success: boolean; data: [] }> {
console.log(`Removing guild from tracking: ${guildId}`);
try {
await db
.update(dbDiscordTable)
.set({ isInServer: false })
.where(eq(dbDiscordTable.guildId, guildId));
return { success: true, data: [] };
} catch (error) {
console.error("Error removing guild from tracking:", error);
return { success: false, data: [] };
}
}
// Check if the DM channel is already in the "guilds" table
export async function discordCheckIfDmChannelExists(
channelId: string,
): Promise<{ success: boolean; data: (typeof dbDiscordTable.$inferSelect)[] }> {
console.log(`Checking if DM channel exists: ${channelId}`);
try {
const result = await db
.select()
.from(dbDiscordTable)
.where(
and(
eq(dbDiscordTable.guildId, channelId),
eq(dbDiscordTable.isDm, true),
),
);
return { success: true, data: result };
} catch (error) {
console.error("Error checking if DM channel exists:", error);
return { success: false, data: [] };
}
}
// Bot updates
export async function discordUpdateSubscriptionAddChannel(
guildId: string,
channelId: string,
): Promise<{ success: boolean; data: [] }> {
console.log(
`Updating guild ${guildId} to set subscription channel to ${channelId}`,
);
try {
await db
.update(dbDiscordTable)
.set({ feedrUpdatesChannelId: channelId })
.where(eq(dbDiscordTable.guildId, guildId));
return { success: true, data: [] };
} catch (error) {
console.error("Error updating subscription channel:", error);
return { success: false, data: [] };
}
}
export async function discordUpdateSubscriptionRemoveChannel(
guildId: string,
): Promise<{ success: boolean; data: [] }> {
console.log(`Removing subscription channel for guild ${guildId}`);
try {
await db
.update(dbDiscordTable)
.set({ feedrUpdatesChannelId: null })
.where(eq(dbDiscordTable.guildId, guildId));
return { success: true, data: [] };
} catch (error) {
console.error("Error removing subscription channel:", error);
return { success: false, data: [] };
}
}
export async function discordUpdateSubscriptionCheckGuild(
guildId: string,
): Promise<{ success: boolean; data: (typeof dbDiscordTable.$inferSelect)[] }> {
console.log(`Checking subscription settings for guild ${guildId}`);
try {
const result = await db
.select()
.from(dbDiscordTable)
.where(eq(dbDiscordTable.guildId, guildId));
return { success: true, data: result };
} catch (error) {
console.error("Error checking subscription settings for guild:", error);
return { success: false, data: [] };
}
}