forked from GalvinPython/feedr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.ts
More file actions
260 lines (201 loc) · 6.47 KB
/
database.ts
File metadata and controls
260 lines (201 loc) · 6.47 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
import type { dbDiscordTable } from "../types/database";
import { Pool } from "pg";
import { dbCredentials } from "../config";
// import path from "path";
// import { Database } from "bun:sqlite";
// const db = new Database(path.resolve(process.cwd(), "db.sqlite3"));
if (
!dbCredentials.host ||
!dbCredentials.port ||
!dbCredentials.user ||
!dbCredentials.password ||
!dbCredentials.database
) {
throw new Error("Database credentials are not set");
}
export const pool: Pool = new Pool({
host: dbCredentials.host,
port: parseInt(dbCredentials.port),
user: dbCredentials.user,
password: dbCredentials.password,
database: dbCredentials.database,
});
// #region YouTube
export async function checkIfGuildIsTrackingChannelAlready(
channelId: string,
guild_id: string,
): Promise<dbDiscordTable[]> {
const query = `SELECT * FROM discord WHERE platform_user_id = ? AND guild_id = ?`;
try {
const statement = db.prepare(query);
const result = statement.all(channelId, guild_id);
return result;
} catch (err) {
console.error(
"Error checking if guild is tracking channel already:",
err,
);
throw err;
}
}
export async function addNewGuildToTrackChannel(
guild_id: string,
channelId: string,
guild_channel_id: string,
guild_ping_role: string | null,
) {
const query = `INSERT INTO discord (guild_id, platform_user_id, guild_channel_id, guild_ping_role, guild_platform) VALUES (?, ?, ?, ?, 'youtube')`;
try {
const statement = db.prepare(query);
statement.run(guild_id, channelId, guild_channel_id, guild_ping_role);
return true;
} catch (err) {
console.error("Error adding guild to track channel:", err);
return false;
}
}
export async function getGuildsTrackingChannel(channelId: string) {
const query = `SELECT * FROM discord WHERE platform_user_id = ?`;
try {
const statement = db.prepare(query);
const results = statement.all(channelId);
return results;
} catch (err) {
console.error("Error getting guilds tracking channel:", err);
throw err;
}
}
export async function updateVideoId(channelId: string, videoId: string) {
const query = `UPDATE youtube SET latest_video_id = ? WHERE youtube_channel_id = ?`;
try {
const statement = db.prepare(query);
statement.run(videoId, channelId);
return true;
} catch (err) {
console.error("Error updating video ID:", err);
return false;
}
}
export async function stopGuildTrackingChannel(
guild_id: string,
channelId: string,
) {
const query = `DELETE FROM discord WHERE guild_id = ? AND platform_user_id = ?`;
try {
const statement = db.prepare(query);
statement.run(guild_id, channelId);
return true;
} catch (err) {
console.error("Error stopping guild tracking channel:", err);
return false;
}
}
// #endregion
// #region Twitch
export async function twitchGetAllChannelsToTrack() {
const query = `SELECT * FROM twitch`;
try {
const statement = db.prepare(query);
const results = statement.all();
return results;
} catch (err) {
console.error("Error getting all Twitch channels to track:", err);
throw err;
}
}
export async function twitchCheckIfChannelIsAlreadyTracked(channelId: string) {
const query = `SELECT * FROM twitch WHERE twitch_channel_id = ?`;
try {
const statement = db.prepare(query);
const result = statement.all(channelId);
return result.length > 0;
} catch (err) {
console.error(
"Error checking if Twitch channel is already tracked:",
err,
);
throw err;
}
}
export async function twitchAddNewChannelToTrack(
channelId: string,
isLive: boolean,
) {
const query = `INSERT INTO twitch (twitch_channel_id, is_live) VALUES (?, ?)`;
try {
const statement = db.prepare(query);
statement.run(channelId, isLive);
return true;
} catch (err) {
console.error("Error adding Twitch channel to track:", err);
return false;
}
}
export async function twitchAddNewGuildToTrackChannel(
guild_id: string,
channelId: string,
guild_channel_id: string,
guild_ping_role: string | null,
) {
const query = `INSERT INTO discord (guild_id, platform_user_id, guild_channel_id, guild_ping_role, guild_platform) VALUES (?, ?, ?, ?, 'twitch')`;
try {
const statement = db.prepare(query);
statement.run(guild_id, channelId, guild_channel_id, guild_ping_role);
return true;
} catch (err) {
console.error("Error adding guild to track Twitch channel:", err);
return false;
}
}
export async function twitchGetGuildsTrackingChannel(channelId: string) {
const query = `SELECT * FROM discord WHERE platform_user_id = ?`;
try {
const statement = db.prepare(query);
const results = statement.all(channelId);
return results;
} catch (err) {
console.error("Error getting guilds tracking Twitch channel:", err);
throw err;
}
}
export async function twitchUpdateIsLive(channelId: string, isLive: boolean) {
const query = `UPDATE twitch SET is_live = ? WHERE twitch_channel_id = ?`;
try {
const statement = db.prepare(query);
statement.run(isLive, channelId);
return true;
} catch (err) {
console.error("Error updating is live:", err);
return false;
}
}
export async function twitchStopGuildTrackingChannel(
guild_id: string,
channelId: string,
) {
const query = `DELETE FROM discord WHERE guild_id = ? AND platform_user_id = ?`;
try {
const statement = db.prepare(query);
statement.run(guild_id, channelId);
return true;
} catch (err) {
console.error("Error stopping guild tracking Twitch channel:", err);
return false;
}
}
// #endregion
// #region i have no idea what im doing here
export async function getAllTrackedInGuild(
guild_id: string,
): Promise<dbDiscordTable[]> {
const query = `SELECT * FROM discord WHERE guild_id = ?`;
try {
const statement = db.prepare(query);
const results = statement.all(guild_id);
return results as dbDiscordTable[];
} catch (err) {
console.error("Error getting all tracked in guild:", err);
throw err;
}
}
// #endregion