xyter/src/commands/config/subcommands/welcome/index.ts
Vermium Sifell 109b89c820 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
2022-12-19 20:44:17 +01:00

146 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
ChannelType,
ChatInputCommandInteraction,
EmbedBuilder,
PermissionsBitField,
SlashCommandSubcommandBuilder,
} from "discord.js";
import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply";
import checkPermission from "../../../../helpers/checkPermission";
import getEmbedConfig from "../../../../helpers/getEmbedData";
import logger from "../../../../middlewares/logger";
export const builder = (command: SlashCommandSubcommandBuilder) => {
return command
.setName("welcome")
.setDescription("Welcome")
.addBooleanOption((option) =>
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) =>
option
.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)
);
};
export const execute = async (interaction: ChatInputCommandInteraction) => {
await deferReply(interaction, true);
checkPermission(interaction, PermissionsBitField.Flags.ManageGuild);
const { successColor, footerText, footerIcon } = await getEmbedConfig(
interaction.guild
);
const { options, guild } = interaction;
const status = options?.getBoolean("status");
const joinChannel = options?.getChannel("join-channel");
const leaveChannel = options?.getChannel("leave-channel");
const joinChannelMessage = options?.getString("join-message");
const leaveChannelMessage = options?.getString("leave-message");
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.guildConfigWelcome.upsert({
where: {
id: guild.id,
},
update: {
status,
joinChannelId: joinChannel.id,
joinChannelMessage,
leaveChannelId: leaveChannel.id,
leaveChannelMessage,
},
create: {
id: guild.id,
status,
joinChannelId: joinChannel.id,
joinChannelMessage,
leaveChannelId: leaveChannel.id,
leaveChannelMessage,
},
});
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.status) {
return interaction?.editReply({
embeds: [interactionEmbedDisabled],
});
}
const interactionEmbed = new EmbedBuilder()
.setTitle("[:tools:] Welcome")
.setDescription(
`The following configuration will be used.
[👋] **Welcome**
**Channel**: <#${createGuild.joinChannelId}>
**Message**: ${createGuild.joinChannelMessage}
[🚪] **Leave**
**Channel**: <#${createGuild.leaveChannelId}>
**Message**: ${createGuild.leaveChannelMessage}`
)
.setColor(successColor)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
await interaction?.editReply({
embeds: [interactionEmbed],
});
return true;
};