jannisccc
DIAdiscord.js - Imagine an app
•Created by jannisccc on 9/16/2023 in #djs-questions
Model to Webhook
Im exploring Modals atm, the idea of it is a user fills out a simple 1 question form and that info gets sent to make.com via a webhook containing the initial button ID (which already works) and the user input which for some reason doesnt work. ive been trying to get this right for 2 hours now (was trying to get help by bing) but no success as of now. Also the initial button ID is revision-[internal ID]
const { Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, Events } = require('discord.js14');
const axios = require('axios');
const webhookUrl = 'https://hook.eu1.make.com/[Webhook ID]';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const userButtonIds = new Map(); // Map to store the clicked button's ID for each user
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
// Check if the interaction is from the specific server
if (interaction.guild.id !== '[Guild ID]') return;
if (interaction.isButton()) {
console.log(`Received button interaction from ${interaction.user.tag}`);
if (interaction.customId.startsWith('revision-')) {
// Store the clicked button's ID for this user
const clickedButtonId = interaction.customId.replace('revision-', ''); // Remove 'revision-' from the ID
userButtonIds.set(interaction.user.id, clickedButtonId);
const revisionInput = new TextInputBuilder()
.setCustomId('revisionInput-' + clickedButtonId) // Append the clickedButtonId
.setLabel('Explain what you would like to have revised.')
.setPlaceholder('Tip: You can link images in this field aswell')
.setStyle(TextInputStyle.Paragraph);
const actionRow = new ActionRowBuilder().addComponents(revisionInput);
const modal = new ModalBuilder()
.setCustomId('revisionModal-' + clickedButtonId) // Append the clickedButtonId
.setTitle('Revision Request')
.addComponents(actionRow);
console.log(`Showing modal to user`);
await interaction.showModal(modal);
}
} else if (interaction.isModalSubmit()) {
// Get the clicked button's ID for this user
const clickedButtonId = userButtonIds.get(interaction.user.id);
if (interaction.customId === 'revisionModal-' + clickedButtonId) { // Dynamic custom ID
console.log(`Received modal submission from ${interaction.user.tag}`);
let revisionRequest = '';
if (interaction.values) {
revisionRequest = interaction.values.get('revisionInput-' + clickedButtonId); // Append the clickedButtonId
console.log(`User's revision request: ${revisionRequest}`);
} else {
console.log(`User didn't enter any text in the modal.`);
}
// Concatenate revisionRequest and clickedButtonId
const combinedText = revisionRequest + ' ' + clickedButtonId;
// Send the combined text to the webhook
const payload = {
VideoID: clickedButtonId,
revisionRequest: combinedText,
};
try {
await axios.post(webhookUrl, payload);
console.log('Combined text sent to webhook successfully.');
} catch (error) {
console.error('Failed to send combined text to webhook:', error);
}
// Remove the clicked button's ID for this user
userButtonIds.delete(interaction.user.id);
}
}
});
console.log(`Logging in with bot token`);
client.login('[Token]'); // Bot token included in file
const { Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, Events } = require('discord.js14');
const axios = require('axios');
const webhookUrl = 'https://hook.eu1.make.com/[Webhook ID]';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const userButtonIds = new Map(); // Map to store the clicked button's ID for each user
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
// Check if the interaction is from the specific server
if (interaction.guild.id !== '[Guild ID]') return;
if (interaction.isButton()) {
console.log(`Received button interaction from ${interaction.user.tag}`);
if (interaction.customId.startsWith('revision-')) {
// Store the clicked button's ID for this user
const clickedButtonId = interaction.customId.replace('revision-', ''); // Remove 'revision-' from the ID
userButtonIds.set(interaction.user.id, clickedButtonId);
const revisionInput = new TextInputBuilder()
.setCustomId('revisionInput-' + clickedButtonId) // Append the clickedButtonId
.setLabel('Explain what you would like to have revised.')
.setPlaceholder('Tip: You can link images in this field aswell')
.setStyle(TextInputStyle.Paragraph);
const actionRow = new ActionRowBuilder().addComponents(revisionInput);
const modal = new ModalBuilder()
.setCustomId('revisionModal-' + clickedButtonId) // Append the clickedButtonId
.setTitle('Revision Request')
.addComponents(actionRow);
console.log(`Showing modal to user`);
await interaction.showModal(modal);
}
} else if (interaction.isModalSubmit()) {
// Get the clicked button's ID for this user
const clickedButtonId = userButtonIds.get(interaction.user.id);
if (interaction.customId === 'revisionModal-' + clickedButtonId) { // Dynamic custom ID
console.log(`Received modal submission from ${interaction.user.tag}`);
let revisionRequest = '';
if (interaction.values) {
revisionRequest = interaction.values.get('revisionInput-' + clickedButtonId); // Append the clickedButtonId
console.log(`User's revision request: ${revisionRequest}`);
} else {
console.log(`User didn't enter any text in the modal.`);
}
// Concatenate revisionRequest and clickedButtonId
const combinedText = revisionRequest + ' ' + clickedButtonId;
// Send the combined text to the webhook
const payload = {
VideoID: clickedButtonId,
revisionRequest: combinedText,
};
try {
await axios.post(webhookUrl, payload);
console.log('Combined text sent to webhook successfully.');
} catch (error) {
console.error('Failed to send combined text to webhook:', error);
}
// Remove the clicked button's ID for this user
userButtonIds.delete(interaction.user.id);
}
}
});
console.log(`Logging in with bot token`);
client.login('[Token]'); // Bot token included in file
6 replies