Skip to content

Commit d3eec88

Browse files
committed
feat: implement database setup in onReady event
1 parent 6ac2da9 commit d3eec88

File tree

6 files changed

+91
-9
lines changed

6 files changed

+91
-9
lines changed

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export default tseslint.config(
7272
'@typescript-eslint/no-floating-promises': 'off',
7373
'@typescript-eslint/no-explicit-any': 'off',
7474
'@typescript-eslint/unbound-method': 'off',
75+
'@typescript-eslint/require-await': 'off',
76+
'@typescript-eslint/no-require-imports': 'off'
7577
},
7678
}
7779
)

src/attach.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"ping": "https://i.ibb.co/2g1CTst/ping.gif",
44
"love": "https://i.ibb.co/NgYxJW1Q/love.gif",
55
"noted": "https://i.ibb.co/n2ZyjCZ/anotado.gif",
6-
"pc_destroyed": "https://i.ibb.co/BK67qMNg/pc-destroy.gif"
6+
"pcDestroyed": "https://i.ibb.co/BK67qMNg/pc-destroy.gif"
77
}
88
}

src/lib/prisma.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@ export const prisma = new PrismaClient({
1111
emit: 'stdout',
1212
level: 'error',
1313
},
14-
{
15-
emit: 'stdout',
16-
level: 'info',
17-
},
1814
{
1915
emit: 'stdout',
2016
level: 'warn',
2117
},
2218
],
23-
2419
errorFormat: 'pretty',
2520
})
2621

2722
prisma.$on('query', (event) => {
28-
logger.log('')
2923
logger.log(`'Query: ${event.query} in ${event.duration}ms'`)
3024
logger.log(`Params: ${event.params}`)
31-
logger.log('')
3225
})

src/modules/events/ready.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createEvent } from '@/structs/event'
2+
import { setupDatabase } from '@/utils/database'
3+
4+
createEvent({
5+
event: 'ready',
6+
name: 'startupDatabase',
7+
once: true,
8+
async execute(client) {
9+
await setupDatabase(client)
10+
},
11+
})

src/structs/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function loadModules(workdir: PathLike) {
118118
files.forEach((file, index) => {
119119
const shortPath = file.replace(modulesPath, '').replace(/^[\\/]/, '')
120120
try {
121-
import(file)
121+
require(file)
122122
logger.success(
123123
`[${index + 1}/${files.length}] Module loaded: ${chalk.blue(shortPath)}`,
124124
)
@@ -147,6 +147,12 @@ async function registerSlashCommands(client: Client<true>) {
147147
const guildCommands = storage.slashCommands.map(
148148
(slashCommand) => slashCommand,
149149
)
150+
151+
if (guildCommands.length === 0) {
152+
logger.warn('No slash commands to register')
153+
return
154+
}
155+
150156
await guild.commands
151157
.set(guildCommands)
152158
.then((commands) => {

src/utils/database.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { execSync } from 'node:child_process'
2+
3+
import { Client } from 'discord.js'
4+
5+
import { env } from '@/env'
6+
import { prisma } from '@/lib/prisma'
7+
8+
import { logger } from './logger'
9+
10+
async function createDatabase(client: Client<true>) {
11+
const guild = client.guilds.cache.get(env.DISCORD_GUILD_ID)
12+
if (!guild) {
13+
logger.error(
14+
`Guild with ID ${env.DISCORD_GUILD_ID} not found in cache.`,
15+
)
16+
return
17+
}
18+
19+
logger.log(`⚙️ Creating database for guild ${guild.name}`)
20+
try {
21+
await prisma.discordGuild.upsert({
22+
where: { id: guild.id },
23+
update: {
24+
name: guild.name,
25+
icon: guild.iconURL() ?? undefined,
26+
},
27+
28+
create: {
29+
id: guild.id,
30+
name: guild.name,
31+
icon: guild.iconURL() ?? undefined,
32+
env: {
33+
create: {
34+
initialMoneyValue: 301.0,
35+
},
36+
},
37+
},
38+
})
39+
40+
logger.success(`Guild successfully synced: ${guild.name}`)
41+
} catch (err) {
42+
logger.error(`Failed to sync guild ${guild.name}:`, err)
43+
}
44+
}
45+
46+
function applyMigrations() {
47+
try {
48+
execSync('npx prisma migrate deploy', { stdio: 'inherit' })
49+
logger.success('Migrations applied successfully.')
50+
} catch (err) {
51+
logger.error('Error while applying migrations:', err)
52+
}
53+
}
54+
55+
export async function setupDatabase(client: Client<true>) {
56+
const guildId = env.DISCORD_GUILD_ID
57+
58+
const exists = await prisma.discordGuild.findUnique({
59+
where: { id: guildId },
60+
})
61+
62+
if (exists) {
63+
logger.log('Guild already registered. Applying migrations...')
64+
applyMigrations()
65+
return
66+
}
67+
68+
logger.log('Guild not found in the database. Creating structure...')
69+
await createDatabase(client)
70+
}

0 commit comments

Comments
 (0)