const { EmbedBuilder, ActionRowBuilder, ModalBuilder, TextInputBuilder, } = require("@discordjs/builders"); const { PermissionsBitField, } = require("discord.js"); const verificationModel = require("../models/verificationModel.js"); const suggestionModel = require("../models/suggestionModel.js"); const {formatResults} = require("../utils/formatResults.js"); const suggestionId = require("../commands/miscellaneous/suggestion.js"); // * From this point onward, the actual code of the event begins ... module.exports = { name: 'interactionCreate', async execute(interaction, client) { if (interaction.isCommand()) { const command = client.commands.get(interaction.commandName); if (! command) return; try { await command.execute(interaction, client); } catch (error) { console.error(error); } } // From now on, check which interaction type it is and perform the written actions accordingly. if (interaction.isButton()) { switch (interaction.customId) { // Verification system case "primaryVerification": { const data = await verificationModel.findOne({ guildId: interaction.guild.id }); if (interaction.member.roles.cache.has(interaction.guild.roles.cache.get(data.role).id)) return interaction.reply({ embeds: [ new EmbedBuilder().setColor(0xffa502).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription("Du hast den ersten Verifizierungsschritt bereits bestanden, daher kannst du ihn nicht erneut durchführen.").setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); let numberOne = Math.floor(Math.random() * (10 - 1)) + 1; let numberTwo = Math.floor(Math.random() * (20 - 10)) + 10; // Define a new modal and then add components to this model. let modal = new ModalBuilder().setCustomId(`primaryVerificationModal,${numberOne},${numberTwo}`).setTitle("Verifizierung (1)"); modal.addComponents(new ActionRowBuilder().addComponents(new TextInputBuilder().setCustomId("input").setStyle(1).setLabel(`Was ist die Summe von ${numberOne} + ${numberTwo}?`).setPlaceholder("Bitte gib die Lösung in dieses Textfeld ein!").setMaxLength(2).setRequired())); await interaction.showModal(modal); } break; // Suggestion system case "upvotes": { const data = await suggestionModel.findOne({ guildId: interaction.guild.id, messageId: interaction.message.id }); if (! data) return; const message = await interaction.channel.messages.fetch(data.messageId); if (data.upmembers.includes(interaction.user.id)) return await interaction.reply({ embeds: [ new EmbedBuilder().setColor(0xff4757).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription("Du hast bereits abgestimmt!").setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); if (data.downmembers.includes(interaction.user.id)) { let downvotes = data.downvotes; downvotes -= 1; } if (data.downmembers.includes(interaction.user.id)) data.downvotes -= 1; data.upmembers.push(interaction.user.id); data.downmembers.pull(interaction.user.id); const newEmbed = new EmbedBuilder().from(message.embeds[0]).setFields({name: `Upvotes`, value: `> **${data.upvotes + 1}** Votes`, inline: true}, { name: `Downvotes`, value: `> **${data.downvotes}** Votes`, inline: true}, { name: `Votes`, value: formatResults(data.upmembers, data.downmembers), inline: true}, {name: `Autor`, value: `> <@${data.messageAuthorId}>`, inline: false}); // Redefine all buttons for the component object. // TODO <:upvote:1165259604368822292>, <:downvote:1165259500178120755>, , const buttonOne = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('upvotes').setEmoji({name:'👍'}).setLabel("Upvote").setStyle(ButtonStyle.Primary), new ButtonBuilder().setCustomId('downvotes').setEmoji({name:'👎'}).setLabel("Downvote").setStyle(ButtonStyle.Primary) ); const buttonTwo = new ActionRowBuilder().addComponents(new ButtonBuilder().setCustomId('votes').setEmoji({name:'💭'}).setLabel('Votes').setStyle(ButtonStyle.Secondary)); const buttonThree = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('approve').setEmoji({name:'✅'}).setLabel('Akzeptieren').setStyle(ButtonStyle.Success), new ButtonBuilder().setCustomId('reject').setEmoji({name:'❌'}).setLabel('Ablehnen').setStyle(ButtonStyle.Danger) ); await interaction.update({ embeds: [newEmbed], components: [buttonOne, buttonTwo, buttonThree] }); data.upvotes++; data.save(); } case "downvotes": { const data = await suggestionModel.findOne({ guildId: interaction.guild.id, messageId: interaction.message.id }); if (! data) return; const message = await interaction.channel.messages.fetch(data.messageId); if (data.downmembers.includes(interaction.user.id)) return await interaction.reply({ embeds: [ new EmbedBuilder().setColor(0xff4757).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription("Du hast bereits abgestimmt!").setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); if (data.upmembers.includes(interaction.user.id)) { let upvotes = data.upvotes; upvotes -= 1; } if (data.upmembers.includes(interaction.user.id)) data.upvotes -= 1; data.downmembers.push(interaction.user.id); data.upmembers.pull(interaction.user.id); const newEmbed = new EmbedBuilder().from(message.embeds[0]).setFields({name: `Upvotes`, value: `> **${data.upvotes + 1}** Votes`, inline: true}, { name: `Downvotes`, value: `> **${data.downvotes}** Votes`, inline: true}, { name: `Votes`, value: formatResults(data.upmembers, data.downmembers), inline: true}, {name: `Autor`, value: `> <@${data.messageAuthorId}>`, inline: false}); // Redefine all buttons for the component object. const buttonOne = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('upvotes').setEmoji({name:'👍'}).setLabel("Upvote").setStyle(ButtonStyle.Primary), new ButtonBuilder().setCustomId('downvotes').setEmoji({name:'👎'}).setLabel("Downvote").setStyle(ButtonStyle.Primary) ); const buttonTwo = new ActionRowBuilder().addComponents(new ButtonBuilder().setCustomId('votes').setEmoji({name:'💭'}).setLabel('Votes').setStyle(ButtonStyle.Secondary)); const buttonThree = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('approve').setEmoji({name:'✅'}).setLabel('Akzeptieren').setStyle(ButtonStyle.Success), new ButtonBuilder().setCustomId('reject').setEmoji({name:'❌'}).setLabel('Ablehnen').setStyle(ButtonStyle.Danger) ); await interaction.update({ embeds: [newEmbed], components: [buttonOne, buttonTwo, buttonThree] }); data.downvotes++; data.save(); } case "votes": { let upvoters = []; await data.upmembers.forEach(async member => { upvoters.push(`<@${member}>`); }); let downvoters = []; await data.downmembers.forEach(async member => { downvoters.push(`<@${member}>`); }); await interaction.reply({ embeds: [ new EmbedBuilder().setColor(0x4834d4).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription(`Up-/Downvotes von ${suggestionId}`).addFields({ name: 'Upvotes', value: `> ${upvoters.join(', ').slice(0, 1020) || "N/A"}`, inline: true }, { name: 'Downvotes', value: `> ${downvoters.join(', ').slice(0, 1020) || "N/A"}`, inline: true }).setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); } case "approve": { const data = await suggestionModel.findOne({ guildId: interaction.guild.id, messageId: interaction.message.id }); if (! data) return; const message = await interaction.channel.messages.fetch(data.messageId); if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) return; const newEmbed = new EmbedBuilder().from(message.embeds[0]).setDescription('* Dein Vorschlag wurde angenommen!*'); await interaction.update({embeds: [newEmbed], components:[message.components[0]]}); } case "reject": { const data = await suggestionModel.findOne({ guildId: interaction.guild.id, messageId: interaction.message.id }); if (! data) return; const message = await interaction.channel.messages.fetch(data.messageId); if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) return; const newEmbed = new EmbedBuilder().from(message.embeds[0]).setDescription('* Dein Vorschlag wurde abgelehnt!*'); await interaction.update({embeds: [newEmbed], components:[message.components[0]]}); } } } if (interaction.isModalSubmit()) { if (interaction.customId?.startsWith("primaryVerificationModal")) { const data = await verificationModel.findOne({ guildId: interaction.guild.id }); let answer = interaction.fields.getTextInputValue("input"); // Get the member's answer from the question and save it as "input". let numberOne = parseInt(interaction.customId.split(`,`)[1]); // Get the first number of the task ... let numberTwo = parseInt(interaction.customId.split(`,`)[2]); // Get the second number of the task ... if (answer == numberOne + numberTwo) { await interaction.member.roles.add(interaction.guild.roles.cache.get(data.role)).catch(() => {}); return await interaction.reply({ embeds: [ new EmbedBuilder().setColor(0x2ed573).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription("Du hast den ersten Verifizierungsschritt erfolgreich bestanden!").setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); } else { return await interaction.reply({ embeds: [ new EmbedBuilder().setColor(0xff4757).setAuthor({ name: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setDescription(`Die Antwort auf die Aufgabe »\`${numberOne} + ${numberTwo}\`«, die du gegeben hast, ist nicht richtig!`).addFields({ name: 'Antwort ()', value: `${answer}`, inline: false }, { name: 'Lösung ()', value: `${numberOne + numberTwo}`, inline: false }).setFooter({ text: '→ Die Lehre der Väter ←', iconURL: client.user.displayAvatarURL({ dynamic: true, format: 'png' }) }).setTimestamp() ], ephemeral: true }); } } } }, };