From eb947cddcfa6dca1f25f4e5f10b48c143fa635eb Mon Sep 17 00:00:00 2001 From: moe-music Date: Sat, 26 Aug 2023 19:06:28 +0530 Subject: [PATCH] new --- package-lock.json | 48 +++++++------- package.json | 12 ++-- prisma/schema.prisma | 13 ---- src/commands/config/247.ts | 10 +-- src/commands/config/Dj.ts | 39 ++--------- src/commands/config/Prefix.ts | 35 +++------- src/database/server.ts | 121 ++++++++++++++++++++++++++++++++++ 7 files changed, 165 insertions(+), 113 deletions(-) create mode 100644 src/database/server.ts diff --git a/package-lock.json b/package-lock.json index 04cdcea5e..3c4de1143 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,18 +9,18 @@ "version": "3.2.0", "license": "GPL-3.0", "dependencies": { - "@prisma/client": "^5.1.1", + "@prisma/client": "^5.2.0", "discord.js": "^14.13.0", "dotenv": "^16.3.1", "shoukaku": "^3.4.0", "signale": "^1.4.0" }, "devDependencies": { - "@types/node": "^20.5.1", + "@types/node": "^20.5.6", "@types/signale": "^1.4.4", - "prisma": "^5.1.1", + "prisma": "^5.2.0", "ts-node": "^10.9.1", - "typescript": "^5.1.6" + "typescript": "^5.2.2" } }, "node_modules/@cspotcode/source-map-support": { @@ -174,12 +174,12 @@ } }, "node_modules/@prisma/client": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.1.1.tgz", - "integrity": "sha512-fxcCeK5pMQGcgCqCrWsi+I2rpIbk0rAhdrN+ke7f34tIrgPwA68ensrpin+9+fZvuV2OtzHmuipwduSY6HswdA==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.2.0.tgz", + "integrity": "sha512-AiTjJwR4J5Rh6Z/9ZKrBBLel3/5DzUNntMohOy7yObVnVoTNVFi2kvpLZlFuKO50d7yDspOtW6XBpiAd0BVXbQ==", "hasInstallScript": true, "dependencies": { - "@prisma/engines-version": "5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e" + "@prisma/engines-version": "5.2.0-25.2804dc98259d2ea960602aca6b8e7fdc03c1758f" }, "engines": { "node": ">=16.13" @@ -194,16 +194,16 @@ } }, "node_modules/@prisma/engines": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.1.1.tgz", - "integrity": "sha512-NV/4nVNWFZSJCCIA3HIFJbbDKO/NARc9ej0tX5S9k2EVbkrFJC4Xt9b0u4rNZWL4V+F5LAjvta8vzEUw0rw+HA==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.2.0.tgz", + "integrity": "sha512-dT7FOLUCdZmq+AunLqB1Iz+ZH/IIS1Fz2THmKZQ6aFONrQD/BQ5ecJ7g2wGS2OgyUFf4OaLam6/bxmgdOBDqig==", "devOptional": true, "hasInstallScript": true }, "node_modules/@prisma/engines-version": { - "version": "5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e.tgz", - "integrity": "sha512-owZqbY/wucbr65bXJ/ljrHPgQU5xXTSkmcE/JcbqE1kusuAXV/TLN3/exmz21SZ5rJ7WDkyk70J2G/n68iogbQ==" + "version": "5.2.0-25.2804dc98259d2ea960602aca6b8e7fdc03c1758f", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.2.0-25.2804dc98259d2ea960602aca6b8e7fdc03c1758f.tgz", + "integrity": "sha512-jsnKT5JIDIE01lAeCj2ghY9IwxkedhKNvxQeoyLs6dr4ZXynetD0vTy7u6wMJt8vVPv8I5DPy/I4CFaoXAgbtg==" }, "node_modules/@sapphire/async-queue": { "version": "1.5.0", @@ -261,9 +261,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.5.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz", - "integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==" + "version": "20.5.6", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.6.tgz", + "integrity": "sha512-Gi5wRGPbbyOTX+4Y2iULQ27oUPrefaB0PxGQJnfyWN3kvEDGM3mIB5M/gQLmitZf7A9FmLeaqxD3L1CXpm3VKQ==" }, "node_modules/@types/signale": { "version": "1.4.4", @@ -635,13 +635,13 @@ } }, "node_modules/prisma": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.1.1.tgz", - "integrity": "sha512-WJFG/U7sMmcc6TjJTTifTfpI6Wjoh55xl4AzopVwAdyK68L9/ogNo8QQ2cxuUjJf/Wa82z/uhyh3wMzvRIBphg==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.2.0.tgz", + "integrity": "sha512-FfFlpjVCkZwrqxDnP4smlNYSH1so+CbfjgdpioFzGGqlQAEm6VHAYSzV7jJgC3ebtY9dNOhDMS2+4/1DDSM7bQ==", "devOptional": true, "hasInstallScript": true, "dependencies": { - "@prisma/engines": "5.1.1" + "@prisma/engines": "5.2.0" }, "bin": { "prisma": "build/index.js" @@ -762,9 +762,9 @@ "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig==" }, "node_modules/typescript": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", - "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", "dev": true, "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index 69517a0d1..6071e175d 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "lavamusic", - "version": "3.2.0", + "version": "3.3.0", "description": "LavaMusic is a music bot for Discord, written in JavaScript using the Discord.js, Typescript, Shoukaku (Lavalink) library.", "type": "module", "main": "dist/index.js", "scripts": { - "start": "npm run prisma:generate && npm run start:bot", + "start": "npm run start:bot", "prisma:generate": "npx prisma generate", "start:bot": "npm run build && node --enable-source-maps dist/index.js", "build": "tsc --project tsconfig.json" @@ -31,14 +31,14 @@ }, "homepage": "https://github.com/brblacky/lavamusic#readme", "devDependencies": { - "@types/node": "^20.5.1", + "@types/node": "^20.5.6", "@types/signale": "^1.4.4", - "prisma": "^5.1.1", + "prisma": "^5.2.0", "ts-node": "^10.9.1", - "typescript": "^5.1.6" + "typescript": "^5.2.2" }, "dependencies": { - "@prisma/client": "^5.1.1", + "@prisma/client": "^5.2.0", "discord.js": "^14.13.0", "dotenv": "^16.3.1", "shoukaku": "^3.4.0", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 22a6598cc..44cfcebb8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -20,7 +20,6 @@ model Guild { dj Dj? botchannel Botchannel? premium Premium? - tracks Tracks[] } model Setup { @@ -96,15 +95,3 @@ model Premium { user User? @relation(fields: [userId], references: [userId]) guild Guild? @relation(fields: [guildId], references: [guildId]) } - -model Tracks { - id String @id @default(auto()) @map("_id") @db.ObjectId - trackId String @unique - title String - url String - duration String - thumbnail String - played Int @default(0) - guildId String - guild Guild? @relation(fields: [guildId], references: [guildId]) -} \ No newline at end of file diff --git a/src/commands/config/247.ts b/src/commands/config/247.ts index c87688a6f..5a3ffe8a1 100644 --- a/src/commands/config/247.ts +++ b/src/commands/config/247.ts @@ -1,4 +1,5 @@ import { Command, Lavamusic, Context } from '../../structures/index.js'; +import ServerData from '../../database/server.js'; export default class _247 extends Command { constructor(client: Lavamusic) { @@ -39,13 +40,7 @@ export default class _247 extends Command { }); const vc = ctx.member as any; if (!data) { - await client.prisma.stay.create({ - data: { - guildId: ctx.guild.id, - textId: ctx.channel.id, - voiceId: vc.voice.channelId, - }, - }); + await ServerData.set_247(ctx.guild.id, ctx.channel.id, vc.voice.channelId); if (!player) player = await client.queue.create(ctx.guild, vc.voice.channel, ctx.channel, client.shoukaku.getNode()); return ctx.sendMessage({ @@ -57,7 +52,6 @@ export default class _247 extends Command { guildId: ctx.guild.id, }, }); - return ctx.sendMessage({ embeds: [embed.setDescription(`**247 mode has been disabled**`).setColor(client.color.red)], }); diff --git a/src/commands/config/Dj.ts b/src/commands/config/Dj.ts index 762136942..7a0405771 100644 --- a/src/commands/config/Dj.ts +++ b/src/commands/config/Dj.ts @@ -1,5 +1,6 @@ import { Command, Lavamusic, Context } from '../../structures/index.js'; import { ApplicationCommandOptionType } from 'discord.js'; +import ServerData from '../../database/server.js'; export default class Dj extends Command { constructor(client: Lavamusic) { @@ -96,34 +97,12 @@ export default class Dj extends Command { }); if (!dj) { - await this.client.prisma.dj.create({ - data: { - guildId: ctx.guild.id, - roles: { - create: { - roleId: role.id, - }, - }, - mode: true, - }, - }); + await ServerData.setDj(ctx.guild.id, role.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`The dj role <@&${role.id}> has been added`)], }); } else { - await this.client.prisma.dj.update({ - where: { - guildId: ctx.guild.id, - }, - data: { - mode: true, - roles: { - create: { - roleId: role.id, - }, - }, - }, - }); + await ServerData.setDj(ctx.guild.id, role.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`The dj role <@&${role.id}> has been added`)], }); @@ -149,17 +128,7 @@ export default class Dj extends Command { return await ctx.sendMessage({ embeds: [embed.setDescription('There are no dj roles to clear')], }); - await this.client.prisma.roles.deleteMany({ - where: { guildId: ctx.guild.id }, - }); - await this.client.prisma.dj.update({ - where: { - guildId: ctx.guild.id, - }, - data: { - mode: false, - }, - }); + await ServerData.setDj(ctx.guild.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`All dj roles have been removed`)], }); diff --git a/src/commands/config/Prefix.ts b/src/commands/config/Prefix.ts index b75749e67..1146c464e 100644 --- a/src/commands/config/Prefix.ts +++ b/src/commands/config/Prefix.ts @@ -1,4 +1,5 @@ import { Command, Lavamusic, Context } from '../../structures/index.js'; +import ServerData from '../../database/server.js'; export default class Prefix extends Command { constructor(client: Lavamusic) { @@ -49,11 +50,7 @@ export default class Prefix extends Command { } public async run(client: Lavamusic, ctx: Context, args: string[]): Promise { const embed = client.embed().setColor(client.color.main); - let prefix = (await this.client.prisma.guild.findUnique({ - where: { - guildId: ctx.guild.id, - }, - })) as any; + let prefix = await ServerData.get(ctx.guild.id); let subCommand: string; let pre: string; @@ -76,24 +73,14 @@ export default class Prefix extends Command { }); if (!prefix) { - prefix = await this.client.prisma.guild.create({ - data: { - guildId: ctx.guild.id, - prefix: pre, - }, - }); + await ServerData.setPrefix(ctx.guild.id, pre); + prefix = await ServerData.get(ctx.guild.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`The prefix for this server is now \`${prefix.prefix}\``)], }); } else { - prefix = await this.client.prisma.guild.update({ - where: { - guildId: ctx.guild.id, - }, - data: { - prefix: pre, - }, - }); + await ServerData.setPrefix(ctx.guild.id, pre); + prefix = await ServerData.get(ctx.guild.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`The prefix for this server is now \`${prefix.prefix}\``)], }); @@ -103,14 +90,8 @@ export default class Prefix extends Command { return await ctx.sendMessage({ embeds: [embed.setDescription(`The prefix for this server is \`${client.config.prefix}\``)], }); - prefix = await this.client.prisma.guild.update({ - where: { - guildId: ctx.guild.id, - }, - data: { - prefix: client.config.prefix, - }, - }); + await ServerData.setPrefix(ctx.guild.id, client.config.prefix); + prefix = await ServerData.get(ctx.guild.id); return await ctx.sendMessage({ embeds: [embed.setDescription(`The prefix for this server is now \`${client.config.prefix}\``)], }); diff --git a/src/database/server.ts b/src/database/server.ts new file mode 100644 index 000000000..94b87f8aa --- /dev/null +++ b/src/database/server.ts @@ -0,0 +1,121 @@ +import { Guild, PrismaClient } from '@prisma/client'; + + +export default class ServerData { + static prisma = new PrismaClient(); + static async get(guildId: string): Promise { + let data = await this.prisma.guild.findFirst({ + where: { + guildId: guildId + } + }); + if (!data) { + data = await this.prisma.guild.create({ + data: { + guildId: guildId + } + }); + } + return data; + } + + static async getPrefix(guildId: string): Promise { + let data = await this.get(guildId); + return data.prefix; + } + + static async setPrefix(guildId: string, prefix: string): Promise { + let data = await this.get(guildId); + if (!data) { + await this.prisma.guild.create({ + data: { + guildId: guildId, + prefix: prefix + } + }); + } else { + await this.prisma.guild.update({ + where: { + guildId: guildId + }, + data: { + prefix: prefix + } + }); + } + } + + static async set_247(guildId: string, textId: string, voiceId: string): Promise { + let data = await this.prisma.stay.findFirst({ + where: { + guildId: guildId + } + }); + if (!data) { + await this.prisma.stay.create({ + data: { + guildId: guildId, + textId: textId, + voiceId: voiceId + } + }); + } else { + await this.prisma.stay.update({ + where: { + guildId: guildId + }, + data: { + textId: textId, + voiceId: voiceId + } + }); + } + } + + static async setDj(guildId: string, roleId?: string): Promise { + let data = await this.prisma.dj.findFirst({ + where: { + guildId: guildId + } + }); + if (!data && roleId) { + await this.prisma.dj.create({ + data: { + guildId: guildId, + mode: true, + roles: { + create: { + roleId: roleId + } + } + } + }); + } else if (data && roleId) { + await this.prisma.dj.update({ + where: { + guildId: guildId + }, + data: { + mode: true, + roles: { + create: { + roleId: roleId + } + } + } + }); + } else if (data && !roleId) { + await this.prisma.roles.deleteMany({ + where: { guildId: guildId } + }); + await this.prisma.dj.update({ + where: { + guildId: guildId + }, + data: { + mode: false + } + }); + } + } +} \ No newline at end of file