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
5 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
chewie
chewie16mo ago
interaction.values is for select menus
d.js docs
d.js docs16mo ago
guide Other Interactions: Modals - Extracting data from modal submissions read more
Jannis
JannisOP16mo ago
Should I replace "interaction.values" with "interaction.fields.getTextInputValue"?
chewie
chewie16mo ago
you should follow the guide I sent you, yes
Want results from more Discord servers?
Add your server