|
| 1 | +import { pool } from "../database"; |
| 2 | + |
| 3 | +export default async function initTables(): Promise<boolean> { |
| 4 | + const createDiscordTable = ` |
| 5 | + CREATE TABLE IF NOT EXISTS discord ( |
| 6 | + guild_id TEXT PRIMARY KEY, |
| 7 | + is_dm BOOLEAN NOT NULL DEFAULT FALSE, |
| 8 | + allowed_public_sharing BOOLEAN NOT NULL DEFAULT FALSE |
| 9 | + ); |
| 10 | + `; |
| 11 | + |
| 12 | + const createBlueskyTable = ` |
| 13 | + CREATE TABLE IF NOT EXISTS bluesky ( |
| 14 | + bluesky_user_id TEXT PRIMARY KEY, |
| 15 | + latest_post_id TEXT, |
| 16 | + latest_reply_id TEXT |
| 17 | + ); |
| 18 | + `; |
| 19 | + |
| 20 | + const createYouTubeTable = ` |
| 21 | + CREATE TABLE IF NOT EXISTS youtube ( |
| 22 | + youtube_channel_id TEXT PRIMARY KEY, |
| 23 | + latest_video_id TEXT, |
| 24 | + latest_video_id_updated TIMESTAMP, |
| 25 | + latest_short_id TEXT, |
| 26 | + latest_short_id_updated TIMESTAMP, |
| 27 | + latest_stream_id TEXT, |
| 28 | + latest_stream_id_updated TIMESTAMP, |
| 29 | + youtube_channel_is_live BOOLEAN |
| 30 | + ); |
| 31 | + `; |
| 32 | + |
| 33 | + const createTwitchTable = ` |
| 34 | + CREATE TABLE IF NOT EXISTS twitch ( |
| 35 | + twitch_channel_id TEXT PRIMARY KEY, |
| 36 | + twitch_channel_is_live BOOLEAN NOT NULL |
| 37 | + ); |
| 38 | + `; |
| 39 | + |
| 40 | + const createGuildBlueskySubscriptionsTable = ` |
| 41 | + CREATE TABLE IF NOT EXISTS guild_bluesky_subscriptions ( |
| 42 | + id SERIAL PRIMARY KEY, |
| 43 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 44 | + bluesky_user_id TEXT NOT NULL REFERENCES bluesky(bluesky_user_id), |
| 45 | + notification_channel_id TEXT NOT NULL, |
| 46 | + notification_role_id TEXT, |
| 47 | + is_dm BOOLEAN NOT NULL DEFAULT FALSE, |
| 48 | + check_for_replies BOOLEAN NOT NULL DEFAULT FALSE |
| 49 | + ); |
| 50 | + `; |
| 51 | + |
| 52 | + const createGuildYouTubeSubscriptionsTable = ` |
| 53 | + CREATE TABLE IF NOT EXISTS guild_youtube_subscriptions ( |
| 54 | + id SERIAL PRIMARY KEY, |
| 55 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 56 | + youtube_channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id), |
| 57 | + notification_channel_id TEXT NOT NULL, |
| 58 | + notification_role_id TEXT, |
| 59 | + is_dm BOOLEAN NOT NULL DEFAULT FALSE, |
| 60 | + track_videos BOOLEAN NOT NULL DEFAULT FALSE, |
| 61 | + track_shorts BOOLEAN NOT NULL DEFAULT FALSE, |
| 62 | + track_streams BOOLEAN NOT NULL DEFAULT FALSE |
| 63 | + ); |
| 64 | + `; |
| 65 | + |
| 66 | + const createGuildTwitchSubscriptionsTable = ` |
| 67 | + CREATE TABLE IF NOT EXISTS guild_twitch_subscriptions ( |
| 68 | + id SERIAL PRIMARY KEY, |
| 69 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 70 | + twitch_channel_id TEXT NOT NULL REFERENCES twitch(twitch_channel_id), |
| 71 | + notification_channel_id TEXT NOT NULL, |
| 72 | + notification_role_id TEXT, |
| 73 | + is_dm BOOLEAN NOT NULL DEFAULT FALSE |
| 74 | + ); |
| 75 | + `; |
| 76 | + |
| 77 | + const createBotInfoTable = ` |
| 78 | + CREATE TABLE IF NOT EXISTS bot_info ( |
| 79 | + guilds_total INTEGER NOT NULL DEFAULT 0, |
| 80 | + channels_tracked INTEGER NOT NULL DEFAULT 0, |
| 81 | + total_members INTEGER NOT NULL DEFAULT 0, |
| 82 | + updated_at TIMESTAMP NOT NULL DEFAULT now(), |
| 83 | + extended_info_updated_at TIMESTAMP NOT NULL DEFAULT now() |
| 84 | + ); |
| 85 | + `; |
| 86 | + |
| 87 | + const createBotInfoNotificationsTable = ` |
| 88 | + CREATE TABLE IF NOT EXISTS bot_info_notifications ( |
| 89 | + date DATE NOT NULL, |
| 90 | + total_youtube INTEGER NOT NULL DEFAULT 0, |
| 91 | + total_twitch INTEGER NOT NULL DEFAULT 0 |
| 92 | + ); |
| 93 | + `; |
| 94 | + |
| 95 | + const createBotInfoNotificationsTimingsTable = ` |
| 96 | + CREATE TABLE IF NOT EXISTS bot_info_notifications_timings ( |
| 97 | + time DATETIME NOT NULL, |
| 98 | + channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id), |
| 99 | + time_ms INTEGER NOT NULL DEFAULT 0, |
| 100 | + ); |
| 101 | + `; |
| 102 | + |
| 103 | + const createBotInfoTopChannelsTable = ` |
| 104 | + CREATE TABLE IF NOT EXISTS bot_info_top_channels ( |
| 105 | + youtube_channel_id TEXT PRIMARY KEY REFERENCES youtube(youtube_channel_id), |
| 106 | + guilds_tracking INTEGER NOT NULL DEFAULT 0 |
| 107 | + ); |
| 108 | + `; |
| 109 | + |
| 110 | + const createBotInfoTopGuildsTable = ` |
| 111 | + CREATE TABLE IF NOT EXISTS bot_info_top_guilds ( |
| 112 | + guild_id TEXT PRIMARY KEY REFERENCES discord(guild_id), |
| 113 | + members INTEGER NOT NULL DEFAULT 0 |
| 114 | + ); |
| 115 | + `; |
| 116 | + |
| 117 | + const createAuditLogsTable = ` |
| 118 | + CREATE TABLE IF NOT EXISTS audit_logs ( |
| 119 | + id SERIAL PRIMARY KEY, |
| 120 | + event_type TEXT NOT NULL, |
| 121 | + guild_id TEXT NOT NULL REFERENCES discord(guild_id), |
| 122 | + related_id TEXT NOT NULL, |
| 123 | + note TEXT, |
| 124 | + occurred_at TIMESTAMP DEFAULT now() |
| 125 | + ); |
| 126 | + `; |
| 127 | + |
| 128 | + try { |
| 129 | + await pool.query(createDiscordTable); |
| 130 | + console.log("Discord table created"); |
| 131 | + |
| 132 | + await pool.query(createBlueskyTable); |
| 133 | + console.log("Bluesky table created"); |
| 134 | + |
| 135 | + await pool.query(createYouTubeTable); |
| 136 | + console.log("YouTube table created"); |
| 137 | + |
| 138 | + await pool.query(createTwitchTable); |
| 139 | + console.log("Twitch table created"); |
| 140 | + |
| 141 | + await pool.query(createGuildBlueskySubscriptionsTable); |
| 142 | + console.log("Guild Bluesky Subscriptions table created"); |
| 143 | + |
| 144 | + await pool.query(createGuildYouTubeSubscriptionsTable); |
| 145 | + console.log("Guild YouTube Subscriptions table created"); |
| 146 | + |
| 147 | + await pool.query(createGuildTwitchSubscriptionsTable); |
| 148 | + console.log("Guild Twitch Subscriptions table created"); |
| 149 | + |
| 150 | + await pool.query(createBotInfoTable); |
| 151 | + console.log("Bot Info table created"); |
| 152 | + |
| 153 | + await pool.query(createBotInfoNotificationsTable); |
| 154 | + console.log("Bot Info Notifications table created"); |
| 155 | + |
| 156 | + await pool.query(createBotInfoNotificationsTimingsTable); |
| 157 | + console.log("Bot Info Notifications Timings table created"); |
| 158 | + |
| 159 | + await pool.query(createBotInfoTopChannelsTable); |
| 160 | + console.log("Bot Info Top Channels table created"); |
| 161 | + |
| 162 | + await pool.query(createBotInfoTopGuildsTable); |
| 163 | + console.log("Bot Info Top Guilds table created"); |
| 164 | + |
| 165 | + await pool.query(createAuditLogsTable); |
| 166 | + console.log("Audit Logs table created"); |
| 167 | + |
| 168 | + return true; |
| 169 | + } catch (err) { |
| 170 | + console.error("Error creating tables:", err); |
| 171 | + |
| 172 | + return false; |
| 173 | + } |
| 174 | +} |
0 commit comments