refactor(cmd/administration): adding the tsconfig + eslint correction

This commit is contained in:
Raphael 2025-10-14 00:17:23 +02:00 committed by Raphaël
parent e911f3b2e0
commit 8d41ef8f7a
4 changed files with 434 additions and 273 deletions

View file

@ -1,11 +1,10 @@
import { ActionRowBuilder, SlashCommandBuilder } from '@discordjs/builders';
import {
SlashCommandBuilder,
ChatInputCommandInteraction,
EmbedBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChatInputCommandInteraction,
ComponentType,
EmbedBuilder,
Interaction,
MessageFlags,
ModalBuilder,
TextInputBuilder,
@ -43,22 +42,16 @@ function getPlaceholdersEmbed(guildData: GuildPrisma): EmbedBuilder {
.setColor(guildData.color);
}
export const data = new SlashCommandBuilder()
.setName('join')
.setDescription('Configure welcome and leave messages');
export async function execute(interaction: ChatInputCommandInteraction) {
const guildId = interaction.guildId!;
let guildData = await prisma.guild.findUnique({ where: { id: guildId } });
if (!guildData) {
guildData = await prisma.guild.create({ data: { id: guildId } });
}
const mainRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
function createMainRow(
guildData: GuildPrisma,
): ActionRowBuilder<ButtonBuilder> {
return new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId('toggle_welcome')
.setLabel(guildData.welcomeEnabled ? 'Disable welcome' : 'Enable welcome')
.setStyle(guildData.welcomeEnabled ? ButtonStyle.Danger : ButtonStyle.Success),
.setStyle(
guildData.welcomeEnabled ? ButtonStyle.Danger : ButtonStyle.Success,
),
new ButtonBuilder()
.setCustomId('edit_welcome')
.setLabel('Edit welcome message')
@ -66,7 +59,9 @@ export async function execute(interaction: ChatInputCommandInteraction) {
new ButtonBuilder()
.setCustomId('toggle_leave')
.setLabel(guildData.leaveEnabled ? 'Disable leave' : 'Enable leave')
.setStyle(guildData.leaveEnabled ? ButtonStyle.Danger : ButtonStyle.Success),
.setStyle(
guildData.leaveEnabled ? ButtonStyle.Danger : ButtonStyle.Success,
),
new ButtonBuilder()
.setCustomId('edit_leave')
.setLabel('Edit leave message')
@ -77,132 +72,188 @@ export async function execute(interaction: ChatInputCommandInteraction) {
.setEmoji('')
.setStyle(ButtonStyle.Secondary),
);
}
export const data = new SlashCommandBuilder()
.setName('join')
.setDescription('Configure welcome and leave messages');
export async function execute(interaction: ChatInputCommandInteraction) {
const guildId = interaction.guildId;
if (!guildId) {
await interaction.reply({
content: '❌ This command can only be used in a guild.',
flags: MessageFlags.Ephemeral,
});
return;
}
let currentGuildData: GuildPrisma | null = await prisma.guild.findUnique({
where: { id: guildId },
});
if (!currentGuildData) {
currentGuildData = await prisma.guild.create({ data: { id: guildId } });
}
const mainRow : ActionRowBuilder = createMainRow(currentGuildData);
await interaction.reply({
embeds: [getEmbed(guildData)],
embeds: [getEmbed(currentGuildData)],
components: [mainRow],
flags: MessageFlags.Ephemeral,
});
const collector = interaction.channel!.createMessageComponentCollector({
const collector = interaction.channel?.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 60_000,
});
collector.on('collect', async (i) => {
if (i.user.id !== interaction.user.id) {
return i.reply({
content: '❌ You are not allowed to use this panel.',
ephemeral: true,
});
}
if (!collector) {
return;
}
// Show placeholders
if (i.customId === 'placeholders') {
const backRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId('back_to_main')
.setLabel('↩️ Back')
.setStyle(ButtonStyle.Secondary),
);
await interaction.editReply({
embeds: [getPlaceholdersEmbed(guildData)],
components: [backRow],
});
return;
}
collector.on('collect', (i) => {
void (async () => {
if (i.user.id !== interaction.user.id) {
await i.reply({
content: '❌ You are not allowed to use this panel.',
flags: MessageFlags.Ephemeral,
});
return;
}
// ↩️ Back to main panel
if (i.customId === 'back_to_main') {
await interaction.editReply({
embeds: [getEmbed(guildData)],
components: [mainRow],
});
return;
}
// 🟢 Toggle welcome
if (i.customId === 'toggle_welcome') {
guildData = await prisma.guild.update({
where: { id: guildId },
data: { welcomeEnabled: !guildData.welcomeEnabled },
});
await interaction.editReply({
embeds: [getEmbed(guildData)],
components: [mainRow],
});
return;
}
// 🟢 Toggle leave
if (i.customId === 'toggle_leave') {
guildData = await prisma.guild.update({
where: { id: guildId },
data: { leaveEnabled: !guildData.leaveEnabled },
});
await interaction.editReply({
embeds: [getEmbed(guildData)],
components: [mainRow],
});
return;
}
// ✏️ Open modal for editing messages
if (i.customId === 'edit_welcome' || i.customId === 'edit_leave') {
const modal = new ModalBuilder()
.setCustomId(i.customId + '_modal')
.setTitle('Edit Message');
const input = new TextInputBuilder()
.setCustomId('message')
.setLabel('Message content (placeholders allowed)')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('Ex: Welcome {user.mention} to {server.name}!')
.setValue(
i.customId === 'edit_welcome'
? guildData.welcomeMessage || ''
: guildData.leaveMessage || '',
// Show placeholders
if (i.customId === 'placeholders') {
const backRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId('back_to_main')
.setLabel('↩️ Back')
.setStyle(ButtonStyle.Secondary),
);
await interaction.editReply({
embeds: [getPlaceholdersEmbed(currentGuildData)],
components: [backRow],
});
return;
}
modal.addComponents(new ActionRowBuilder<TextInputBuilder>().addComponents(input));
await i.showModal(modal);
}
// ↩️ Back to main panel
if (i.customId === 'back_to_main') {
await interaction.editReply({
embeds: [getEmbed(currentGuildData)],
components: [createMainRow(currentGuildData)],
});
return;
}
// 🟢 Toggle welcome
if (i.customId === 'toggle_welcome') {
const updatedGuildData = await prisma.guild.update({
where: { id: guildId },
data: { welcomeEnabled: !currentGuildData.welcomeEnabled },
});
currentGuildData = updatedGuildData;
const newMainRow = createMainRow(updatedGuildData);
await interaction.editReply({
embeds: [getEmbed(updatedGuildData)],
components: [newMainRow],
});
return;
}
// 🟢 Toggle leave
if (i.customId === 'toggle_leave') {
const updatedGuildData = await prisma.guild.update({
where: { id: guildId },
data: { leaveEnabled: !currentGuildData.leaveEnabled },
});
currentGuildData = updatedGuildData;
const newMainRow = createMainRow(updatedGuildData);
await interaction.editReply({
embeds: [getEmbed(updatedGuildData)],
components: [newMainRow],
});
return;
}
// ✏️ Open modal for editing messages
if (i.customId === 'edit_welcome' || i.customId === 'edit_leave') {
const modal = new ModalBuilder()
.setCustomId(`${i.customId}_modal`)
.setTitle('Edit Message');
const input = new TextInputBuilder()
.setCustomId('message')
.setLabel({ name: 'Message content (placeholders allowed)' })
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('Ex: Welcome {user.mention} to {server.name}!')
.setValue(
i.customId === 'edit_welcome'
? currentGuildData.welcomeMessage || ''
: currentGuildData.leaveMessage || '',
);
modal.addComponents(
new ActionRowBuilder<TextInputBuilder>().addComponents(input),
);
await i.showModal(modal);
}
})();
});
// Handle modal submissions
interaction.client.on('interactionCreate', async (modalInt) => {
if (!modalInt.isModalSubmit()) return;
const modalHandler = (modalInt: Interaction) => {
void (async () => {
if (
!modalInt.isModalSubmit() ||
modalInt.user.id !== interaction.user.id
) {
return;
}
if (modalInt.customId === 'edit_welcome_modal') {
const msg = modalInt.fields.getTextInputValue('message');
guildData = await prisma.guild.update({
where: { id: guildId },
data: { welcomeMessage: msg },
});
await modalInt.reply({
content: '✅ | Welcome message updated!',
ephemeral: true,
});
await interaction.editReply({
embeds: [getEmbed(guildData)],
components: [mainRow],
});
}
const typedModalInt = modalInt;
if (modalInt.customId === 'edit_leave_modal') {
const msg = modalInt.fields.getTextInputValue('message');
guildData = await prisma.guild.update({
where: { id: guildId },
data: { leaveMessage: msg },
});
await modalInt.reply({
content: '✅ | Leave message updated!',
ephemeral: true,
});
await interaction.editReply({
embeds: [getEmbed(guildData)],
components: [mainRow],
});
}
if (typedModalInt.customId === 'edit_welcome_modal') {
const msg = typedModalInt.fields.getTextInputValue('message');
const updatedGuildData = await prisma.guild.update({
where: { id: guildId },
data: { welcomeMessage: msg },
});
currentGuildData = updatedGuildData;
await typedModalInt.reply({
content: '✅ | Welcome message updated!',
ephemeral: true,
});
const newMainRow = createMainRow(updatedGuildData);
await interaction.editReply({
embeds: [getEmbed(updatedGuildData)],
components: [newMainRow],
});
}
if (typedModalInt.customId === 'edit_leave_modal') {
const msg = typedModalInt.fields.getTextInputValue('message');
const updatedGuildData = await prisma.guild.update({
where: { id: guildId },
data: { leaveMessage: msg },
});
currentGuildData = updatedGuildData;
await typedModalInt.reply({
content: '✅ | Leave message updated!',
ephemeral: true,
});
const newMainRow = createMainRow(updatedGuildData);
await interaction.editReply({
embeds: [getEmbed(updatedGuildData)],
components: [newMainRow],
});
}
})();
};
interaction.client.on('interactionCreate', modalHandler);
collector.on('end', () => {
interaction.client.off('interactionCreate', modalHandler);
});
}