interaction listeners make my brain hurt

Sending info under:
5 Replies
d.js toolkit
d.js toolkit3w 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
DoctorNobody
DoctorNobodyOP3w ago
my voice channel code and my welcome embed (and application code seem to be crossing streams in terms of event listeners. the specifics are that when i click a button under that embed it opens a modal, and when it gets submitted its calling this function in my voiceChannel.js:
// Handle modal interactions for name and limit
client.on('interactionCreate', async (interaction) => {
if (!interaction.isModalSubmit()) return;

const { customId, fields, member, channel } = interaction;
const userId = member.id;
const channelData = createdVoiceChannels.get(channel.id);

if (!channelData || channelData.ownerId !== userId) {
return interaction.reply({ content: 'You are not the owner of this channel.', flags: 64 });
}

if (customId === 'name_modal') {
const newName = fields.getTextInputValue('new_name');
await channel.setName(newName);
await interaction.reply(`The channel name has been updated to: ${newName}`, { flags: 64 });
} else if (customId === 'limit_modal') {
const newLimit = parseInt(fields.getTextInputValue('new_limit'));
if (isNaN(newLimit) || newLimit < 0) {
return interaction.reply({ content: 'Please provide a valid number for the user limit.', flags: 64 });
}
await channel.setUserLimit(newLimit);
await interaction.reply(`The user limit has been updated to: ${newLimit}`, { flags: 64 });
}
});
// Handle modal interactions for name and limit
client.on('interactionCreate', async (interaction) => {
if (!interaction.isModalSubmit()) return;

const { customId, fields, member, channel } = interaction;
const userId = member.id;
const channelData = createdVoiceChannels.get(channel.id);

if (!channelData || channelData.ownerId !== userId) {
return interaction.reply({ content: 'You are not the owner of this channel.', flags: 64 });
}

if (customId === 'name_modal') {
const newName = fields.getTextInputValue('new_name');
await channel.setName(newName);
await interaction.reply(`The channel name has been updated to: ${newName}`, { flags: 64 });
} else if (customId === 'limit_modal') {
const newLimit = parseInt(fields.getTextInputValue('new_limit'));
if (isNaN(newLimit) || newLimit < 0) {
return interaction.reply({ content: 'Please provide a valid number for the user limit.', flags: 64 });
}
await channel.setUserLimit(newLimit);
await interaction.reply(`The user limit has been updated to: ${newLimit}`, { flags: 64 });
}
});
instead of the modal interaction handling in my welcomeSignUp.js, for some ridiculous reason i figured they would be able to function simultaneously and i could keep the modular nature of the code. will i need to create a single function for modal handling for both the files or is there some way to have it only handle relevant events? bit new to this sorry.
No description
No description
DoctorNobody
DoctorNobodyOP3w ago
again i do apologize if this is a dumb question. we just be trying our best out here
duck
duck3w ago
neither discord nor djs has anything to determine what's considered a "relevant event" it does not track where in your code the event listener was added the only thing that would distinguish this code from another interactionCreate listener is the if statement if (customId === 'name_modal') { which seems to be after the code you're expecting not to execute here in general we suggest having one listener per event, then delegating work from there this would avoid any confusion about what event listener handles what
DoctorNobody
DoctorNobodyOP3w ago
😭 time to de-pretty my code. thanks 🫂

Did you find this page helpful?