♻️ Config Welcome now on Prisma

This commit is contained in:
Axel Olausson Holtenäs 2022-10-20 13:42:24 +02:00
parent 5f5f3dccb8
commit 5e7a30169b
5 changed files with 218 additions and 106 deletions

View file

@ -0,0 +1,41 @@
-- 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,
"shopRolesEnabled" BOOLEAN NOT NULL DEFAULT false,
"shopRolesPricePerHour" INTEGER NOT NULL DEFAULT 5,
"welcomeEnabled" BOOLEAN NOT NULL DEFAULT false,
"welcomeJoinChannelId" TEXT,
"welcomejoinChannelMessahe" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Guild" ("apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "updatedAt") SELECT "apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "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;

View file

@ -0,0 +1,49 @@
/*
Warnings:
- You are about to drop the column `welcomejoinChannelMessahe` on the `Guild` table. All the data in the column will be lost.
*/
-- 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,
"shopRolesEnabled" BOOLEAN NOT NULL DEFAULT false,
"shopRolesPricePerHour" INTEGER NOT NULL DEFAULT 5,
"welcomeEnabled" BOOLEAN NOT NULL DEFAULT false,
"welcomeJoinChannelId" TEXT,
"welcomeJoinChannelMessahe" TEXT,
"welcomeLeaveChannelId" TEXT,
"welcomeLeaveChannelMessage" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Guild" ("apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "updatedAt", "welcomeEnabled", "welcomeJoinChannelId") SELECT "apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "updatedAt", "welcomeEnabled", "welcomeJoinChannelId" 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;

View file

@ -0,0 +1,49 @@
/*
Warnings:
- You are about to drop the column `welcomeJoinChannelMessahe` on the `Guild` table. All the data in the column will be lost.
*/
-- 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,
"shopRolesEnabled" BOOLEAN NOT NULL DEFAULT false,
"shopRolesPricePerHour" INTEGER NOT NULL DEFAULT 5,
"welcomeEnabled" BOOLEAN NOT NULL DEFAULT false,
"welcomeJoinChannelId" TEXT,
"welcomeJoinChannelMessage" TEXT,
"welcomeLeaveChannelId" TEXT,
"welcomeLeaveChannelMessage" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Guild" ("apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "updatedAt", "welcomeEnabled", "welcomeJoinChannelId", "welcomeLeaveChannelId", "welcomeLeaveChannelMessage") SELECT "apiCpggTokenContent", "apiCpggTokenIv", "apiCpggUrlContent", "apiCpggUrlIv", "auditsChannelId", "auditsEnabled", "countersEnabled", "createdAt", "creditsEnabled", "creditsMinimumLength", "creditsRate", "creditsTimeout", "creditsWorkRate", "creditsWorkTimeout", "embedColorError", "embedColorSuccess", "embedColorWait", "embedFooterIcon", "embedFooterText", "id", "pointsEnabled", "pointsMinimumLength", "pointsRate", "pointsTimeout", "reputationsEnabled", "shopRolesEnabled", "shopRolesPricePerHour", "updatedAt", "welcomeEnabled", "welcomeJoinChannelId", "welcomeLeaveChannelId", "welcomeLeaveChannelMessage" 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;

View file

@ -52,6 +52,12 @@ model Guild {
shopRolesEnabled Boolean @default(false)
shopRolesPricePerHour Int @default(5)
welcomeEnabled Boolean @default(false)
welcomeJoinChannelId String?
welcomeJoinChannelMessage String?
welcomeLeaveChannelId String?
welcomeLeaveChannelMessage String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

View file

@ -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("welcome")
.setDescription("Welcome")
.addBooleanOption((option) =>
option.setName("status").setDescription("Should welcome be enabled?")
option
.setName("status")
.setDescription("Should welcome be enabled?")
.setRequired(true)
)
.addChannelOption((option) =>
option
.setName("join-channel")
.setDescription("Channel for join messages.")
.addChannelTypes(ChannelType.GuildText)
.setRequired(true)
)
.addChannelOption((option) =>
@ -35,17 +39,20 @@ export default {
.setName("leave-channel")
.setDescription("Channel for leave messages.")
.addChannelTypes(ChannelType.GuildText)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("leave-message")
.setDescription("Message for leave messages.")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("join-message")
.setDescription("Message for join messages.")
.setRequired(true)
);
},
execute: async (interaction: ChatInputCommandInteraction) => {
@ -60,121 +67,81 @@ export default {
const joinChannelMessage = options?.getString("join-message");
const leaveChannelMessage = options?.getString("leave-message");
const guildDB = await guildSchema?.findOne({
guildId: guild?.id,
if (!guild) throw new Error("Guild not found");
if (status === null) throw new Error("Status not specified");
if (!joinChannel) throw new Error("Join channel not specified");
if (!joinChannelMessage)
throw new Error("Join channel message not specified");
if (!leaveChannel) throw new Error("Leave channel not specified");
if (!leaveChannelMessage)
throw new Error("Leave channel message not specified");
const createGuild = await prisma.guild.upsert({
where: {
id: guild.id,
},
update: {
welcomeEnabled: status,
welcomeJoinChannelId: joinChannel.id,
welcomeJoinChannelMessage: joinChannelMessage,
welcomeLeaveChannelId: leaveChannel.id,
welcomeLeaveChannelMessage: leaveChannelMessage,
},
create: {
id: guild.id,
welcomeEnabled: status,
welcomeJoinChannelId: joinChannel.id,
welcomeJoinChannelMessage: joinChannelMessage,
welcomeLeaveChannelId: leaveChannel.id,
welcomeLeaveChannelMessage: leaveChannelMessage,
},
});
if (guildDB === null) {
return logger?.silly(`Guild not found in database.`);
logger.silly(createGuild);
const interactionEmbedDisabled = new EmbedBuilder()
.setTitle("[:tools:] Welcome")
.setDescription(
"This module is currently disabled, please enable it to continue."
)
.setColor(successColor)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
if (!createGuild.welcomeEnabled) {
return interaction?.editReply({
embeds: [interactionEmbedDisabled],
});
}
guildDB.welcome.status =
status !== null ? status : guildDB?.welcome?.status;
guildDB.welcome.joinChannel =
joinChannel !== null ? joinChannel.id : guildDB?.welcome?.joinChannel;
guildDB.welcome.leaveChannel =
leaveChannel !== null ? leaveChannel.id : guildDB?.welcome?.leaveChannel;
guildDB.welcome.joinChannelMessage =
joinChannelMessage !== null
? joinChannelMessage
: guildDB?.welcome?.joinChannelMessage;
guildDB.welcome.leaveChannelMessage =
leaveChannelMessage !== null
? leaveChannelMessage
: guildDB?.welcome?.leaveChannelMessage;
await guildDB?.save()?.then(async () => {
logger?.silly(`Guild welcome updated.`);
const interactionEmbedDisabled = new EmbedBuilder()
.setTitle("[:tools:] Welcome")
.setDescription(
"This module is currently disabled, please enable it to continue."
)
.setColor(successColor)
.addFields(
{
name: "🤖 Status",
value: `${guildDB?.points?.status}`,
inline: true,
},
{
name: "📈 Rate",
value: `${guildDB?.points?.rate}`,
inline: true,
},
{
name: "🔨 Minimum Length",
value: `${guildDB?.points?.minimumLength}`,
inline: true,
},
{
name: "⏰ Timeout",
value: `${guildDB?.points?.timeout}`,
inline: true,
}
)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
if (!guildDB?.welcome?.status) {
return interaction?.editReply({
embeds: [interactionEmbedDisabled],
});
}
const interactionEmbed = new EmbedBuilder()
.setTitle("[:tools:] Welcome")
.setDescription(
`The following configuration will be used.
const interactionEmbed = new EmbedBuilder()
.setTitle("[:tools:] Welcome")
.setDescription(
`The following configuration will be used.
[👋] **Welcome**
**Channel**: <#${guildDB?.welcome?.joinChannel}>
**Message**: ${guildDB?.welcome?.joinChannelMessage}
**Channel**: <#${createGuild.welcomeJoinChannelId}>
**Message**: ${createGuild.welcomeJoinChannelMessage}
[🚪] **Leave**
**Channel**: <#${guildDB?.welcome?.leaveChannel}>
**Message**: ${guildDB?.welcome?.leaveChannelMessage}`
)
.setColor(successColor)
.addFields(
{
name: "🤖 Status",
value: `${guildDB?.points?.status}`,
inline: true,
},
{
name: "📈 Rate",
value: `${guildDB?.points?.rate}`,
inline: true,
},
{
name: "🔨 Minimum Length",
value: `${guildDB?.points?.minimumLength}`,
inline: true,
},
{
name: "⏰ Timeout",
value: `${guildDB?.points?.timeout}`,
inline: true,
}
)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
await interaction?.editReply({
embeds: [interactionEmbed],
**Channel**: <#${createGuild.welcomeLeaveChannelId}>
**Message**: ${createGuild.welcomeLeaveChannelMessage}`
)
.setColor(successColor)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
return;
await interaction?.editReply({
embeds: [interactionEmbed],
});
return;
},
};