@@ -2,7 +2,7 @@ import type { dbDiscordTable, dbYouTube } from "../types/database";
22
33import { Pool } from "pg" ;
44
5- import { dbCredentials } from "../config" ;
5+ import { dbCredentials , env } from "../config" ;
66
77// import path from "path";
88// import { Database } from "bun:sqlite";
@@ -30,7 +30,17 @@ export const pool: Pool = new Pool({
3030export async function initTables ( ) : Promise < boolean > {
3131 const createDiscordTable = `
3232 CREATE TABLE IF NOT EXISTS discord (
33- guild_id TEXT PRIMARY KEY
33+ guild_id TEXT PRIMARY KEY,
34+ is_dm BOOLEAN NOT NULL DEFAULT FALSE,
35+ allowed_public_sharing BOOLEAN NOT NULL DEFAULT FALSE
36+ );
37+ ` ;
38+
39+ const createBlueskyTable = `
40+ CREATE TABLE IF NOT EXISTS bluesky (
41+ bluesky_user_id TEXT PRIMARY KEY,
42+ latest_post_id TEXT,
43+ latest_reply_id TEXT
3444 );
3545 ` ;
3646
@@ -54,14 +64,29 @@ export async function initTables(): Promise<boolean> {
5464 );
5565 ` ;
5666
67+ const createGuildBlueskySubscriptionsTable = `
68+ CREATE TABLE IF NOT EXISTS guild_bluesky_subscriptions (
69+ id SERIAL PRIMARY KEY,
70+ guild_id TEXT NOT NULL REFERENCES discord(guild_id),
71+ bluesky_user_id TEXT NOT NULL REFERENCES bluesky(bluesky_user_id),
72+ notification_channel_id TEXT NOT NULL,
73+ notification_role_id TEXT,
74+ is_dm BOOLEAN NOT NULL DEFAULT FALSE,
75+ check_for_replies BOOLEAN NOT NULL DEFAULT FALSE
76+ );
77+ ` ;
78+
5779 const createGuildYouTubeSubscriptionsTable = `
5880 CREATE TABLE IF NOT EXISTS guild_youtube_subscriptions (
5981 id SERIAL PRIMARY KEY,
6082 guild_id TEXT NOT NULL REFERENCES discord(guild_id),
6183 youtube_channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id),
6284 notification_channel_id TEXT NOT NULL,
6385 notification_role_id TEXT,
64- is_dm BOOLEAN DEFAULT FALSE
86+ is_dm BOOLEAN NOT NULL DEFAULT FALSE,
87+ track_videos BOOLEAN NOT NULL DEFAULT FALSE,
88+ track_shorts BOOLEAN NOT NULL DEFAULT FALSE,
89+ track_streams BOOLEAN NOT NULL DEFAULT FALSE
6590 );
6691 ` ;
6792
@@ -72,15 +97,39 @@ export async function initTables(): Promise<boolean> {
7297 twitch_channel_id TEXT NOT NULL REFERENCES twitch(twitch_channel_id),
7398 notification_channel_id TEXT NOT NULL,
7499 notification_role_id TEXT,
75- is_dm BOOLEAN DEFAULT FALSE
100+ is_dm BOOLEAN NOT NULL DEFAULT FALSE
76101 );
77102 ` ;
78103
79104 const createBotInfoTable = `
80105 CREATE TABLE IF NOT EXISTS bot_info (
81- guilds_total INTEGER NOT NULL,
82- channels_tracked INTEGER NOT NULL,
83- updated_at TIMESTAMP DEFAULT now()
106+ guilds_total INTEGER NOT NULL DEFAULT 0,
107+ channels_tracked INTEGER NOT NULL DEFAULT 0,
108+ total_members INTEGER NOT NULL DEFAULT 0,
109+ updated_at TIMESTAMP NOT NULL DEFAULT now(),
110+ extended_info_updated_at TIMESTAMP NOT NULL DEFAULT now()
111+ );
112+ ` ;
113+
114+ const createBotInfoNotificationsTable = `
115+ CREATE TABLE IF NOT EXISTS bot_info_notifications (
116+ date DATE NOT NULL,
117+ total_youtube INTEGER NOT NULL DEFAULT 0,
118+ total_twitch INTEGER NOT NULL DEFAULT 0
119+ );
120+ ` ;
121+
122+ const createBotInfoTopChannelsTable = `
123+ CREATE TABLE IF NOT EXISTS bot_info_top_channels (
124+ youtube_channel_id TEXT PRIMARY KEY REFERENCES youtube(youtube_channel_id),
125+ guilds_tracking INTEGER NOT NULL DEFAULT 0
126+ );
127+ ` ;
128+
129+ const createBotInfoTopGuildsTable = `
130+ CREATE TABLE IF NOT EXISTS bot_info_top_guilds (
131+ guild_id TEXT PRIMARY KEY REFERENCES discord(guild_id),
132+ members INTEGER NOT NULL DEFAULT 0
84133 );
85134 ` ;
86135
@@ -99,12 +148,18 @@ export async function initTables(): Promise<boolean> {
99148 await pool . query ( createDiscordTable ) ;
100149 console . log ( "Discord table created" ) ;
101150
151+ await pool . query ( createBlueskyTable ) ;
152+ console . log ( "Bluesky table created" ) ;
153+
102154 await pool . query ( createYouTubeTable ) ;
103155 console . log ( "YouTube table created" ) ;
104156
105157 await pool . query ( createTwitchTable ) ;
106158 console . log ( "Twitch table created" ) ;
107159
160+ await pool . query ( createGuildBlueskySubscriptionsTable ) ;
161+ console . log ( "Guild Bluesky Subscriptions table created" ) ;
162+
108163 await pool . query ( createGuildYouTubeSubscriptionsTable ) ;
109164 console . log ( "Guild YouTube Subscriptions table created" ) ;
110165
@@ -114,6 +169,15 @@ export async function initTables(): Promise<boolean> {
114169 await pool . query ( createBotInfoTable ) ;
115170 console . log ( "Bot Info table created" ) ;
116171
172+ await pool . query ( createBotInfoNotificationsTable ) ;
173+ console . log ( "Bot Info Notifications table created" ) ;
174+
175+ await pool . query ( createBotInfoTopChannelsTable ) ;
176+ console . log ( "Bot Info Top Channels table created" ) ;
177+
178+ await pool . query ( createBotInfoTopGuildsTable ) ;
179+ console . log ( "Bot Info Top Guilds table created" ) ;
180+
117181 await pool . query ( createAuditLogsTable ) ;
118182 console . log ( "Audit Logs table created" ) ;
119183
@@ -146,7 +210,7 @@ export async function checkIfChannelIsAlreadyTracked(channelId: string) {
146210export async function addNewChannelToTrack ( channelId : string ) {
147211 console . log ( "Adding channel to track:" , channelId ) ;
148212 const res = await fetch (
149- `https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&id=${ channelId . replace ( "UC" , "UU" ) } &key=${ process . env . YOUTUBE_API_KEY } ` ,
213+ `https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&id=${ channelId . replace ( "UC" , "UU" ) } &key=${ env . youtubeApiKey } ` ,
150214 ) ;
151215
152216 if ( ! res . ok ) {
0 commit comments