too many things

This commit is contained in:
nin0 2025-05-15 07:05:16 -04:00
parent 3b892b3db0
commit b5dd0ed413
Signed by: nin0
SSH key fingerprint: SHA256:NOoDnFVvZNFvqfXCIhzr6oCTDImZAbTTuyAysZ8Ufk8
9 changed files with 1187 additions and 3 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
.env
dist/
node_modules/
node_modules/
database.db

20
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,20 @@
{
"name": "Attach to process",
"type": "node",
"request": "attach",
"port": 9229,
"skipFiles": ["<node_internals>/**", "${workspaceFolder}/node_modules/**"],
"configurations": [
{
"name": "Attach to process",
"type": "node",
"request": "attach",
"port": 9229,
"skipFiles": [
"<node_internals>/**",
"${workspaceFolder}/node_modules/**"
],
"configurations": []
}
]
}

View file

@ -4,14 +4,16 @@
"dependencies": {
"esbuild": "^0.25.3",
"fastify": "^5.3.2",
"oceanic.js": "^1.12.0"
"oceanic.js": "^1.12.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.7"
},
"devDependencies": {
"@types/node": "^22.15.2",
"tsx": "^4.19.3"
},
"scripts": {
"dev": "tsx --watch --env-file-if-exists=.env src/index.ts",
"dev": "tsx --watch --inspect-brk --env-file-if-exists=.env src/index.ts",
"build": "node build.mjs",
"start": "node build.mjs && node dist/index.js"
}

1039
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -1,2 +1,3 @@
onlyBuiltDependencies:
- esbuild
- sqlite3

4
src/Constants.ts Normal file
View file

@ -0,0 +1,4 @@
export const Constants = {
PENDING_CHANNEL_ID: "1370539719842070683",
REJECTION_CHANNEL_ID: "1371073658403164261"
};

26
src/database.ts Normal file
View file

@ -0,0 +1,26 @@
import sqlite3 from "sqlite3";
import { Database, open } from "sqlite";
export let db: Database;
export async function openDb() {
db = await open({
filename: "database.db",
driver: sqlite3.Database
});
console.log("Opened database");
db.exec(
`CREATE TABLE IF NOT EXISTS applications (
id TEXT PRIMARY KEY,
user_id TEXT,
status INTEGER,
responses TEXT,
actioned_by TEXT,
reject_reason TEXT,
interview_invite TEXT,
message_id TEXT,
channel_id TEXT,
thread_id TEXT
)`
);
}

View file

@ -3,6 +3,7 @@ import { selfappReq } from "./selfappReq";
import { setupJoinRequestHandler } from "./joinRequestHandler";
import { Constants } from "./Constants";
import { Divider, generateList, generateUserComponent, Header } from "./cv2";
import { openDb } from "./database";
export const client = new Client({
auth: `Bot ${process.env.BOT_TOKEN}`,
@ -26,4 +27,5 @@ process.on("uncaughtException", (e) => {
console.error(e);
});
openDb();
client.connect();

89
src/joinRequestHandler.ts Normal file
View file

@ -0,0 +1,89 @@
import { ComponentTypes, MessageFlags, Shard } from "oceanic.js";
import { client } from ".";
import { Constants } from "./Constants";
import { Header, Divider, generateUserComponent } from "./cv2";
import { db } from "./database";
import { selfappReq } from "./selfappReq";
export async function setupJoinRequestHandler(shard: Shard) {
shard.ws?.on("message", async (d) => {
const data = JSON.parse(d.toString("utf8"));
if (!data.t) return;
if (data.t.startsWith("GUILD_JOIN_REQUEST")) {
const applicationData = data.d;
switch (data.t) {
case "GUILD_JOIN_REQUEST_UPDATE": {
switch (applicationData.status) {
case "SUBMITTED": {
const pendingMsg =
await client.rest.channels.createMessage(
Constants.PENDING_CHANNEL_ID,
{
content: "Placeholder"
}
);
await db.run(
"INSERT INTO applications (id, user_id, status, message_id, channel_id) VALUES (?, ?, 0, ?, ?)",
applicationData.request.id,
applicationData.request.user_id,
pendingMsg.id,
pendingMsg.channelID
);
break;
}
}
break;
}
case "GUILD_JOIN_REQUEST_DELETE": {
const application = await db.get(
"SELECT * FROM applications WHERE id=?",
applicationData.id
);
if (application) {
await db.run(
"DELETE FROM applications WHERE id=?",
applicationData.id
);
await client.rest.channels.deleteMessage(
application.channel_id,
application.message_id
);
}
await client.rest.channels.createMessage(
Constants.REJECTION_CHANNEL_ID,
{
flags: MessageFlags.IS_COMPONENTS_V2,
components: [
{
type: ComponentTypes.CONTAINER,
accentColor: 0xffaaaa,
components: [
Header("Join request withdrawn", 2),
Divider,
await generateUserComponent(
applicationData.user_id
),
Divider,
Header("Application", 3),
application
? {
type: ComponentTypes.TEXT_DISPLAY,
content: "User has applied"
}
: {
type: ComponentTypes.TEXT_DISPLAY,
content:
"User has never applied"
}
]
}
]
}
);
break;
}
}
}
});
}