Clean code without nesting on('collect'

I'm trying to get 5 sequential infos from the user but the only way to do that was nesting a lot of callbacks. Is there another way to do it without nesting and without using forms? Example code:
try {

const cbResourceName = channel.createMessageComponentCollector({ filter: filterResourceName, componentType: ComponentType.StringSelect, time: 300000, max: 1 });

cbResourceName.on('end', collected => {
interaction.deleteReply().catch((e) => {})
});

cbResourceName.on('collect', async (userMsgResourceName) => {

const cbVersion = interaction.channel.createMessageCollector({ filter: filterVersion, max: 1,time: 300000 });

cbVersion.on('end', collected => {
botMsgVersion.delete().catch(e => {});
});

cbVersion.on('collect', async (userVersion) => {

const csChangelog = interaction.channel.createMessageCollector({ filter: filterChangelog, max: 1,time: 300000 });

csChangelog.on('end', collected => {
botMsgChangelog.delete().catch(e => {});
});

csChangelog.on('collect', async (userMsgChangelog) => {

const cbChanges = interaction.channel.createMessageCollector({ filter: filterChanges, max: 1,time: 300000 });

cbChanges.on('end', collected => {
botMsgChanges.delete().catch(e => {});
});

cbChanges.on('collect', async (userMsgChanges) => {

const csImgLink = interaction.channel.createMessageCollector({ filter: filterImgLink, max: 1,time: 300000 });

csImgLink.on('end', collected => {
botMsgImgLink.delete().catch(e => {});
});

csImgLink.on('collect', async (userMsgImgLink) => {

})
})
})
})
})
try {

const cbResourceName = channel.createMessageComponentCollector({ filter: filterResourceName, componentType: ComponentType.StringSelect, time: 300000, max: 1 });

cbResourceName.on('end', collected => {
interaction.deleteReply().catch((e) => {})
});

cbResourceName.on('collect', async (userMsgResourceName) => {

const cbVersion = interaction.channel.createMessageCollector({ filter: filterVersion, max: 1,time: 300000 });

cbVersion.on('end', collected => {
botMsgVersion.delete().catch(e => {});
});

cbVersion.on('collect', async (userVersion) => {

const csChangelog = interaction.channel.createMessageCollector({ filter: filterChangelog, max: 1,time: 300000 });

csChangelog.on('end', collected => {
botMsgChangelog.delete().catch(e => {});
});

csChangelog.on('collect', async (userMsgChangelog) => {

const cbChanges = interaction.channel.createMessageCollector({ filter: filterChanges, max: 1,time: 300000 });

cbChanges.on('end', collected => {
botMsgChanges.delete().catch(e => {});
});

cbChanges.on('collect', async (userMsgChanges) => {

const csImgLink = interaction.channel.createMessageCollector({ filter: filterImgLink, max: 1,time: 300000 });

csImgLink.on('end', collected => {
botMsgImgLink.delete().catch(e => {});
});

csImgLink.on('collect', async (userMsgImgLink) => {

})
})
})
})
})
13 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
space
space2y ago
You can use awaitMessages instead of createMessageCollector https://discord.js.org/#/docs/discord.js/main/class/TextChannel?scrollTo=awaitMessages (works on all text based channels)
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
Even with the awaitMessages it will be nested inside the awaitMessages.then(), no?
space
space2y ago
Can be, you can also use async/await and avoid that.
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
Nice, i'll try
space
space2y ago
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
let userMsgTitle = await channel.awaitMessages({ filter: filterTitle, time: 300000, max: 1 })

userMsgTitle.delete()
let userMsgTitle = await channel.awaitMessages({ filter: filterTitle, time: 300000, max: 1 })

userMsgTitle.delete()
It isnt supposed to delete the message i just sent?
space
space2y ago
You get back a collection of collected messages, that would effectively do nothing.
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
This means there is no way to delete the message?
space
space2y ago
There is You need to grab the collected message (if any) from the collection. There is a handy first method on it. https://discordjs.dev/docs/packages/collection/1.3.0/Collection:Class#first
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
Oh, now returns in a different way than before Sometimes its good to use a brain Thanks god, i'm totally pleased right now
let title = await getTextMessageFromUser(`**Title**`, client, interaction, member, channel);
let version = await getTextMessageFromUser(`**Version**`, client, interaction, member, channel);
let title = await getTextMessageFromUser(`**Title**`, client, interaction, member, channel);
let version = await getTextMessageFromUser(`**Version**`, client, interaction, member, channel);
Lixeiro Charmoso
Lixeiro CharmosoOP2y ago
space
space2y ago
I like the abstraction msthumbsup
Want results from more Discord servers?
Add your server