diff --git a/prisma/migrations/20221020111948_audits_module/migration.sql b/prisma/migrations/20221020111948_audits_module/migration.sql new file mode 100644 index 0000000..46db9e6 --- /dev/null +++ b/prisma/migrations/20221020111948_audits_module/migration.sql @@ -0,0 +1,36 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Guild" ( + "id" TEXT NOT NULL, + "embedColorSuccess" TEXT NOT NULL DEFAULT '#22bb33', + "embedColorWait" TEXT NOT NULL DEFAULT '#f0ad4e', + "embedColorError" TEXT NOT NULL DEFAULT '#bb2124', + "embedFooterText" TEXT NOT NULL DEFAULT 'https://github.com/ZynerOrg/xyter', + "embedFooterIcon" TEXT NOT NULL DEFAULT 'https://github.com/ZynerOrg.png', + "creditsEnabled" BOOLEAN NOT NULL DEFAULT false, + "creditsRate" INTEGER NOT NULL DEFAULT 1, + "creditsTimeout" INTEGER NOT NULL DEFAULT 5, + "creditsWorkRate" INTEGER NOT NULL DEFAULT 25, + "creditsWorkTimeout" INTEGER NOT NULL DEFAULT 86400, + "creditsMinimumLength" INTEGER NOT NULL DEFAULT 5, + "pointsEnabled" BOOLEAN NOT NULL DEFAULT false, + "pointsRate" INTEGER NOT NULL DEFAULT 1, + "pointsTimeout" INTEGER NOT NULL DEFAULT 5, + "pointsMinimumLength" INTEGER NOT NULL DEFAULT 5, + "reputationsEnabled" BOOLEAN NOT NULL DEFAULT false, + "countersEnabled" BOOLEAN NOT NULL DEFAULT false, + "apiCpggUrlIv" TEXT, + "apiCpggUrlContent" TEXT, + "apiCpggTokenIv" TEXT, + "apiCpggTokenContent" TEXT, + "auditsEnabled" BOOLEAN NOT NULL DEFAULT false, + "auditsChannelId" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); +INSERT INTO "new_Guild" ("apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "updatedAt") SELECT "apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "updatedAt" FROM "Guild"; +DROP TABLE "Guild"; +ALTER TABLE "new_Guild" RENAME TO "Guild"; +CREATE UNIQUE INDEX "Guild_id_key" ON "Guild"("id"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 16b1622..9a0a715 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -23,11 +23,6 @@ model Guild { embedFooterText String @default("https://github.com/ZynerOrg/xyter") embedFooterIcon String @default("https://github.com/ZynerOrg.png") - apiCpggUrlIv String? - apiCpggUrlContent String? - apiCpggTokenIv String? - apiCpggTokenContent String? - // Modules creditsEnabled Boolean @default(false) creditsRate Int @default(1) @@ -46,6 +41,14 @@ model Guild { countersEnabled Boolean @default(false) counters GuildCounter[] + apiCpggUrlIv String? + apiCpggUrlContent String? + apiCpggTokenIv String? + apiCpggTokenContent String? + + auditsEnabled Boolean @default(false) + auditsChannelId String? + createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } diff --git a/src/commands/config/modules/audits/index.ts b/src/commands/config/modules/audits/index.ts index 46e2bfb..3a6dd1f 100644 --- a/src/commands/config/modules/audits/index.ts +++ b/src/commands/config/modules/audits/index.ts @@ -5,9 +5,9 @@ import { EmbedBuilder, PermissionsBitField, } from "discord.js"; +import prisma from "../../../../handlers/database"; import getEmbedConfig from "../../../../helpers/getEmbedData"; import logger from "../../../../middlewares/logger"; -import guildSchema from "../../../../models/guild"; export default { metadata: { @@ -21,13 +21,17 @@ export default { .setName("audits") .setDescription("Audits") .addBooleanOption((option) => - option.setName("status").setDescription("Should audits be enabled?") + option + .setName("status") + .setDescription("Should audits be enabled?") + .setRequired(true) ) .addChannelOption((option) => option .setName("channel") .setDescription("Channel for audit messages.") .addChannelTypes(ChannelType.GuildText) + .setRequired(true) ); }, execute: async (interaction: ChatInputCommandInteraction) => { @@ -39,49 +43,55 @@ export default { const channel = options.getChannel("channel"); if (!guild) throw new Error("Guild not found."); - const guildDB = await guildSchema.findOne({ - guildId: guild.id, + if (!channel) throw new Error("Channel not found."); + if (status === null) throw new Error("Status not found."); + + const createGuild = await prisma.guild.upsert({ + where: { + id: guild.id, + }, + update: { + auditsEnabled: status, + auditsChannelId: channel.id, + }, + create: { + id: guild.id, + auditsEnabled: status, + auditsChannelId: channel.id, + }, }); - if (!guildDB) throw new Error("Guild configuration not found."); - guildDB.audits.status = status !== null ? status : guildDB.audits.status; - guildDB.audits.channelId = channel ? channel.id : guildDB.audits.channelId; + logger.silly(createGuild); - await guildDB.save().then(async () => { - logger.verbose( - `Guild ${guild.name} updated their configuration for audits.` - ); - - const embedSuccess = new EmbedBuilder() - .setTitle("[:hammer:] Audits") - .setDescription("Guild configuration updated successfully.") - .setColor(successColor) - .addFields( - { - name: "🤖 Status", - value: `${ - guildDB.audits.status - ? ":white_check_mark: Enabled" - : ":x: Disabled" - }`, - inline: true, - }, - { - name: "🌊 Channel", - value: `<#${guildDB.audits.channelId}>`, - inline: true, - } - ) - .setTimestamp() - .setFooter({ - iconURL: footerIcon, - text: footerText, - }); - - await interaction.editReply({ - embeds: [embedSuccess], + const embedSuccess = new EmbedBuilder() + .setTitle("[:hammer:] Audits") + .setDescription("Guild configuration updated successfully.") + .setColor(successColor) + .addFields( + { + name: "🤖 Status", + value: `${ + createGuild.auditsEnabled + ? ":white_check_mark: Enabled" + : ":x: Disabled" + }`, + inline: true, + }, + { + name: "🌊 Channel", + value: `<#${createGuild.auditsChannelId}>`, + inline: true, + } + ) + .setTimestamp() + .setFooter({ + iconURL: footerIcon, + text: footerText, }); - return; + + await interaction.editReply({ + embeds: [embedSuccess], }); + return; }, };