Validation Error in Mongoose/djs

Hello! I am trying to write 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. Here is my related code (djs v14.16.2): moderator.js
const target = interaction.guild.members.cache.get(interaction.options.getString("user_id").user.username);

if (!ModeratorSchema.find({ username: target })) {
ModeratorSchema.create({
username: target,
user_id: interaction.options.getString("user_id"),
type: interaction.options.getInteger("type")
})

interaction.reply({
content: `Added \`${target} (${interaction.options.getString("user_id")})\` as a system moderator with permission level ${interaction.options.getInteger("type")}`,
ephemeral: true
});

client.channels.cache.get(log_channel).send({
content: `\`${target} (${interaction.options.getString("user_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
});
}
const target = interaction.guild.members.cache.get(interaction.options.getString("user_id").user.username);

if (!ModeratorSchema.find({ username: target })) {
ModeratorSchema.create({
username: target,
user_id: interaction.options.getString("user_id"),
type: interaction.options.getInteger("type")
})

interaction.reply({
content: `Added \`${target} (${interaction.options.getString("user_id")})\` as a system moderator with permission level ${interaction.options.getInteger("type")}`,
ephemeral: true
});

client.channels.cache.get(log_channel).send({
content: `\`${target} (${interaction.options.getString("user_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
});
}
ModeratorSchema.js
const { model, Schema } = require("mongoose");

let ModeratorSchema = new Schema({
username: String,
user_id: {
type: String,
unique: true
},
type: Number
});

module.exports = model("moderator", ModeratorSchema);
const { model, Schema } = require("mongoose");

let ModeratorSchema = new Schema({
username: String,
user_id: {
type: String,
unique: true
},
type: Number
});

module.exports = model("moderator", ModeratorSchema);
Thanks in advance for any help!
4 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
aestriex
aestriexOP4mo ago
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.
Mark
Mark4mo ago
A) as this is more related to your DB, #other-js-ts is more appropriate B) your user_id option would be better suited as a UserOption instead of a string option - it would validate the input to make sure it's a valid user and also simplify your code - target would much be just the options.getUser(...) and get the username and id from target C) probably better to search by the id instead of the username, as they are unique and can't be changed, that's why d.js keys the cache by them D) troubleshoot the result of your DB query and see what it's returning
aestriex
aestriexOP4mo ago
It's returning a full output of what it seems like is the entire schema... but when I do the following to cover everything it could output:
ModeratorSchema.find({ user_id: target.id }), function (err, data) {
if (data) {
info("OK");
} else if (err) {
info("Error")
error(err)
} else {
info("No data")
}
}
ModeratorSchema.find({ user_id: target.id }), function (err, data) {
if (data) {
info("OK");
} else if (err) {
info("Error")
error(err)
} else {
info("No data")
}
}
It just doesn't respond at all, no error output either, I'm not sure why I updated target to be a User like you said
const target = interaction.options.getUser("user");
const target = interaction.options.getUser("user");
Okay, thanks!
Want results from more Discord servers?
Add your server