Skip to content

Commit 030d064

Browse files
committed
fix(schema.prisma): refactor schema to improve readability and remove unused models
feat(schema.prisma): add new models for Settings, Notes, and Reminders to support new features fix(main.py): move database connection after starting coroutines to ensure they are started before connecting to the database feat(main.py): add log message for closing database connection to improve debugging
1 parent 8a4fd58 commit 030d064

File tree

2 files changed

+65
-154
lines changed

2 files changed

+65
-154
lines changed

prisma/schema.prisma

+61-151
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// // datasource db {
2-
// // provider = "postgresql"
3-
// // url = env("DATABASE_URL")
4-
// // }
1+
// datasource db {
2+
// provider = "postgresql"
3+
// url = env("DATABASE_URL")
4+
// directUrl = env("DIRECT_URL")
5+
// }
56

67
datasource db {
78
// SQLite provider
@@ -19,6 +20,16 @@ generator client {
1920
recursive_type_depth = -1
2021
}
2122

23+
// General settings for the system
24+
model Settings {
25+
// Key of the setting
26+
key String @id
27+
// Value of the setting
28+
value String
29+
// Optional description of what the setting does
30+
description String?
31+
}
32+
2233
model Users {
2334
// The user’s unique ID (via Discord)
2435
id BigInt @id
@@ -51,23 +62,32 @@ model Users {
5162
infractions_received Infractions[] @relation("User")
5263
5364
snippets Snippets[]
65+
66+
afk Boolean @default(false)
67+
afk_reason String?
68+
afk_since DateTime?
69+
70+
notes_given Notes[] @relation("Moderator")
71+
notes_received Notes[] @relation("User")
72+
73+
Reminders Reminders[]
5474
}
5575

5676
model Roles {
5777
// The ID for the role (via Discord)
58-
id BigInt @id
78+
id BigInt @id
5979
// The name of the role
6080
name String
6181
// Indicates if the role will be displayed separately from other members.
62-
hoist Boolean
82+
hoist Boolean @default(false)
6383
// Indicates if the role is managed by the guild through some form of integrations such as Twitch.
64-
managed Boolean @default(false)
84+
managed Boolean @default(false)
6585
// Indicates if the role is mentionable.
66-
mentionable Boolean @default(false)
86+
mentionable Boolean @default(false)
6787
// The role’s creation time in UTC.
68-
created_at DateTime
88+
created_at DateTime?
6989
// Returns a string that allows you to mention a role.
70-
mention String @default("")
90+
mention String? @default("")
7191
7292
// This field links a role to the users that have it. It references the `UserRoles` junction table. If you fetch a role from the database and include this field, you will get a list of UserRoles entries and from there you can find all the users that have this role.
7393
users UserRoles[]
@@ -106,8 +126,8 @@ model Infractions {
106126
expires_at DateTime?
107127
108128
// These fields establish a relationship with the `Users` model. `moderator_id` is the ID of the user who gave the infraction. The line `moderator Users? @relation("Moderator", fields: [moderator_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the moderator) is associated with this infraction.
109-
moderator Users? @relation("Moderator", fields: [moderator_id], references: [id])
110-
moderator_id BigInt?
129+
moderator Users @relation("Moderator", fields: [moderator_id], references: [id])
130+
moderator_id BigInt
111131
112132
// These fields establish another relationship with the `Users` model. `user_id` is the ID of the user who received the infraction. The line `user Users @relation("User", fields: [user_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the user who received the infraction) is associated with this infraction.
113133
user Users @relation("User", fields: [user_id], references: [id])
@@ -121,150 +141,40 @@ model Snippets {
121141
content String
122142
// The creation time of the snippet
123143
created_at DateTime @default(now())
124-
author Users @relation(fields: [author_id], references: [id])
125-
author_id BigInt
126-
}
127-
128-
// Old stuff
129-
130-
// // Table representing users in the system
131-
// model users {
132-
// // user identifier (Discord ID)
133-
// user_id BigInt @id
134-
// // Indicator if the user is a bot
135-
// user_is_bot Boolean?
136-
// // Optional user's username
137-
// user_username String?
138-
// // Optional user's global name
139-
// user_global_name String?
140-
// // Timestamp when user was created
141-
// user_created_at DateTime
142-
// // Link to roles associated with this user
143-
// user_roles user_roles[]
144-
// // Link to notes associated with this user
145-
// notes notes[]
146-
// // Link to infractions associated with this user
147-
// infractions infractions[]
148-
// //
149-
// }
150-
151-
// // Table representing roles in the system
152-
// model roles {
153-
// // Role identifier
154-
// role_id BigInt @id
155-
// // Name of the role
156-
// role_name String
157-
// // Link to users associated with this role
158-
// user_roles user_roles[]
159-
// // Link to moderators associated with this role
160-
// moderators moderators[]
161-
// }
162-
163-
// // General settings for the system
164-
// model settings {
165-
// // Key of the setting
166-
// setting_key String @id
167-
// // Value of the setting
168-
// setting_value String
169-
// // Optional description of what the setting does
170-
// setting_description String?
171-
// }
172-
173-
// // Table representing moderators in the system
174-
// model moderators {
175-
// // Moderator identifier (Discord ID)
176-
// moderator_id BigInt @id
177-
// // Associated role identifier
178-
// role_id BigInt?
179-
// // Link to role associated with this moderator
180-
// role roles? @relation(fields: [role_id], references: [role_id])
181-
// // Link to notes created by this moderator
182-
// notes notes[]
183-
// // Link to infractions created by this moderator
184-
// infractions infractions[]
185-
// }
186144
187-
// // Table for storing logs
188-
// model logs {
189-
// // Unique log identifier
190-
// log_id String @id @default(uuid())
191-
// // Timestamp when log was created
192-
// log_created_at DateTime @default(now())
193-
// // Level of the log
194-
// log_level String?
195-
// // Content/text of the log
196-
// log_content String?
197-
// }
145+
// This field establishes a relationship with the `Users` model. `author_id` is the ID of the user who created the snippet. The line `author Users @relation(fields: [author_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the author) is associated with this snippet.
146+
author Users @relation(fields: [author_id], references: [id])
147+
author_id BigInt
148+
}
198149

199-
// // Relationship table between a user and their roles
200-
// model user_roles {
201-
// // User identifier
202-
// user_id BigInt
203-
// // Role identifier
204-
// role_id BigInt
205-
// // Link to the user
206-
// users users @relation(fields: [user_id], references: [user_id])
207-
// // Link to the role
208-
// roles roles @relation(fields: [role_id], references: [role_id])
150+
model Notes {
151+
id BigInt @id
152+
content String
153+
created_at DateTime @default(now())
209154
210-
// // Composite primary key consisting of user_id and role_id
211-
// @@id([user_id, role_id])
212-
// }
155+
// These fields establish a relationship with the `Users` model. `moderator_id` is the ID of the user who created the note. The line `moderator Users? @relation("Moderator", fields: [moderator_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the moderator) is associated with this note.
156+
moderator Users @relation("Moderator", fields: [moderator_id], references: [id])
157+
moderator_id BigInt
213158
214-
// // Table for storing notes/moderator reports on users
215-
// model notes {
216-
// // Unique identifier for the note
217-
// note_id String @id @default(uuid())
218-
// // Content of the note
219-
// note_content String
220-
// // Moderator who created the note
221-
// moderator_id BigInt?
222-
// // User who the note is about
223-
// user_id BigInt?
224-
// // When the note was created
225-
// note_created_at DateTime @default(now())
226-
// // Link to the moderator
227-
// moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
228-
// // Link to the user
229-
// user users? @relation(fields: [user_id], references: [user_id])
230-
// }
159+
// These fields establish another relationship with the `Users` model. `user_id` is the ID of the user who the note is about. The line `user Users @relation("User", fields: [user_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the user who the note is about) is associated with this note.
160+
user Users @relation("User", fields: [user_id], references: [id])
161+
user_id BigInt
162+
}
231163

232-
// // Table for storing one-to-many snippets of text
233-
// model snippets {
234-
// // Name of the snippet
235-
// snippet_name String @id
236-
// // Content of the snippet
237-
// snippet_content String
238-
// }
164+
model Reminders {
165+
id BigInt @id
166+
content String
167+
created_at DateTime @default(now())
168+
expires_at DateTime
239169
240-
// // Table representing guilds/servers in the system
241-
// model guilds {
242-
// // Unique identifier for the guild (Discord ID)
243-
// guild_id BigInt @id
244-
// // Name of the guild
245-
// guild_name String
246-
// // ID of the owner of the guild
247-
// guild_owner_id BigInt
248-
// }
170+
// These fields establish a relationship with the `Users` model. `author_id` is the ID of the user who created the reminder. The line `author Users @relation(fields: [author_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the author) is associated with this reminder.
171+
author Users @relation(fields: [author_id], references: [id])
172+
author_id BigInt
173+
}
249174

250-
// // Table representing infractions/punishments on users
251-
// model infractions {
252-
// // Unique identifier for the infraction
253-
// infraction_id String @id @default(uuid())
254-
// // ID of the moderator who gave the infraction
255-
// moderator_id BigInt?
256-
// // ID of the user who received the infraction
257-
// user_id BigInt?
258-
// // Type of the infraction (ban, mute, etc)
259-
// infraction_type String
260-
// // Optional reason for the infraction
261-
// infraction_reason String?
262-
// // When the infraction was given
263-
// infraction_created_at DateTime @default(now())
264-
// // When the infraction expires, if applicable
265-
// infraction_expires_at DateTime?
266-
// // Link to the moderator who gave the infraction
267-
// moderator moderators? @relation(fields: [moderator_id], references: [moderator_id])
268-
// // Link to the user who received the infraction
269-
// user users? @relation(fields: [user_id], references: [user_id])
175+
// model Logs {
176+
// id BigInt @id
177+
// content String
178+
// log_type String
179+
// created_at DateTime @default(now())
270180
// }

tux/main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,20 @@ async def on_ready(self) -> None:
195195

196196
logger.info(f"{self.user} has connected to Discord!")
197197

198-
await db.connect()
199-
200198
# start the change_activity coroutine
201199
asyncio.create_task(self.change_activity()) # noqa: RUF006
202200

203201
# start console coroutine
204202
asyncio.create_task(self.console()) # noqa: RUF006
205203

204+
# connect to the database
205+
await db.connect()
206+
206207
@commands.Cog.listener()
207208
async def on_disconnect(self) -> None:
208209
logger.warning("Bot has disconnected from Discord.")
209-
210210
await db.disconnect()
211+
logger.warning("Database connection closed.")
211212

212213

213214
async def main() -> None:

0 commit comments

Comments
 (0)