Skip to content

Commit e5923ae

Browse files
committed
wip: tracking
1 parent e2ffaf4 commit e5923ae

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

api/src/db/queries/tracking.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { CronJob } from "cron";
2+
3+
let usersToUpdate: Record<string, string[]> = {};
4+
5+
export async function addUserToTrackingData(userId: string, guildId: string) : Promise<void> {
6+
console.log("Adding user to tracking data:", userId, guildId);
7+
if (!usersToUpdate[guildId]) {
8+
usersToUpdate[guildId] = [];
9+
}
10+
if (!usersToUpdate[guildId].includes(userId)) {
11+
usersToUpdate[guildId].push(userId);
12+
}
13+
return;
14+
}
15+
16+
async function doTrackingJob() {
17+
const usersToUpdateTemp = { ...usersToUpdate };
18+
usersToUpdate = {};
19+
if (!Object.keys(usersToUpdateTemp).length) {
20+
console.log("No users to update!");
21+
return;
22+
}
23+
const guildIds = Object.keys(usersToUpdateTemp);
24+
for (const guildId of guildIds) {
25+
const userIds = usersToUpdateTemp[guildId];
26+
for (const userId of userIds) {
27+
console.log(userId, guildId);
28+
}
29+
}
30+
}
31+
32+
const trackingJob = new CronJob("*/5 * * * * *", doTrackingJob);
33+
trackingJob.start();

api/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express, { type NextFunction, type Request, type Response } from "express";
22
import cors from "cors";
3-
import { getBotInfo, getGuild, getUser, getUsers, initTables, pool, updateGuild, enableUpdates, disableUpdates, setCooldown, setUpdatesChannel, setXP, setLevel, removeGuild, removeUser, getAllServersWithUpdatesEnabled } from "./db";
3+
import { getBotInfo, getGuild, getUser, getUsers, initTables, pool, updateGuild, enableUpdates, disableUpdates, setCooldown, setUpdatesChannel, setXP, setLevel, removeGuild, removeUser, getAllServersWithUpdatesEnabled, addUserToTrackingData } from "./db";
44

55
const app = express();
66
const PORT = 18103;
@@ -478,6 +478,10 @@ app.post("/admin/:action/:guild/:target", authMiddleware, async (req, res) => {
478478
return res.status(500).json({ message: "Internal server error" });
479479
}
480480
}
481+
case "tracking": {
482+
await addUserToTrackingData(target, guild);
483+
break;
484+
}
481485
default:
482486
return res.status(400).json({ message: "Illegal request" });
483487
}
@@ -739,3 +743,7 @@ async function syncFromLurkr(guild: string) {
739743
}
740744
}
741745
//#endregion
746+
747+
//#region Tracking
748+
749+
//#endregion

bot/src/events/messageCreate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Message } from 'discord.js';
22
import client from '../index';
3-
import { getCooldown, makePOSTRequest, updateGuildInfo } from '../utils/requestAPI';
3+
import { addUserToTrackingData, getCooldown, makePOSTRequest, updateGuildInfo } from '../utils/requestAPI';
44

55
const cooldowns = new Map<string, number>();
66

@@ -24,4 +24,6 @@ client.on('messageCreate', async (message: Message) => {
2424
const guildIcon = message.guild?.iconURL() ?? 'https://cdn.discordapp.com/embed/avatars/0.png';
2525
const guildMembers = message.guild?.memberCount;
2626
await updateGuildInfo(message.guildId as string, guildName as string, guildIcon as string, guildMembers as number);
27+
28+
await addUserToTrackingData(message.author.id, message.guildId as string);
2729
});

bot/src/utils/requestAPI.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ export async function getDBSize() {
110110
return response.json();
111111
}
112112

113+
export async function addUserToTrackingData(userId: string, guildId: string) {
114+
await fetch(`http://localhost:18103/admin/tracking/${guildId}/${userId}`, {
115+
"headers": {
116+
'Content-Type': 'application/json',
117+
'Authorization': process.env.AUTH as string,
118+
},
119+
"method": "POST"
120+
});
121+
}
122+
113123
//#region Roles
114124
export async function getRoles(guild: string) {
115125
const response = await fetch(`http://localhost:18103/admin/roles/${guild}/get`, {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"devDependencies": {
1717
"@eslint/js": "^9.7.0",
18-
"@types/bun": "latest",
18+
"@types/bun": "1.1.6",
1919
"@types/eslint__js": "^8.42.3",
2020
"dotenv-cli": "^7.4.2",
2121
"eslint": "^8.56.0",

0 commit comments

Comments
 (0)