I'm trying to write a Discord.JS bot that lists and removes specific channels/threads using MongoDB's Model and Schema functionality. I've gotten everything else figured out, the actual message deletion, channel deletion, and everything else I needed for the remove function, but for some reason prompting MongoDB to delete the ChatListing schema using ids specified doesn't work.
case 'remove':
modal.setTitle('Remove a Listing');
const listingIDInput = new TextInputBuilder()
.setCustomId('listingIDInput')
.setLabel(`What's the ID of your listing?`)
.setPlaceholder('EX... 14309')
.setMinLength(5)
.setStyle(TextInputStyle.Short)
.setRequired(true);
const rmrow = new ActionRowBuilder().addComponents(listingIDInput);
modal.addComponents(rmrow);
await interaction.showModal(modal);
try {
await interaction.awaitModalSubmit({ time: 120_000 }).then( (interaction) => {
const listingToRemove = interaction.fields.getTextInputValue('listingIDInput');
ChatListing.findOne({ GuildID: guild.id, ListingID: listingToRemove }, async(err, data) =>{
if(err) throw err;
if(!data) return;
if(data.MemberID == member.id) {
const channel = await guild.channels.cache.get(data.Channel);
const msg = await channel.messages.fetch(data.MessageID);
msg.delete();
var id = data._id;
ChatListing.findByIdAndDelete({_id: mongoose.Types.ObjectId(id)});
embed.setTitle('Listing successfully removed.')
.setColor('Green')
.setDescription('⚠️ | Your chat listing has been removed successufully. We\'re sorry to see it go! | ⚠️')
.setTimestamp();
await interaction.reply({ embeds: [embed], ephemeral: true });
} else {
embed.setTitle('You aren\'t the owner of the listing!')
.setColor('Red')
.setDescription('You aren\'t capable of removing this listing because you aren\'t the user that posted it.')
.setTimestamp();
await interaction.reply({ embeds: [embed], ephemeral: true });
}
});
});
} catch (err) {
console.error(err);
}
break;
This is just the snippet in the switch case used for the slash command I've built around this functionality, and the case for listing removal.
It doesn't cause any errors in the console, however when I check the database, the test listing I put up is still there and doesn't seem to go.
Is there anything I'm doing wrong? Wherever I've looked I can't quite seem to find anything that solves this problem for me. Is the reason it's not working because it's listed within a ChatListing.findOne() function? If so, how can I modify it to work outside of that function and still keep the removal functionality?
CodePudding user response:
Try using findOneAndDelete
and use the returned Promise to handle success or failure:
ChatListing.findOneAndDelete({_id: mongoose.Types.ObjectId(id)})
.then(() => {
embed.setTitle('Listing successfully removed.')
.setColor('Green')
.setDescription('⚠️ | Your chat listing has been removed successufully. We\'re sorry to see it go! | ⚠️')
.setTimestamp();
interaction.reply({ embeds: [embed], ephemeral: true });
})
.catch(error => {
console.error(error);
});