Skip to content

Commit 2401e15

Browse files
authored
Add database handlers (#12)
* Create database service file * Separate populating guilds from creating table handler * Add config for using database functions * Cleanup
1 parent 58a4ada commit 2401e15

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/events/ready/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { REST, Routes } from 'discord.js';
22
import { AppCommands } from '../../commands/commands';
3-
import { BOT_TOKEN, ENV, GUILD_IDS } from '../../config/environment';
3+
import { BOT_TOKEN, ENV, GUILD_IDS, USE_DATABASE } from '../../config/environment';
4+
import { createGuildTable, populateGuilds } from '../../services/database';
45
import { EventModule } from '../events';
56

67
const rest = new REST({ version: '9' }).setToken(BOT_TOKEN);
@@ -40,6 +41,10 @@ export default function ({ app, appCommands }: EventModule) {
4041
app.once('ready', async () => {
4142
try {
4243
await registerApplicationCommands(appCommands);
44+
if (USE_DATABASE) {
45+
await createGuildTable();
46+
await populateGuilds(app.guilds.cache);
47+
}
4348
console.log("I'm booting up! (◕ᴗ◕✿)");
4449
} catch (error) {
4550
console.log(error);

src/services/database.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { Collection, Guild } from 'discord.js';
2+
import { Pool, PoolClient } from 'pg';
3+
import { databaseConfig } from '../config/database';
4+
const pool: Pool = new Pool(databaseConfig);
5+
6+
type GuildRecord = {
7+
uuid: string;
8+
name: string;
9+
member_count: number;
10+
owner_id: string;
11+
};
12+
13+
export async function createGuildTable() {
14+
const client: PoolClient = await pool.connect();
15+
if (client) {
16+
try {
17+
await client.query('BEGIN');
18+
const createGuildTableQuery =
19+
'CREATE TABLE IF NOT EXISTS Guild(uuid TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL, member_count INTEGER NOT NULL, owner_id TEXT NOT NULL)';
20+
await client.query(createGuildTableQuery);
21+
await client.query('COMMIT');
22+
} catch (error) {
23+
await client.query('ROLLBACK');
24+
console.log(error);
25+
//TODO: Add error handling
26+
} finally {
27+
client.release();
28+
}
29+
}
30+
}
31+
export async function getGuilds() {
32+
const client: PoolClient = await pool.connect();
33+
if (client) {
34+
try {
35+
await client.query('BEGIN');
36+
const getAllGuildsQuery = 'SELECT * FROM Guild';
37+
const allGuilds = await client.query(getAllGuildsQuery);
38+
return allGuilds;
39+
} catch (error) {
40+
await client.query('ROLLBACK');
41+
console.log(error);
42+
//TODO: Add error handling
43+
} finally {
44+
client.release();
45+
}
46+
}
47+
}
48+
export async function populateGuilds(existingGuilds: Collection<string, Guild>) {
49+
try {
50+
const guildsInDatabase = await getGuilds();
51+
existingGuilds.forEach(async (guild: Guild) => {
52+
const isInDatabase =
53+
guildsInDatabase &&
54+
guildsInDatabase.rows.find((guildDb: GuildRecord) => guildDb.uuid === guild.id);
55+
if (!isInDatabase) {
56+
await insertNewGuild(guild);
57+
}
58+
});
59+
} catch (error) {
60+
console.log(error);
61+
//TODO: Add error handling
62+
}
63+
}
64+
export async function insertNewGuild(newGuild: Guild) {
65+
const client: PoolClient = await pool.connect();
66+
if (client) {
67+
try {
68+
await client.query('BEGIN');
69+
const insertNewGuildQuery =
70+
'INSERT INTO Guild (uuid, name, member_count, owner_id) VALUES ($1, $2, $3, $4)';
71+
await client.query(insertNewGuildQuery, [
72+
newGuild.id,
73+
newGuild.name,
74+
newGuild.memberCount,
75+
newGuild.ownerId,
76+
]);
77+
await client.query('COMMIT');
78+
} catch (error) {
79+
await client.query('ROLLBACK');
80+
console.log(error);
81+
//TODO: Add error handling
82+
} finally {
83+
client.release();
84+
}
85+
}
86+
}
87+
export async function deleteGuild(existingGuild: Guild) {
88+
const client: PoolClient = await pool.connect();
89+
if (client) {
90+
try {
91+
await client.query('BEGIN');
92+
const deleteGuildQuery = 'DELETE from Guild WHERE uuid = ($1)';
93+
await client.query(deleteGuildQuery, [existingGuild.id]);
94+
await client.query('COMMIT');
95+
} catch (error) {
96+
await client.query('ROLLBACK');
97+
console.log(error);
98+
} finally {
99+
client.release();
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)