aestriex
DIAdiscord.js - Imagine an app
•Created by aestriex on 9/22/2024 in #djs-questions
Validation Error in Mongoose/djs
Hello! I am trying to write a command called
Thanks in advance for any help!
/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);
7 replies