Added Drizzle ORM for Database Connection and Basic Member Command

This commit is contained in:
Ahmad 2024-11-23 15:14:21 -05:00
parent f1e6e05345
commit 9030fbcdcb
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
11 changed files with 1955 additions and 154 deletions

24
src/commands/members.ts Normal file
View file

@ -0,0 +1,24 @@
import { SlashCommandBuilder, CommandInteraction, EmbedBuilder } from "discord.js";
import { getAllMembers } from "../util/db.js";
interface Command {
data: Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
execute: (interaction: CommandInteraction) => Promise<void>;
}
const command: Command = {
data: new SlashCommandBuilder()
.setName("members")
.setDescription("Lists all non-bot members of the server"),
execute: async (interaction) => {
const members = await getAllMembers();
const memberList = members.map(m => `**${m.discordUsername}** (${m.discordId})`).join("\n");
const membersEmbed = new EmbedBuilder()
.setTitle("Members")
.setDescription(memberList)
.setColor(0x0099ff);
await interaction.reply({ embeds: [membersEmbed] });
},
};
export default command;

7
src/db/schema.ts Normal file
View file

@ -0,0 +1,7 @@
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
export const memberTable = pgTable("members", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
discordId: varchar("discord_id").notNull().unique(),
discordUsername: varchar("discord_username").notNull(),
});

View file

@ -1,12 +1,16 @@
import fs from "node:fs";
import path from "node:path";
import { Client, Collection, Events, GatewayIntentBits } from "discord.js";
import { deployCommands } from "./util/deployCommand.js";
import { getAllMembers, setMembers } from "./util/db.js";
const config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
const { token } = config;
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
const { token, guildId } = config;
const client: any = new Client({ intents: [GatewayIntentBits.Guilds] });
const client: any = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});
client.commands = new Collection();
try {
@ -44,7 +48,13 @@ try {
console.log(`Error while registering commands: ${error}`);
}
client.once(Events.ClientReady, (c: any) => {
client.once(Events.ClientReady, async (c: any) => {
const guild = await client.guilds.fetch(guildId);
const members = await guild.members.fetch();
const nonBotMembers = members.filter((member: any) => !member.user.bot);
await setMembers(nonBotMembers);
console.log(`Ready! Logged in as ${c.user.tag}`);
});

40
src/util/db.ts Normal file
View 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);
}
});
}