mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-05-14 04:33:05 +00:00
Added Drizzle ORM for Database Connection and Basic Member Command
This commit is contained in:
parent
f1e6e05345
commit
9030fbcdcb
11 changed files with 1955 additions and 154 deletions
40
src/util/db.ts
Normal file
40
src/util/db.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import fs from "node:fs";
|
||||
import pkg from "pg";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { memberTable } from "../db/schema.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
const { Pool } = pkg;
|
||||
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
|
||||
const { dbConnectionString, guildId } = config;
|
||||
|
||||
const dbPool = new Pool({
|
||||
connectionString: dbConnectionString,
|
||||
ssl: true,
|
||||
});
|
||||
const db = drizzle({ client: dbPool });
|
||||
|
||||
export async function getAllMembers() {
|
||||
return await db.select().from(memberTable);
|
||||
}
|
||||
|
||||
export async function setMembers(nonBotMembers: any) {
|
||||
nonBotMembers.forEach(async (member: any) => {
|
||||
const memberExists = await db
|
||||
.select()
|
||||
.from(memberTable)
|
||||
.where(eq(memberTable.discordId, member.user.id));
|
||||
if (memberExists.length > 0) {
|
||||
await db
|
||||
.update(memberTable)
|
||||
.set({ discordUsername: member.user.username })
|
||||
.where(eq(memberTable.discordId, member.user.id));
|
||||
} else {
|
||||
const members: typeof memberTable.$inferInsert = {
|
||||
discordId: member.user.id,
|
||||
discordUsername: member.user.username,
|
||||
};
|
||||
await db.insert(memberTable).values(members);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue