too many things
This commit is contained in:
parent
3b892b3db0
commit
b5dd0ed413
9 changed files with 1187 additions and 3 deletions
4
src/Constants.ts
Normal file
4
src/Constants.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export const Constants = {
|
||||
PENDING_CHANNEL_ID: "1370539719842070683",
|
||||
REJECTION_CHANNEL_ID: "1371073658403164261"
|
||||
};
|
26
src/database.ts
Normal file
26
src/database.ts
Normal 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
|
||||
)`
|
||||
);
|
||||
}
|
|
@ -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
89
src/joinRequestHandler.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue