Problem
I made a warning system for my discord bot where admins could warn a user and add their warnings to the database. It gives an ID so that it can be removed when desired. I use my own IDs to make it simpler. Removing the ID requires the user tag and the warning ID and it should remove the warning from the ID. I use MongoDB as my database. Pushing warnings is all good but removing them doesn't seem to work at all.
Here's the piece of the code. This is triggered when the bot command is triggered:
const warnDoc = await Warn.findOne({ guildID: message.guild!.id, warnedUserID: warnMember.id });
warnDoc.warnings.pull({ warningID: warnID });
await warnDoc.save().catch((err: any) => console.log(err));
Here is the schema:
const warnSchema = new mongoose.Schema({
guildID: String,
guildName: String,
warnedUserID: String,
warnedUserName: String,
warnings: [{
warningID: String,
warningMessage: String,
warningAuthorUserName: String,
warningAuthorUserID: String,
warningDate: String,
}],
});
warnDoc
has the subdocument array which is the warnings given by an admin/moderator to a user. It finds it by using the message's guildID
and the user ID of the warned user. guildID
is needed so that a user can have warnings in multiple servers where the bot is present and each server has separate warnings.
It does output the subdocuments by logging warnDoc.warnings
but using the pull
method doesn't seem to work at all. There's no errors or anything. It just doesn't remove the specific subdocument provided with the ID. The document simply never changed.