const { SlashCommandBuilder, PermissionFlagsBits, ChannelType, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ComponentType, ChannelSelectMenuBuilder, } = require("discord.js"); const logSchema = require("../../../schema.js/logSchema"); const aConfig = require("../../../logConfig.json"); module.exports = { data: new SlashCommandBuilder() .setName("log-system") .setDescription("settate i log per il server") .addSubcommand((s) => s .setName("configura") .setDescription("Configurazione del log del server usa gli webhook") ) .addSubcommand((s) => s.setName("remove").setDescription("rimuove i log dal server") ), userPermissions: [PermissionFlagsBits.ViewAuditLog], botPermissions: [PermissionFlagsBits.ViewAuditLog], async execute(interaction, client) { const { options, guildId, channel, guild } = interaction; const sub = options.getSubcommand(); if (!["configura", "remove"].includes(sub)) return; const rEmbed = new EmbedBuilder().setFooter({ iconURL: `${client.user.displayAvatarURL({ dynamic: true })}`, text: `${client.user.username} - Logging System`, }); switch (sub) { case "configura": let data = await logSchema.findOne({ GuildID: guildId }); let response; if (!data) { rEmbed .setColor("Red") .setDescription("`Nuovo server: configurazione dei log system`"); response = await interaction.reply({ embeds: [rEmbed], fetchReply: true, ephemeral: true, }); data = new logSchema({ GuildID: guildId, Webhooks: [], ChannelLogs: [], GuildLogs: [], MemberLogs: [], IntegrationLogs: [], OtherLogs: [], }); data.save(); } else { response = await interaction.deferReply({ fetchReply: true, ephemeral: true, }); } const choices = aConfig.auditLogList; const TypeSSM = new ActionRowBuilder().setComponents( new StringSelectMenuBuilder({ options: choices .slice(0, 14) .map((choice) => ({ label: choice.event, value: choice.event })), }) .setCustomId("TypeSSM") .setPlaceholder("selezionare il log che vuoi attivare") .setMaxValues(14) ); const logChannel = new ActionRowBuilder().setComponents( new ChannelSelectMenuBuilder() .setCustomId("logChannel") .setPlaceholder("in quale canale") .setMaxValues(1) .setDefaultChannels(channel.id) .setChannelTypes(ChannelType.GuildText) ); const continueBtn = new ActionRowBuilder().setComponents( new ButtonBuilder() .setCustomId("logContinue") .setLabel("salva & continua") .setStyle(ButtonStyle.Success) ); rEmbed .setColor("Red") .setTitle("canali & messaggi (1/5)") .setDescription( `\`\` quale tipo di log di \`canali & messaggi\`? Perfavore selezionare il menu qui sotto\n\nfai il \`/log-system configura\`commando` ) .setFields( { name: "Log Channel", value: `<#${channel.id}> \`(canale)\``, inline: true, }, { name: "Typo di log", value: `\`no log attivi\``, inline: true } ); interaction.editReply({ embeds: [rEmbed], components: [TypeSSM, logChannel, continueBtn], }); let time = 30_000; const SSMCollector = response.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time, }); const CSMCollector = response.createMessageComponentCollector({ componentType: ComponentType.ChannelSelect, time, }); const BTNCollector = response.createMessageComponentCollector({ componentType: ComponentType.Button, time, }); SSMCollector.on("collect", async (i) => { rEmbed.data.fields[1].value = `\`${i.values.sort().join("\` \`")}\``; let enabledType = []; i.component.options.forEach((o) => { if (i.values.includes(o.value)) { o = new StringSelectMenuOptionBuilder({ label: o.label, value: o.value, default: true, }); } else { o = new StringSelectMenuOptionBuilder({ label: o.label, value: o.value, default: false, }); } enabledType.push(o); }); TypeSSM.components[0].options = enabledType.slice( -i.component.options.length ); interaction.editReply({ embeds: [rEmbed], components: [TypeSSM, logChannel, continueBtn], }); i.reply({ content: `Aggiornamento dei tipologie in: \`${i.values .sort() .join("\` \`")}\``, ephemeral: true, }); SSMCollector.resetTimer(); CSMCollector.resetTimer(); BTNCollector.resetTimer(); }); CSMCollector.on("collect", async (i) => { channel.id === i.values[0] ? rEmbed.data.fields[0].value = `<#${channel}> \`(nel canale)\`` : rEmbed.data.fields[0].value = `<#${i.values[0]}>` await interaction.editReply({ embeds: [rEmbed], components: [TypeSSM, logChannel, continueBtn], }); i.reply({ content: `Aggiorname la chat dei log in: <#${i.values[0]}>`, ephemeral: true, }); SSMCollector.resetTimer(); CSMCollector.resetTimer(); BTNCollector.resetTimer(); }); let buttonClicks = 0; let webhookDatabase = []; BTNCollector.on("collect", async (i) => { await i.deferReply({ ephemeral: true }); const Type = i.message.components[0].components[0].data.options; const ChannelId = i.message.embeds[0].fields[0].value .split(" ")[0] .slice(2, -1); const logChannel = guild.channels.cache.get(ChannelId); const channelWebhook = await logChannel.fetchWebhooks(); const clientWebhook = channelWebhook.filter( (w) => w.name === client.user.username ); let webhookData; if (clientWebhook.size < 1) { const newWebhook = await logChannel.createWebhook({ name: client.user.username, avatar: client.user.displayAvatarURL({ dynamic: true }), }); webhookData = { channelId: newWebhook.channelId, token: newWebhook.token, }; } else { webhookData = { channelId: clientWebhook.first().channelId, token: clientWebhook.first().token, }; } webhookDatabase.push(webhookData); let Database = []; Type.forEach((audiData) => { if (audiData.default === true) { audiData = { name: audiData.value, enabled: true }; } else { audiData = { name: audiData.value, enabled: false }; } Database.push(audiData); }); let newOptions = []; let indexStart; let indexEnd; let Category; buttonClicks++; switch (buttonClicks) { case 1: Category = "Emoji, events, stickers & roles"; indexStart = 14; indexEnd = 26; await logSchema.findOneAndUpdate( { GuildID: guildId }, { Webhooks: webhookDatabase, ChannelLogs: Database } ); break; case 2: Category = "Guild members"; indexStart = 26; indexEnd = 37; await logSchema.findOneAndUpdate( { GuildID: guildId }, { Webhooks: webhookDatabase, GuildLogs: Database } ); break; case 3: Category = "Integrations & automod"; indexStart = 37; indexEnd = 51; await logSchema.findOneAndUpdate( { GuildID: guildId }, { Webhooks: webhookDatabase, MemberLogs: Database } ); break; case 4: Category = "Other audit types"; indexStart = 51; indexEnd = 60; await logSchema.findOneAndUpdate( { GuildID: guildId }, { Webhooks: webhookDatabase, IntegrationLogs: Database } ); break; case 5: const upData = await logSchema.findOneAndUpdate( { GuildID: guildId }, { Webhooks: webhookDatabase, OtherLogs: Database }, { returnNewDocument: true } ); const sEmbed = new EmbedBuilder() .setColor("Red") .setDescription("Settaggio la chat per webhooks ...") .setFooter({ iconURL: `${client.user.displayAvatarURL({ dynamic: true })}`, text: `${client.user.username} - Log system`, }); await interaction.editReply({ embeds: [sEmbed], components: [] }); i.editReply({ contnt: `Salva tipologia e canale logging` }); const guildWebhooks = await guild.fetchWebhooks(); const clientGuildWb = guildWebhooks.filter( (w) => w.name === client.user.username ); let existingWbChannel = clientGuildWb.map( (webhook) => webhook.channelId ); let upWbChannel = []; upData.Webhooks.forEach((webData) => { upWbChannel.push(webData.channelId); }); for (let x = 0; x < existingWbChannel.length; x++) { if (upWbChannel.includes(existingWbChannel[x])) continue; const removeChannel = guild.channels.cache.get( existingWbChannel[x] ); const removeChannelWebhook = await removeChannel.fetchWebhooks(); const removeWebhook = removeChannelWebhook.filter( (w) => w.name === client.user.username ); await removeWebhook.first().delete(); } sEmbed .setColor("Green") .setDescription( "`:white_check_mark:` Settaggio del webhook con sucesso." ); return interaction.editReply({ embeds: [sEmbed] }); } rEmbed .setTitle(`${Category} (${buttonClicks + 1}/5)`) .setColor("Red") .setDescription( `\`\` quale tipo di log di \`${Category}\`? Perfavore selezionare il menu qui sotto\n\nfai il \`/log-system configura\`commando` ) .setFields( { name: "Log Channel", value: `-`, inline: true, }, { name: "Typo di log", value: `\`no log attivi\``, inline: true } ); choices .slice(indexStart, indexEnd) .map((choice) => ({ label: choice.event, value: choice.event })) .forEach((o) => { o = new StringSelectMenuOptionBuilder({ label: o.label, value: o.value, default: false, }); newOptions.push(o); }); channel.id === ChannelId ? rEmbed.data.fields[0].value = `<#${channel.id}>\`(canale)\`` : rEmbed.data.fields[0].value = `<#${ChannelId}>`; TypeSSM.components[0].options = newOptions; TypeSSM.components[0].data.max_values = indexEnd - indexStart; await interaction.editReply({ embeds: [rEmbed], components: [TypeSSM, logChannel, continueBtn], ephemeral: true }); i.editReply({ content: "Salva i log type e chat log" }); SSMCollector.resetTimer(); CSMCollector.resetTimer(); BTNCollector.resetTimer(); }); SSMCollector.on("end", async () => { if (buttonClicks === 5) return; const tEmbed = new EmbedBuilder() .setColor("DarkRed") .setDescription( "la configurazione si รจ fermata\n\nAvvia: /log-system configura" ) .setFooter({ iconURL: `${client.user.displayAvatarURL({ dynamic: true })}`, text: `${client.user.username} - Log system`, }); await interaction.editReply({ embeds: [tEmbed], components: [] }); }); break; case "remove": await interaction.deferReply({ ephemeral: true }); const removed = await logSchema.findOneAndDelete({ GuildID: guildId }); if (removed) { const Guild = await guild.fetchWebhooks(); const ClientWebhooks = Guild.filter( (w) => w.name === client.user.username ); ClientWebhooks.each(async (webhook) => { await webhook.delete(); }); rEmbed .setColor("Green") .setDescription( "`:whrite_check_mark:` remozione dei log con sucesso." ); } else { rEmbed .setColor("Red") .setDescription( "Nel server non ci sono settati i log system\n\nEsegui questo command `/log-system configura`" ); } return interaction.editReply({ embeds: [rEmbed] }); } }, };