diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6ea936c86..ad4099269 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -5,67 +5,72 @@ // Try Prisma Accelerate: https://pris.ly/cli/accelerate-init generator client { - provider = "prisma-client-js" - binaryTargets = ["native", "debian-openssl-3.0.x"] + provider = "prisma-client-js" + binaryTargets = ["native", "debian-openssl-3.0.x"] } datasource db { - provider = "sqlite" - url = "file:./lavamusic.db" + provider = "sqlite" + url = "file:./lavamusic.db" +} + +model Bot { + botId String @unique + totalPlaySong Int } model Guild { - guildId String @id - prefix String - language String? - stay Stay? - dj Dj? - roles Role[] - setup Setup? + guildId String @id + prefix String + language String? @default("EnglishUS") + stay Stay? + dj Dj? + roles Role[] + setup Setup? } model Stay { - guildId String @id - textId String - voiceId String - Guild Guild @relation(fields: [guildId], references: [guildId]) + guildId String @id + textId String + voiceId String + Guild Guild @relation(fields: [guildId], references: [guildId]) } model Dj { - guildId String @id - mode Boolean - Guild Guild @relation(fields: [guildId], references: [guildId]) + guildId String @id + mode Boolean + Guild Guild @relation(fields: [guildId], references: [guildId]) } model Role { - guildId String - roleId String - Guild Guild @relation(fields: [guildId], references: [guildId]) + guildId String + roleId String + Guild Guild @relation(fields: [guildId], references: [guildId]) - @@unique([guildId, roleId]) + @@unique([guildId, roleId]) } model Playlist { - id String @id @default(uuid()) - userId String - name String - songs Song[] + id String @id @default(uuid()) + userId String + name String + songs Song[] - @@unique([userId, name]) + @@unique([userId, name]) } model Song { - id String @id @default(uuid()) - track String - playlistId String - playlist Playlist @relation(fields: [playlistId], references: [id]) + id String @id @default(uuid()) + track String + playlistId String + playlist Playlist @relation(fields: [playlistId], references: [id]) - @@unique([track, playlistId]) + @@unique([track, playlistId]) } model Setup { - guildId String @id - textId String - messageId String - Guild Guild @relation(fields: [guildId], references: [guildId]) -} + guildId String @id + textId String + messageId String + Guild Guild @relation(fields: [guildId], references: [guildId]) +} \ No newline at end of file diff --git a/src/database/server.ts b/src/database/server.ts index 2850aa980..642656fb7 100644 --- a/src/database/server.ts +++ b/src/database/server.ts @@ -1,4 +1,4 @@ -import { type Dj, type Guild, type Playlist, PrismaClient, type Role, type Setup, type Song, type Stay } from "@prisma/client"; +import { type Dj, type Guild, type Playlist, PrismaClient, type Role, type Song, type Setup, type Stay } from "@prisma/client"; import config from "../config.js"; export default class ServerData { @@ -9,16 +9,15 @@ export default class ServerData { } public async get(guildId: string): Promise { - return ( - (await this.prisma.guild.findUnique({ - where: { guildId }, - })) ?? this.createGuild(guildId) - ); + return (await this.prisma.guild.findUnique({ where: { guildId } })) ?? this.createGuild(guildId); } private async createGuild(guildId: string): Promise { return await this.prisma.guild.create({ - data: { guildId, prefix: config.prefix }, + data: { + guildId, + prefix: config.prefix, + }, }); } @@ -30,6 +29,39 @@ export default class ServerData { }); } + public async getPrefix(guildId: string): Promise { + const guild = await this.get(guildId); + return guild?.prefix ?? config.prefix; + } + + public async updateLanguage(guildId: string, language: string): Promise { + await this.prisma.guild.update({ + where: { guildId }, + data: { language }, + }); + } + + public async getLanguage(guildId: string): Promise { + const guild = await this.get(guildId); + return guild?.language ?? config.defaultLanguage; + } + + public async getSetup(guildId: string): Promise { + return await this.prisma.setup.findUnique({ where: { guildId } }); + } + + public async setSetup(guildId: string, textId: string, messageId: string): Promise { + await this.prisma.setup.upsert({ + where: { guildId }, + update: { textId, messageId }, + create: { guildId, textId, messageId }, + }); + } + + public async deleteSetup(guildId: string): Promise { + await this.prisma.setup.delete({ where: { guildId } }); + } + public async set_247(guildId: string, textId: string, voiceId: string): Promise { await this.prisma.stay.upsert({ where: { guildId }, @@ -42,6 +74,13 @@ export default class ServerData { await this.prisma.stay.delete({ where: { guildId } }); } + public async get_247(guildId?: string): Promise { + if (guildId) { + return await this.prisma.stay.findUnique({ where: { guildId } }); + } + return this.prisma.stay.findMany(); + } + public async setDj(guildId: string, mode: boolean): Promise { await this.prisma.dj.upsert({ where: { guildId }, @@ -50,13 +89,6 @@ export default class ServerData { }); } - public async get_247(guildId?: string): Promise { - if (guildId) { - return await this.prisma.stay.findUnique({ where: { guildId } }); - } - return this.prisma.stay.findMany(); - } - public async getDj(guildId: string): Promise { return await this.prisma.dj.findUnique({ where: { guildId } }); } @@ -77,36 +109,22 @@ export default class ServerData { await this.prisma.role.deleteMany({ where: { guildId } }); } - public async getSetup(guildId: string): Promise { - return await this.prisma.setup.findUnique({ where: { guildId } }); - } - - public async setSetup(guildId: string, textId: string, messageId: string): Promise { - await this.prisma.setup.upsert({ - where: { guildId }, - update: { textId, messageId }, - create: { guildId, textId, messageId }, - }); - } - - public async deleteSetup(guildId: string): Promise { - await this.prisma.setup.delete({ where: { guildId } }); - } - public async getPlaylist(userId: string, name: string): Promise { return await this.prisma.playlist.findUnique({ where: { userId_name: { userId, name } }, }); } - public async getUserPlaylists(userId: string) { + public async getUserPlaylists(userId: string): Promise { return await this.prisma.playlist.findMany({ - where: { - userId: userId, - }, + where: { userId }, }); } + public async createPlaylist(userId: string, name: string): Promise { + await this.prisma.playlist.create({ data: { userId, name } }); + } + public async createPlaylistWithSongs(userId: string, name: string, songs: any[]): Promise { await this.prisma.playlist.create({ data: { @@ -119,10 +137,6 @@ export default class ServerData { }); } - public async createPlaylist(userId: string, name: string): Promise { - await this.prisma.playlist.create({ data: { userId, name } }); - } - public async deletePlaylist(userId: string, name: string): Promise { await this.prisma.playlist.delete({ where: { userId_name: { userId, name } }, @@ -161,9 +175,7 @@ export default class ServerData { await this.prisma.song.deleteMany({ where: { playlistId: playlist.id, - track: { - contains: encodedSong, - }, + track: { contains: encodedSong }, }, }); } @@ -172,9 +184,7 @@ export default class ServerData { public async getSongs(userId: string, name: string): Promise { const playlist = await this.getPlaylist(userId, name); if (playlist) { - return this.prisma.song.findMany({ - where: { playlistId: playlist.id }, - }); + return this.prisma.song.findMany({ where: { playlistId: playlist.id } }); } return []; } @@ -182,9 +192,7 @@ export default class ServerData { public async clearPlaylist(userId: string, name: string): Promise { const playlist = await this.getPlaylist(userId, name); if (playlist) { - await this.prisma.song.deleteMany({ - where: { playlistId: playlist.id }, - }); + await this.prisma.song.deleteMany({ where: { playlistId: playlist.id } }); } } @@ -199,24 +207,6 @@ export default class ServerData { public async clearAllSongs(): Promise { await this.prisma.song.deleteMany(); } - - public async updateLanguage(guildId: string, language: string): Promise { - const guild = await this.get(guildId); - if (guild) { - await this.prisma.guild.update({ - where: { guildId }, - data: { language }, - }); - } else { - await this.createGuild(guildId); - await this.updateLanguage(guildId, language); - } - } - - public async getLanguage(guildId: string): Promise { - const guild = await this.get(guildId); - return guild?.language ?? config.defaultLanguage; - } } /**