Skip to content

Commit 4b19414

Browse files
committed
db: migrate bot_info to new system
1 parent 16ccb02 commit 4b19414

File tree

6 files changed

+104
-63
lines changed

6 files changed

+104
-63
lines changed

src/events/ready.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,26 @@
1-
import { ActivityType, Events, PresenceUpdateStatus } from "discord.js";
1+
import { Events } from "discord.js";
2+
import { CronJob } from "cron";
23

34
import client from "../index";
45
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
56
import { config } from "../config";
6-
import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
7-
import { updateBotInfo } from "../utils/database";
8-
9-
// update the bot's presence
10-
async function updatePresence() {
11-
if (!client?.user) return;
12-
13-
const servers = client.guilds.cache.size;
14-
const members = client.guilds.cache.reduce(
15-
(acc, guild) => acc + guild.memberCount,
16-
0,
17-
);
18-
19-
await updateBotInfo(servers, members);
20-
client.user.setPresence({
21-
activities: [
22-
{
23-
name: `Notifying ${servers.toLocaleString()} servers [${members.toLocaleString()} members]`,
24-
type: ActivityType.Custom,
25-
},
26-
],
27-
status: PresenceUpdateStatus.Online,
28-
});
29-
}
7+
// import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
8+
import { cronUpdateBotInfo } from "../utils/cronJobs";
309

3110
// Log into the bot
3211
client.once(Events.ClientReady, async (bot) => {
3312
console.log(`Ready! Logged in as ${bot.user?.tag}`);
3413

3514
// Set the bot's presence and update it every minute
36-
await updatePresence();
15+
await cronUpdateBotInfo();
3716
fetchLatestUploads();
38-
setInterval(updatePresence, 60000);
17+
18+
// Set the bot's presence and update it every minute
19+
new CronJob("0 * * * * *", async () => {
20+
await cronUpdateBotInfo();
21+
}).start();
3922
setInterval(fetchLatestUploads, config.updateIntervalYouTube as number);
23+
4024
// One at a time
4125
// checkIfStreamersAreLive();
4226
// setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);

src/types/database.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ export type dbDiscordTable = {
2121
platform_user_id: string;
2222
guild_ping_role: null | string;
2323
};
24+
25+
export interface dbBotInfo {
26+
locked_row: boolean;
27+
guilds_total: number;
28+
channels_tracked: number;
29+
total_members: number;
30+
updated_at: string;
31+
extended_info_updated_at: string;
32+
}

src/utils/cronJobs.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ActivityType, Guild, PresenceUpdateStatus } from "discord.js";
2+
3+
import client from "..";
4+
5+
import { updateBotInfo } from "./db/botinfo";
6+
7+
export async function cronUpdateBotInfo() {
8+
if (!client?.user) return;
9+
10+
const servers: number = client.guilds.cache.size;
11+
const members: number = client.guilds.cache.reduce(
12+
(acc: number, guild: Guild): number => acc + guild.memberCount,
13+
0,
14+
);
15+
16+
await updateBotInfo(servers, members);
17+
client.user.setPresence({
18+
activities: [
19+
{
20+
name: `Notifying ${servers.toLocaleString()} servers [${members.toLocaleString()} members]`,
21+
type: ActivityType.Custom,
22+
},
23+
],
24+
status: PresenceUpdateStatus.Online,
25+
});
26+
}

src/utils/database.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -305,42 +305,6 @@ export async function twitchStopGuildTrackingChannel(
305305
}
306306
// #endregion
307307

308-
// #region Bot Info
309-
export async function getBotInfo() {
310-
const query = `SELECT * FROM bot_info`;
311-
312-
try {
313-
const statement = db.prepare(query);
314-
const result = statement.get();
315-
316-
return result;
317-
} catch (err) {
318-
console.error("Error getting bot info:", err);
319-
throw err;
320-
}
321-
}
322-
323-
export async function updateBotInfo(
324-
total_servers: number,
325-
total_members: number,
326-
) {
327-
console.log("Updating bot info:", total_servers, total_members);
328-
const query = `UPDATE bot_info SET total_servers = ?, total_members = ?`;
329-
330-
try {
331-
const statement = db.prepare(query);
332-
333-
statement.run(total_servers, total_members);
334-
335-
return true;
336-
} catch (err) {
337-
console.error("Error updating bot info:", err);
338-
339-
return false;
340-
}
341-
}
342-
// #endregion
343-
344308
// #region i have no idea what im doing here
345309

346310
export async function getAllTrackedInGuild(

src/utils/db/botinfo.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { PoolClient, QueryResult } from "pg";
2+
import { pool } from "../database";
3+
import type { dbBotInfo } from "../../types/database";
4+
5+
export async function updateBotInfo(
6+
guilds_total: number = 0,
7+
channels_tracked: number = 0,
8+
total_members: number = 0,
9+
): Promise<void> {
10+
const query = `
11+
UPDATE bot_info
12+
SET guilds_total = $1, channels_tracked = $2, total_members = $3, updated_at = NOW()
13+
WHERE locked_row = true;
14+
`;
15+
16+
try {
17+
const client = await pool.connect();
18+
19+
await client.query(query, [
20+
guilds_total,
21+
channels_tracked,
22+
total_members,
23+
]);
24+
25+
client.release();
26+
27+
console.log("Bot info updated successfully");
28+
} catch (err) {
29+
console.error("Error updating bot info:", err);
30+
}
31+
}
32+
33+
export async function getBotInfo() {
34+
const query = `SELECT * FROM bot_info`;
35+
36+
try {
37+
const client: PoolClient = await pool.connect();
38+
const result: QueryResult<any> = await client.query(query);
39+
40+
client.release();
41+
42+
return result.rows[0] as dbBotInfo;
43+
} catch (err) {
44+
console.error("Error getting bot info:", err);
45+
46+
return null;
47+
}
48+
}

src/utils/db/init.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export default async function initTables(): Promise<boolean> {
7676

7777
const createBotInfoTable = `
7878
CREATE TABLE IF NOT EXISTS bot_info (
79+
locked_row BOOLEAN NOT NULL DEFAULT TRUE,
7980
guilds_total INTEGER NOT NULL DEFAULT 0,
8081
channels_tracked INTEGER NOT NULL DEFAULT 0,
8182
total_members INTEGER NOT NULL DEFAULT 0,
@@ -125,6 +126,10 @@ export default async function initTables(): Promise<boolean> {
125126
);
126127
`;
127128

129+
const seedBotInfoTable = `
130+
INSERT INTO bot_info (locked_row, guilds_total, channels_tracked, total_members) VALUES (true, 0, 0, 0)
131+
`;
132+
128133
try {
129134
const client = await pool.connect();
130135

@@ -167,6 +172,11 @@ export default async function initTables(): Promise<boolean> {
167172
await client.query(createAuditLogsTable);
168173
console.log("Audit Logs table created");
169174

175+
await client.query(seedBotInfoTable);
176+
console.log("Bot Info table seeded");
177+
178+
client.release();
179+
170180
return true;
171181
} catch (err) {
172182
console.error("Error creating tables:", err);

0 commit comments

Comments
 (0)