fix: 🗃️ 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
This commit is contained in:
Axel Olausson Holtenäs 2022-12-19 20:19:04 +01:00
parent 258e15c45d
commit 109b89c820
17 changed files with 337 additions and 171 deletions

View file

@ -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;

View file

@ -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 -- CreateTable
CREATE TABLE `GuildConfigEmbeds` ( CREATE TABLE `GuildConfigEmbeds` (
`id` VARCHAR(191) NOT NULL, `id` VARCHAR(191) NOT NULL,
@ -134,16 +60,6 @@ CREATE TABLE `GuildConfigCounters` (
UNIQUE INDEX `GuildConfigCounters_id_key`(`id`) UNIQUE INDEX `GuildConfigCounters_id_key`(`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ) 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 -- CreateTable
CREATE TABLE `GuildConfigApisCpgg` ( CREATE TABLE `GuildConfigApisCpgg` (
`id` VARCHAR(191) NOT NULL, `id` VARCHAR(191) NOT NULL,
@ -195,7 +111,7 @@ CREATE TABLE `GuildConfigWelcome` (
`id` VARCHAR(191) NOT NULL, `id` VARCHAR(191) NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL, `updatedAt` DATETIME(3) NOT NULL,
`statis` BOOLEAN NOT NULL DEFAULT false, `status` BOOLEAN NOT NULL DEFAULT false,
`joinChannelId` VARCHAR(191) NULL, `joinChannelId` VARCHAR(191) NULL,
`joinChannelMessage` VARCHAR(191) NULL, `joinChannelMessage` VARCHAR(191) NULL,
`leaveChannelId` VARCHAR(191) NULL, `leaveChannelId` VARCHAR(191) NULL,
@ -231,12 +147,6 @@ ALTER TABLE `GuildConfigReputation` ADD CONSTRAINT `GuildConfigReputation_id_fke
-- AddForeignKey -- AddForeignKey
ALTER TABLE `GuildConfigCounters` ADD CONSTRAINT `GuildConfigCounters_id_fkey` FOREIGN KEY (`id`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 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 -- AddForeignKey
ALTER TABLE `GuildConfigApisCpgg` ADD CONSTRAINT `GuildConfigApisCpgg_id_fkey` FOREIGN KEY (`id`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 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 -- AddForeignKey
ALTER TABLE `GuildCounters` ADD CONSTRAINT `GuildCounters_guildId_fkey` FOREIGN KEY (`guildId`) REFERENCES `Guild`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 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`;

View file

@ -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;

View file

@ -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`;

View file

@ -49,12 +49,12 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
id: guild.id, id: guild.id,
}, },
update: { update: {
status: status, status,
channelId: channel.id, channelId: channel.id,
}, },
create: { create: {
id: guild.id, id: guild.id,
status: status, status,
channelId: channel.id, channelId: channel.id,
}, },
}); });

View file

@ -95,20 +95,20 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
}, },
update: { update: {
status: enabled, status: enabled,
rate: rate, rate,
timeout: timeout, timeout,
workRate: workRate, workRate,
workTimeout: workTimeout, workTimeout,
minimumLength: minimumLength, minimumLength,
}, },
create: { create: {
id: guild.id, id: guild.id,
status: enabled, status: enabled,
rate: rate, rate,
timeout: timeout, timeout,
workRate: workRate, workRate,
workTimeout: workTimeout, workTimeout,
minimumLength: minimumLength, minimumLength,
}, },
}); });

View file

@ -67,17 +67,17 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
id: guild.id, id: guild.id,
}, },
update: { update: {
status: status, status,
rate: rate, rate,
timeout: timeout, timeout,
minimumLength: minimumLength, minimumLength,
}, },
create: { create: {
id: guild.id, id: guild.id,
status: status, status,
rate: rate, rate,
timeout: timeout, timeout,
minimumLength: minimumLength, minimumLength,
}, },
}); });

View file

@ -81,19 +81,19 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
id: guild.id, id: guild.id,
}, },
update: { update: {
status: status, status,
joinChannelId: joinChannel.id, joinChannelId: joinChannel.id,
joinChannelMessage: joinChannelMessage, joinChannelMessage,
leaveChannelId: leaveChannel.id, leaveChannelId: leaveChannel.id,
leaveChannelMessage: leaveChannelMessage, leaveChannelMessage,
}, },
create: { create: {
id: guild.id, id: guild.id,
status: status, status,
joinChannelId: joinChannel.id, joinChannelId: joinChannel.id,
joinChannelMessage: joinChannelMessage, joinChannelMessage,
leaveChannelId: leaveChannel.id, leaveChannelId: leaveChannel.id,
leaveChannelMessage: leaveChannelMessage, leaveChannelMessage,
}, },
}); });

View file

@ -3,6 +3,7 @@ import { CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js";
import prisma from "../../../../handlers/database"; import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
import upsertGuildMember from "../../../../helpers/upsertGuildMember";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
// 1. Export a builder function. // 1. Export a builder function.
@ -33,7 +34,7 @@ export const execute = async (interaction: CommandInteraction) => {
const EmbedSuccess = await BaseEmbedSuccess(guild, ":credit_card:︱Balance"); const EmbedSuccess = await BaseEmbedSuccess(guild, ":credit_card:︱Balance");
// 5. Upsert the user in the database. // 5. Upsert the user in the database.
const createGuildMember = await prisma.guildMemberCredits.upsert({ const createGuildMemberCredits = await prisma.guildMemberCredits.upsert({
where: { where: {
userId_guildId: { userId_guildId: {
userId: (target || user).id, userId: (target || user).id,
@ -60,15 +61,17 @@ export const execute = async (interaction: CommandInteraction) => {
include: { GuildMember: true }, include: { GuildMember: true },
}); });
logger.silly(createGuildMember); logger.silly(createGuildMemberCredits);
await upsertGuildMember(guild, user);
// 6. Send embed. // 6. Send embed.
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
EmbedSuccess.setDescription( EmbedSuccess.setDescription(
target target
? `${target} has ${createGuildMember.balance} coins in his account.` ? `${target} has ${createGuildMemberCredits.balance} coins in his account.`
: `You have ${createGuildMember.balance} coins in your account.` : `You have ${createGuildMemberCredits.balance} coins in your account.`
), ),
], ],
}); });

View file

@ -7,6 +7,7 @@ import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
import creditsTransfer from "../../../../helpers/credits/transfer"; import creditsTransfer from "../../../../helpers/credits/transfer";
import upsertGuildMember from "../../../../helpers/upsertGuildMember";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
// 1. Export a builder function. // 1. Export a builder function.
@ -59,6 +60,8 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
`:credit_card:︱You received a gift from ${user.username}` `:credit_card:︱You received a gift from ${user.username}`
); );
await upsertGuildMember(guild, user);
// 5. Start an transaction of the credits. // 5. Start an transaction of the credits.
await creditsTransfer(guild, user, account, credits); await creditsTransfer(guild, user, account, credits);

View file

@ -8,6 +8,7 @@ import {
import prisma from "../../../../handlers/database"; import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
import upsertGuildMember from "../../../../helpers/upsertGuildMember";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
// 1. Export a builder function. // 1. Export a builder function.
@ -21,10 +22,12 @@ export const execute = async (interaction: CommandInteraction) => {
await deferReply(interaction, false); await deferReply(interaction, false);
// 2. Destructure interaction object. // 2. Destructure interaction object.
const { guild, client } = interaction; const { guild, client, user } = interaction;
if (!guild) throw new Error("Guild not found"); if (!guild) throw new Error("Guild not found");
if (!client) throw new Error("Client not found"); if (!client) throw new Error("Client not found");
await upsertGuildMember(guild, user);
// 3. Create base embeds. // 3. Create base embeds.
const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Top"); const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Top");

View file

@ -5,6 +5,7 @@ import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds"; import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
import creditsGive from "../../../../helpers/credits/give"; import creditsGive from "../../../../helpers/credits/give";
import upsertGuildMember from "../../../../helpers/upsertGuildMember";
import cooldown from "../../../../middlewares/cooldown"; import cooldown from "../../../../middlewares/cooldown";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
@ -23,6 +24,8 @@ export const execute = async (interaction: CommandInteraction) => {
if (!guild) throw new Error("Guild not found"); if (!guild) throw new Error("Guild not found");
if (!user) throw new Error("User not found"); if (!user) throw new Error("User not found");
await upsertGuildMember(guild, user);
// 3. Create base embeds. // 3. Create base embeds.
const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Work"); const EmbedSuccess = await BaseEmbedSuccess(guild, "[:dollar:] Work");
@ -51,13 +54,13 @@ export const execute = async (interaction: CommandInteraction) => {
max: createGuild.workRate, max: createGuild.workRate,
}); });
const upsertGuildMember = await creditsGive(guild, user, creditsEarned); const upsertGuildMemberResult = await creditsGive(guild, user, creditsEarned);
// 8. Send embed. // 8. Send embed.
await interaction.editReply({ await interaction.editReply({
embeds: [ embeds: [
EmbedSuccess.setDescription( 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:`
), ),
], ],
}); });

View file

@ -53,7 +53,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
const { successColor, footerText, footerIcon } = await getEmbedConfig( const { successColor, footerText, footerIcon } = await getEmbedConfig(
interaction.guild interaction.guild
); // Destructure ); // Destructure
const { guild, user, options } = interaction; const { guild, options } = interaction;
const uses = options?.getInteger("uses"); const uses = options?.getInteger("uses");
const creditAmount = options?.getInteger("credit"); const creditAmount = options?.getInteger("credit");

View file

@ -13,7 +13,10 @@ export default {
where: { id: member.guild.id }, where: { id: member.guild.id },
}); });
if (!getGuildConfigWelcome) return logger.verbose("Guild not found"); if (!getGuildConfigWelcome) {
logger.verbose("Guild not found");
return;
}
const { client } = member; const { client } = member;

View file

@ -12,7 +12,6 @@ export const execute = async (message: Message) => {
await modules.points.execute(message); await modules.points.execute(message);
await modules.counters.execute(message); await modules.counters.execute(message);
const { client } = message;
if (!message.member) return; if (!message.member) return;
if (message.author.bot) return; if (message.author.bot) return;

View file

@ -7,7 +7,10 @@ export default async (guild: Guild, embed: EmbedBuilder) => {
const getGuildConfigAudits = await prisma.guildConfigAudits.findUnique({ const getGuildConfigAudits = await prisma.guildConfigAudits.findUnique({
where: { id: guild.id }, 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.status !== true) return;
if (!getGuildConfigAudits.channelId) { if (!getGuildConfigAudits.channelId) {

View file

@ -1,9 +1,7 @@
import { GuildMember } from "discord.js"; import { Guild, User } from "discord.js";
import db from "../../handlers/database"; import db from "../../handlers/database";
export default async (guildMember: GuildMember) => { export default async (guild: Guild, user: User) => {
const { guild, user } = guildMember;
return await db.guildMember.upsert({ return await db.guildMember.upsert({
where: { where: {
userId_guildId: { userId_guildId: {