How do I wait for a Collector to finish before executing lines below?

The issue is that "finish" (at the bottom of the code) is being logged before the collector finishes. How do I make the program wait for the collector to finish before continuing?
let comArr = [`slice`, `call`, `walk`];
let actionVal = 0;

await msg.edit({ ephemeral: false, embeds: [embed], components: [buttonArr] });

let filter = m => comArr.includes(m.customId) && m.user.id == message.author.id;
let collector = await message.channel.createMessageComponentCollector({ componentType: Discord.ComponentType.Button, filter, time: 15000 });

collector.on('collect', async i => {
await i.deferUpdate();
await n.sleep(50);
console.log(i.customId)
if (i.customId == `slice`) {
actionVal = 1;
collector.emit('end', collected => { });
} else if (i.customId == `call`) {
actionVal = 2;
collector.emit('end', collected => { });
} else if (i.customId == `walk`) {
actionVal = 3;
collector.emit('end', collected => { });
} else {
collector.emit('end', collected => { });
}

});

// end collector event
collector.on('end', async collected => {
console.log(actionVal)

let embed3 = new Discord.EmbedBuilder()
.setColor(`${n.color()}`)
.setTitle(`test`)
});

await msg.edit({ ephemeral: false, embeds: [embed3], components: [] });
});

await n.sleep(5000);
console.log(`finish`)
let comArr = [`slice`, `call`, `walk`];
let actionVal = 0;

await msg.edit({ ephemeral: false, embeds: [embed], components: [buttonArr] });

let filter = m => comArr.includes(m.customId) && m.user.id == message.author.id;
let collector = await message.channel.createMessageComponentCollector({ componentType: Discord.ComponentType.Button, filter, time: 15000 });

collector.on('collect', async i => {
await i.deferUpdate();
await n.sleep(50);
console.log(i.customId)
if (i.customId == `slice`) {
actionVal = 1;
collector.emit('end', collected => { });
} else if (i.customId == `call`) {
actionVal = 2;
collector.emit('end', collected => { });
} else if (i.customId == `walk`) {
actionVal = 3;
collector.emit('end', collected => { });
} else {
collector.emit('end', collected => { });
}

});

// end collector event
collector.on('end', async collected => {
console.log(actionVal)

let embed3 = new Discord.EmbedBuilder()
.setColor(`${n.color()}`)
.setTitle(`test`)
});

await msg.edit({ ephemeral: false, embeds: [embed3], components: [] });
});

await n.sleep(5000);
console.log(`finish`)
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
method Message#awaitMessageComponent() Collects a single component interaction that passes the filter. The Promise will reject if the time expires.
Want results from more Discord servers?
Add your server