I am trying to make a Discord bot with a command called /moderator
with a sub-command /moderator add
which adds a user’s ID to a moderator database (MongoDB/Mongoose) which is then checked anytime someone wants to run a moderator command.
I am using mongoose for a template schema.
This is my full code:
if (!ModeratorSchema.find({ user_id: target.id })) {
ModeratorSchema.create({
username: target.username,
user_id: target.id,
type: interaction.options.getInteger("type")
})
interaction.reply({
content: `Added `${target} (${target.id})` as a system moderator with permission level ${interaction.options.getInteger("type")}`,
ephemeral: true
});
client.channels.cache.get(log_channel).send({
content: ``${target} (${target.id})` was added as a system moderator by `${interaction.user.username} (${interaction.user.id})` with permission level `${interaction.options.getInteger("type")}`.`
})
} else {
interaction.reply({
content: "User is already a system moderator.",
ephemeral: true
});
}
This is a check I was using to see if the schema was working, and after running this, there was no output at all.
ModeratorSchema.find({ user_id: target.id }), function (err, data) {
if (data) {
info("OK");
} else if (err) {
info("Error")
error(err)
} else {
info("No data")
}
}
Any help would be greatly appreciated! Thanks in advance.
I am trying to check to see if a user is already in the database before adding a new record.
I made it so the code does add moderators (username, user_id, type) to a database, but I can’t seem to get validation checks to work to make sure people don’t add a user id multiple times. I am using mongoose for schemas, and made user_id unique, so while the system won’t add a user document multiple times, the Discord output isn’t working still.
Aestriex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2