From 109b89c82030046edec5ccf8710a94fbdd6d90be Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Mon, 19 Dec 2022 20:19:04 +0100 Subject: [PATCH] fix: :card_file_box: Perform migrations from older versions This is my first customized migration script, I hope it works as intended. This may result in a database loss, please perform backups before proceeding, it should do migrate if everything works as intended --- .../migration.sql | 16 - .../migration.sql | 372 +++++++++++++----- .../20221219171553_fix_typo/migration.sql | 9 - .../migration.sql | 14 - .../config/subcommands/audits/index.ts | 4 +- .../config/subcommands/credits/index.ts | 20 +- .../config/subcommands/points/index.ts | 16 +- .../config/subcommands/welcome/index.ts | 12 +- .../credits/subcommands/balance/index.ts | 11 +- .../credits/subcommands/gift/index.ts | 3 + src/commands/credits/subcommands/top/index.ts | 5 +- .../credits/subcommands/work/index.ts | 7 +- .../credits/subcommands/giveaway/index.ts | 2 +- src/events/guildMemberAdd/joinMessage.ts | 5 +- src/events/messageCreate/index.ts | 1 - src/helpers/auditLogger/index.ts | 5 +- src/helpers/upsertGuildMember/index.ts | 6 +- 17 files changed, 337 insertions(+), 171 deletions(-) delete mode 100644 prisma/migrations/20221217150459_move_credits_earned_to_credits_module/migration.sql delete mode 100644 prisma/migrations/20221219171553_fix_typo/migration.sql delete mode 100644 prisma/migrations/20221219175656_remove_api_collection/migration.sql diff --git a/prisma/migrations/20221217150459_move_credits_earned_to_credits_module/migration.sql b/prisma/migrations/20221217150459_move_credits_earned_to_credits_module/migration.sql deleted file mode 100644 index 073fb79..0000000 --- a/prisma/migrations/20221217150459_move_credits_earned_to_credits_module/migration.sql +++ /dev/null @@ -1,16 +0,0 @@ --- AlterTable - -ALTER TABLE `GuildMemberCredits` ADD COLUMN `balance` INTEGER NOT NULL DEFAULT 0; - --- InsertInto - -INSERT INTO GuildMemberCredits (balance, userId, guildId) -SELECT creditsEarned, - userId, - guildId -FROM GuildMember; - --- DropColumn - -ALTER TABLE GuildMember -DROP COLUMN creditsEarned; \ No newline at end of file diff --git a/prisma/migrations/20221219165454_seperate_modules_on_guild/migration.sql b/prisma/migrations/20221219165454_seperate_modules_on_guild/migration.sql index 8c452fe..c4e7e8a 100644 --- a/prisma/migrations/20221219165454_seperate_modules_on_guild/migration.sql +++ b/prisma/migrations/20221219165454_seperate_modules_on_guild/migration.sql @@ -1,77 +1,3 @@ -/* - Warnings: - - - You are about to drop the column `apiCpggTokenContent` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `apiCpggTokenIv` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `apiCpggUrlContent` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `apiCpggUrlIv` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `auditsChannelId` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `auditsEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `countersEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsMinimumLength` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsRate` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsTimeout` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsWorkRate` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `creditsWorkTimeout` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `embedColorError` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `embedColorSuccess` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `embedColorWait` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `embedFooterIcon` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `embedFooterText` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `pointsEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `pointsMinimumLength` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `pointsRate` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `pointsTimeout` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `reputationsEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `shopRolesEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `shopRolesPricePerHour` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `welcomeEnabled` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `welcomeJoinChannelId` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `welcomeJoinChannelMessage` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `welcomeLeaveChannelId` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the column `welcomeLeaveChannelMessage` on the `Guild` table. All the data in the column will be lost. - - You are about to drop the `GuildCounter` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropForeignKey -ALTER TABLE `GuildCounter` DROP FOREIGN KEY `GuildCounter_guildId_fkey`; - --- AlterTable -ALTER TABLE `Guild` DROP COLUMN `apiCpggTokenContent`, - DROP COLUMN `apiCpggTokenIv`, - DROP COLUMN `apiCpggUrlContent`, - DROP COLUMN `apiCpggUrlIv`, - DROP COLUMN `auditsChannelId`, - DROP COLUMN `auditsEnabled`, - DROP COLUMN `countersEnabled`, - DROP COLUMN `creditsEnabled`, - DROP COLUMN `creditsMinimumLength`, - DROP COLUMN `creditsRate`, - DROP COLUMN `creditsTimeout`, - DROP COLUMN `creditsWorkRate`, - DROP COLUMN `creditsWorkTimeout`, - DROP COLUMN `embedColorError`, - DROP COLUMN `embedColorSuccess`, - DROP COLUMN `embedColorWait`, - DROP COLUMN `embedFooterIcon`, - DROP COLUMN `embedFooterText`, - DROP COLUMN `pointsEnabled`, - DROP COLUMN `pointsMinimumLength`, - DROP COLUMN `pointsRate`, - DROP COLUMN `pointsTimeout`, - DROP COLUMN `reputationsEnabled`, - DROP COLUMN `shopRolesEnabled`, - DROP COLUMN `shopRolesPricePerHour`, - DROP COLUMN `welcomeEnabled`, - DROP COLUMN `welcomeJoinChannelId`, - DROP COLUMN `welcomeJoinChannelMessage`, - DROP COLUMN `welcomeLeaveChannelId`, - DROP COLUMN `welcomeLeaveChannelMessage`; - --- DropTable -DROP TABLE `GuildCounter`; - -- CreateTable CREATE TABLE `GuildConfigEmbeds` ( `id` VARCHAR(191) NOT NULL, @@ -134,16 +60,6 @@ CREATE TABLE `GuildConfigCounters` ( UNIQUE INDEX `GuildConfigCounters_id_key`(`id`) ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; --- CreateTable -CREATE TABLE `GuildConfigApis` ( - `id` VARCHAR(191) NOT NULL, - `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updatedAt` DATETIME(3) NOT NULL, - `guildConfigApisCpggId` VARCHAR(191) NULL, - - UNIQUE INDEX `GuildConfigApis_id_key`(`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - -- CreateTable CREATE TABLE `GuildConfigApisCpgg` ( `id` VARCHAR(191) NOT NULL, @@ -195,7 +111,7 @@ CREATE TABLE `GuildConfigWelcome` ( `id` VARCHAR(191) NOT NULL, `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), `updatedAt` DATETIME(3) NOT NULL, - `statis` BOOLEAN NOT NULL DEFAULT false, + `status` BOOLEAN NOT NULL DEFAULT false, `joinChannelId` VARCHAR(191) NULL, `joinChannelMessage` VARCHAR(191) NULL, `leaveChannelId` VARCHAR(191) NULL, @@ -231,12 +147,6 @@ ALTER TABLE `GuildConfigReputation` ADD CONSTRAINT `GuildConfigReputation_id_fke -- AddForeignKey ALTER TABLE `GuildConfigCounters` ADD CONSTRAINT `GuildConfigCounters_id_fkey` FOREIGN KEY (`id`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; --- AddForeignKey -ALTER TABLE `GuildConfigApis` ADD CONSTRAINT `GuildConfigApis_id_fkey` FOREIGN KEY (`id`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `GuildConfigApis` ADD CONSTRAINT `GuildConfigApis_guildConfigApisCpggId_fkey` FOREIGN KEY (`guildConfigApisCpggId`) REFERENCES `GuildConfigApisCpgg`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; - -- AddForeignKey ALTER TABLE `GuildConfigApisCpgg` ADD CONSTRAINT `GuildConfigApisCpgg_id_fkey` FOREIGN KEY (`id`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; @@ -257,3 +167,283 @@ ALTER TABLE `GuildConfigWelcome` ADD CONSTRAINT `GuildConfigWelcome_id_fkey` FOR -- AddForeignKey ALTER TABLE `GuildCounters` ADD CONSTRAINT `GuildCounters_guildId_fkey` FOREIGN KEY (`guildId`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + + + +-- AlterTable + +ALTER TABLE `GuildMemberCredits` ADD COLUMN `balance` INTEGER NOT NULL DEFAULT 0; + +-- InsertInto + +INSERT INTO GuildConfigApisCpgg (tokenContent,tokenIv,urlContent,urlIv,id,updatedAt) +SELECT apiCpggTokenContent,apiCpggTokenIv,apiCpggUrlContent,apiCpggUrlIv,id,updatedAt +FROM Guild +WHERE + ( + apiCpggTokenContent IS NOT NULL + AND + apiCpggTokenIv IS NOT NULL + AND + apiCpggUrlContent IS NOT NULL + AND + apiCpggUrlIv IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `apiCpggTokenContent`, + DROP COLUMN `apiCpggTokenIv`, + DROP COLUMN `apiCpggUrlContent`, + DROP COLUMN `apiCpggUrlIv`; + +-- InsertInto + +INSERT INTO GuildConfigAudits (status,channelId,id,updatedAt) +SELECT auditsEnabled,auditsChannelId,id,updatedAt +FROM Guild +WHERE + ( + auditsEnabled IS NOT NULL + AND + auditsChannelId IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `auditsChannelId`, + DROP COLUMN `auditsEnabled`; + +-- InsertInto + +INSERT INTO GuildConfigCounters (status,id,updatedAt) +SELECT countersEnabled,id,updatedAt +FROM Guild +WHERE + ( + countersEnabled IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `countersEnabled`; + +-- InsertInto + +INSERT INTO GuildConfigCredits (status,minimumLength,rate,timeout,workRate,workTimeout,id,updatedAt) +SELECT creditsEnabled,creditsMinimumLength,creditsRate,creditsTimeout,creditsWorkRate,creditsWorkTimeout,id,updatedAt +FROM Guild +WHERE + ( + creditsEnabled IS NOT NULL + AND + creditsMinimumLength IS NOT NULL + AND + creditsRate IS NOT NULL + AND + creditsTimeout IS NOT NULL + AND + creditsWorkRate IS NOT NULL + AND + creditsWorkTimeout IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `creditsEnabled`, + DROP COLUMN `creditsMinimumLength`, + DROP COLUMN `creditsRate`, + DROP COLUMN `creditsTimeout`, + DROP COLUMN `creditsWorkRate`, + DROP COLUMN `creditsWorkTimeout`; + + + +-- InsertInto + +INSERT INTO GuildConfigEmbeds (errorColor,successColor,waitColor,footerIcon,footerText,id,updatedAt) +SELECT embedColorError,embedColorSuccess,embedColorWait,embedFooterIcon,embedFooterText,id,updatedAt +FROM Guild +WHERE + ( + embedColorError IS NOT NULL + AND + embedColorSuccess IS NOT NULL + AND + embedColorWait IS NOT NULL + AND + embedFooterIcon IS NOT NULL + AND + embedFooterText IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `embedColorError`, + DROP COLUMN `embedColorSuccess`, + DROP COLUMN `embedColorWait`, + DROP COLUMN `embedFooterIcon`, + DROP COLUMN `embedFooterText`; + + +-- InsertInto + +INSERT INTO GuildConfigPoints (status,minimumLength,rate,timeout,id,updatedAt) +SELECT pointsEnabled,pointsMinimumLength,pointsRate,pointsTimeout,id,updatedAt +FROM Guild +WHERE + ( + pointsEnabled IS NOT NULL + AND + pointsMinimumLength IS NOT NULL + AND + pointsRate IS NOT NULL + AND + pointsTimeout IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `pointsEnabled`, + DROP COLUMN `pointsMinimumLength`, + DROP COLUMN `pointsRate`, + DROP COLUMN `pointsTimeout`; + + +-- InsertInto + +INSERT INTO GuildConfigReputation (status,id,updatedAt) +SELECT reputationsEnabled,id,updatedAt +FROM Guild +WHERE + ( + reputationsEnabled IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `reputationsEnabled`; + + + +-- InsertInto + +INSERT INTO GuildConfigShopRoles (status,pricePerHour,id,updatedAt) +SELECT shopRolesEnabled,shopRolesPricePerHour,id,updatedAt +FROM Guild +WHERE + ( + shopRolesEnabled IS NOT NULL + AND + shopRolesPricePerHour IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable + +ALTER TABLE `Guild` DROP COLUMN `shopRolesEnabled`, + DROP COLUMN `shopRolesPricePerHour`; + +-- InsertInto + +INSERT INTO GuildConfigWelcome (status,joinChannelId,joinChannelMessage,leaveChannelId,leaveChannelMessage,id,updatedAt) +SELECT welcomeEnabled,welcomeJoinChannelId,welcomeJoinChannelMessage,welcomeLeaveChannelId,welcomeLeaveChannelMessage,id,updatedAt +FROM Guild +WHERE + ( + welcomeEnabled IS NOT NULL + AND + welcomeJoinChannelId IS NOT NULL + AND + welcomeJoinChannelMessage IS NOT NULL + AND + welcomeLeaveChannelId IS NOT NULL + AND + id IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- AlterTable +ALTER TABLE `Guild` DROP COLUMN `welcomeEnabled`, + DROP COLUMN `welcomeJoinChannelId`, + DROP COLUMN `welcomeJoinChannelMessage`, + DROP COLUMN `welcomeLeaveChannelId`, + DROP COLUMN `welcomeLeaveChannelMessage`; + + + +-- InsertInto + +INSERT INTO GuildCounters (guildId,channelId,triggerWord,count,updatedAt) +SELECT guildId,channelId,triggerWord,count,updatedAt +FROM GuildCounter +WHERE + ( + guildId IS NOT NULL + AND + channelId IS NOT NULL + AND + triggerWord IS NOT NULL + AND + count IS NOT NULL + AND + updatedAt IS NOT NULL + ); + +-- DropTable +DROP TABLE `GuildCounter`; + +-- InsertInto + +INSERT INTO GuildMemberCredits (balance,userId,guildId) +SELECT creditsEarned,userid,guildId +FROM GuildMember +WHERE + ( + creditsEarned IS NOT NULL + AND + userId IS NOT NULL + AND + guildId IS NOT NULL + ); + +-- DeleteFrom +DELETE FROM GuildMemberCredits +WHERE balance = 0; + +-- AlterTable + +ALTER TABLE `GuildMember` DROP COLUMN `creditsEarned`; + diff --git a/prisma/migrations/20221219171553_fix_typo/migration.sql b/prisma/migrations/20221219171553_fix_typo/migration.sql deleted file mode 100644 index be615cc..0000000 --- a/prisma/migrations/20221219171553_fix_typo/migration.sql +++ /dev/null @@ -1,9 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `statis` on the `GuildConfigWelcome` table. All the data in the column will be lost. - -*/ --- AlterTable -ALTER TABLE `GuildConfigWelcome` DROP COLUMN `statis`, - ADD COLUMN `status` BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/migrations/20221219175656_remove_api_collection/migration.sql b/prisma/migrations/20221219175656_remove_api_collection/migration.sql deleted file mode 100644 index f6e459f..0000000 --- a/prisma/migrations/20221219175656_remove_api_collection/migration.sql +++ /dev/null @@ -1,14 +0,0 @@ -/* - Warnings: - - - You are about to drop the `GuildConfigApis` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropForeignKey -ALTER TABLE `GuildConfigApis` DROP FOREIGN KEY `GuildConfigApis_guildConfigApisCpggId_fkey`; - --- DropForeignKey -ALTER TABLE `GuildConfigApis` DROP FOREIGN KEY `GuildConfigApis_id_fkey`; - --- DropTable -DROP TABLE `GuildConfigApis`; diff --git a/src/commands/config/subcommands/audits/index.ts b/src/commands/config/subcommands/audits/index.ts index b54de5c..c1c551d 100644 --- a/src/commands/config/subcommands/audits/index.ts +++ b/src/commands/config/subcommands/audits/index.ts @@ -49,12 +49,12 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { id: guild.id, }, update: { - status: status, + status, channelId: channel.id, }, create: { id: guild.id, - status: status, + status, channelId: channel.id, }, }); diff --git a/src/commands/config/subcommands/credits/index.ts b/src/commands/config/subcommands/credits/index.ts index ab2d094..8eeb1e9 100644 --- a/src/commands/config/subcommands/credits/index.ts +++ b/src/commands/config/subcommands/credits/index.ts @@ -95,20 +95,20 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { }, update: { status: enabled, - rate: rate, - timeout: timeout, - workRate: workRate, - workTimeout: workTimeout, - minimumLength: minimumLength, + rate, + timeout, + workRate, + workTimeout, + minimumLength, }, create: { id: guild.id, status: enabled, - rate: rate, - timeout: timeout, - workRate: workRate, - workTimeout: workTimeout, - minimumLength: minimumLength, + rate, + timeout, + workRate, + workTimeout, + minimumLength, }, }); diff --git a/src/commands/config/subcommands/points/index.ts b/src/commands/config/subcommands/points/index.ts index 5db7845..efcf161 100644 --- a/src/commands/config/subcommands/points/index.ts +++ b/src/commands/config/subcommands/points/index.ts @@ -67,17 +67,17 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { id: guild.id, }, update: { - status: status, - rate: rate, - timeout: timeout, - minimumLength: minimumLength, + status, + rate, + timeout, + minimumLength, }, create: { id: guild.id, - status: status, - rate: rate, - timeout: timeout, - minimumLength: minimumLength, + status, + rate, + timeout, + minimumLength, }, }); diff --git a/src/commands/config/subcommands/welcome/index.ts b/src/commands/config/subcommands/welcome/index.ts index 69e9a94..4b4f77a 100644 --- a/src/commands/config/subcommands/welcome/index.ts +++ b/src/commands/config/subcommands/welcome/index.ts @@ -81,19 +81,19 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { id: guild.id, }, update: { - status: status, + status, joinChannelId: joinChannel.id, - joinChannelMessage: joinChannelMessage, + joinChannelMessage, leaveChannelId: leaveChannel.id, - leaveChannelMessage: leaveChannelMessage, + leaveChannelMessage, }, create: { id: guild.id, - status: status, + status, joinChannelId: joinChannel.id, - joinChannelMessage: joinChannelMessage, + joinChannelMessage, leaveChannelId: leaveChannel.id, - leaveChannelMessage: leaveChannelMessage, + leaveChannelMessage, }, }); diff --git a/src/commands/credits/subcommands/balance/index.ts b/src/commands/credits/subcommands/balance/index.ts index 76b5385..4e82abe 100644 --- a/src/commands/credits/subcommands/balance/index.ts +++ b/src/commands/credits/subcommands/balance/index.ts @@ -3,6 +3,7 @@ import { CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js"; import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; +import upsertGuildMember from "../../../../helpers/upsertGuildMember"; import logger from "../../../../middlewares/logger"; // 1. Export a builder function. @@ -33,7 +34,7 @@ export const execute = async (interaction: CommandInteraction) => { const EmbedSuccess = await BaseEmbedSuccess(guild, ":credit_card:︱Balance"); // 5. Upsert the user in the database. - const createGuildMember = await prisma.guildMemberCredits.upsert({ + const createGuildMemberCredits = await prisma.guildMemberCredits.upsert({ where: { userId_guildId: { userId: (target || user).id, @@ -60,15 +61,17 @@ export const execute = async (interaction: CommandInteraction) => { include: { GuildMember: true }, }); - logger.silly(createGuildMember); + logger.silly(createGuildMemberCredits); + + await upsertGuildMember(guild, user); // 6. Send embed. await interaction.editReply({ embeds: [ EmbedSuccess.setDescription( target - ? `${target} has ${createGuildMember.balance} coins in his account.` - : `You have ${createGuildMember.balance} coins in your account.` + ? `${target} has ${createGuildMemberCredits.balance} coins in his account.` + : `You have ${createGuildMemberCredits.balance} coins in your account.` ), ], }); diff --git a/src/commands/credits/subcommands/gift/index.ts b/src/commands/credits/subcommands/gift/index.ts index 57c52e5..85c92eb 100644 --- a/src/commands/credits/subcommands/gift/index.ts +++ b/src/commands/credits/subcommands/gift/index.ts @@ -7,6 +7,7 @@ import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import creditsTransfer from "../../../../helpers/credits/transfer"; +import upsertGuildMember from "../../../../helpers/upsertGuildMember"; import logger from "../../../../middlewares/logger"; // 1. Export a builder function. @@ -59,6 +60,8 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { `:credit_card:︱You received a gift from ${user.username}` ); + await upsertGuildMember(guild, user); + // 5. Start an transaction of the credits. await creditsTransfer(guild, user, account, credits); diff --git a/src/commands/credits/subcommands/top/index.ts b/src/commands/credits/subcommands/top/index.ts index 7e6e016..7385b8e 100644 --- a/src/commands/credits/subcommands/top/index.ts +++ b/src/commands/credits/subcommands/top/index.ts @@ -8,6 +8,7 @@ import { import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; +import upsertGuildMember from "../../../../helpers/upsertGuildMember"; import logger from "../../../../middlewares/logger"; // 1. Export a builder function. @@ -21,10 +22,12 @@ export const execute = async (interaction: CommandInteraction) => { await deferReply(interaction, false); // 2. Destructure interaction object. - const { guild, client } = interaction; + const { guild, client, user } = interaction; if (!guild) throw new Error("Guild not found"); if (!client) throw new Error("Client not found"); + await upsertGuildMember(guild, user); + // 3. Create base embeds. const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Top"); diff --git a/src/commands/credits/subcommands/work/index.ts b/src/commands/credits/subcommands/work/index.ts index ed60419..985140b 100644 --- a/src/commands/credits/subcommands/work/index.ts +++ b/src/commands/credits/subcommands/work/index.ts @@ -5,6 +5,7 @@ import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import creditsGive from "../../../../helpers/credits/give"; +import upsertGuildMember from "../../../../helpers/upsertGuildMember"; import cooldown from "../../../../middlewares/cooldown"; import logger from "../../../../middlewares/logger"; @@ -23,6 +24,8 @@ export const execute = async (interaction: CommandInteraction) => { if (!guild) throw new Error("Guild not found"); if (!user) throw new Error("User not found"); + await upsertGuildMember(guild, user); + // 3. Create base embeds. const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Work"); @@ -51,13 +54,13 @@ export const execute = async (interaction: CommandInteraction) => { max: createGuild.workRate, }); - const upsertGuildMember = await creditsGive(guild, user, creditsEarned); + const upsertGuildMemberResult = await creditsGive(guild, user, creditsEarned); // 8. Send embed. await interaction.editReply({ embeds: [ EmbedSuccess.setDescription( - `You worked and earned **${creditsEarned}** credits! You now have **${upsertGuildMember.balance}** credits. :tada:` + `You worked and earned **${creditsEarned}** credits! You now have **${upsertGuildMemberResult.balance}** credits. :tada:` ), ], }); diff --git a/src/commands/manage/groups/credits/subcommands/giveaway/index.ts b/src/commands/manage/groups/credits/subcommands/giveaway/index.ts index 66f48c2..5d884c2 100644 --- a/src/commands/manage/groups/credits/subcommands/giveaway/index.ts +++ b/src/commands/manage/groups/credits/subcommands/giveaway/index.ts @@ -53,7 +53,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { const { successColor, footerText, footerIcon } = await getEmbedConfig( interaction.guild ); // Destructure - const { guild, user, options } = interaction; + const { guild, options } = interaction; const uses = options?.getInteger("uses"); const creditAmount = options?.getInteger("credit"); diff --git a/src/events/guildMemberAdd/joinMessage.ts b/src/events/guildMemberAdd/joinMessage.ts index 9be7fbd..007c67f 100644 --- a/src/events/guildMemberAdd/joinMessage.ts +++ b/src/events/guildMemberAdd/joinMessage.ts @@ -13,7 +13,10 @@ export default { where: { id: member.guild.id }, }); - if (!getGuildConfigWelcome) return logger.verbose("Guild not found"); + if (!getGuildConfigWelcome) { + logger.verbose("Guild not found"); + return; + } const { client } = member; diff --git a/src/events/messageCreate/index.ts b/src/events/messageCreate/index.ts index 5b51731..d58f5e4 100644 --- a/src/events/messageCreate/index.ts +++ b/src/events/messageCreate/index.ts @@ -12,7 +12,6 @@ export const execute = async (message: Message) => { await modules.points.execute(message); await modules.counters.execute(message); - const { client } = message; if (!message.member) return; if (message.author.bot) return; diff --git a/src/helpers/auditLogger/index.ts b/src/helpers/auditLogger/index.ts index 7dd022b..5cb4092 100644 --- a/src/helpers/auditLogger/index.ts +++ b/src/helpers/auditLogger/index.ts @@ -7,7 +7,10 @@ export default async (guild: Guild, embed: EmbedBuilder) => { const getGuildConfigAudits = await prisma.guildConfigAudits.findUnique({ where: { id: guild.id }, }); - if (!getGuildConfigAudits) return logger.verbose("Guild not found"); + if (!getGuildConfigAudits) { + logger.verbose("Guild not found"); + return; + } if (getGuildConfigAudits.status !== true) return; if (!getGuildConfigAudits.channelId) { diff --git a/src/helpers/upsertGuildMember/index.ts b/src/helpers/upsertGuildMember/index.ts index 22a90a2..2275e32 100644 --- a/src/helpers/upsertGuildMember/index.ts +++ b/src/helpers/upsertGuildMember/index.ts @@ -1,9 +1,7 @@ -import { GuildMember } from "discord.js"; +import { Guild, User } from "discord.js"; import db from "../../handlers/database"; -export default async (guildMember: GuildMember) => { - const { guild, user } = guildMember; - +export default async (guild: Guild, user: User) => { return await db.guildMember.upsert({ where: { userId_guildId: {