- Joined
- Nov 29, 2021
- Messages
- 1
- Reaction score
- 0
I'm trying to delete a warn by using its `_id` property, but it is unable to find the warn, but when I console.log all the warns, it is there. Why am I unable to find the warn?
My warn.js command
My welcome.js model
Please help me out, thanks in advance!
My warn.js command
JavaScript:
const schema = require('../../models/welcome.js')
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "warns",
description: "Warning system.",
options: [
{
name: 'add',
description: 'Warns a user.',
type: 'SUB_COMMAND',
options: [
{
name: 'user',
description: 'The user to warn.',
type: 'USER',
required: true
},
{
name: 'reason',
description: 'The reason for the warn.',
type: 'STRING',
required: true
},
{
name: 'evidence',
description: 'The evidence for the warn.',
type: 'STRING'
}
]
},
{
name: 'delete',
description: 'Deletes a warn for a user.',
type: 'SUB_COMMAND',
options: [
{
name: 'user',
description: 'The user whose warn to remove.',
type: 'USER',
required: true
},
{
name: 'warn_id',
description: 'The ID of the warn to delete (Can be fetched from the "warns list" command).',
type: 'STRING',
required: true
},
]
},
{
name: 'clear',
description: 'Clear all warns for a user.',
type: 'SUB_COMMAND',
options: [{
name: 'user',
description: 'The user whose warns to clear.',
type: 'USER',
required: true
}]
},
{
name: 'list',
description: 'Lists all the warns for a user.',
type: 'SUB_COMMAND',
options: [{
name: 'user',
description: 'The user whose warns to list.',
type: 'USER',
required: true
}]
},
],
async execute(interaction) {
const sub = interaction.options.getSubcommand(['add', 'delete', 'clear', 'list'])
const member = interaction.options.getMember('user')
const reason = interaction.options.getString('reason')
const evidence = interaction.options.getString('evidence') || 'No evidence provided for this warn.'
const warn_id = interaction.options.getInteger('warn_number')
const date = new Date(interaction.createdAt).toLocaleDateString()
let result = await schema.findOne({
_id: interaction.guild.id
})
let user = result.warnings.find(u => u.userId === member.id)
if (sub === 'add') {
const embed = new MessageEmbed()
.setDescription(`:white_check_mark: ${member} was warned.\nReason: **${reason}**\nEvidence: ${evidence}`)
.setColor('RED')
.setFooter(interaction.client.user.tag,interaction.client.user.displayAvatarURL({dynamic: true}))
await interaction.reply({ embeds: [embed], ephemeral: true })
let warnId;
if (user) {
let warn = {
reason: reason,
executorId: interaction.member.id,
evidence: evidence,
date: date
}
let warnNum = user.warns.push(warn)
warnId = user.warns[warnNum - 1]._id
console.log(warnId)
}
if (!user) {
user = { userId: member.id,
warns: [{
reason: reason,
executorId: interaction.member.id,
evidence: evidence,
date: date
}] }
result.warnings.push(user)
warnId = user.warns[0]._id
}
let dm = 'Yes';
try {
await member.send(`You were warned in **${interaction.guild.name}** for the reason: **${reason}**.`)
}
catch(err) {
dm = 'No'
}
console.log(warnId)
if (!result.logChannelId) return
let logChannel = interaction.guild.channels.cache.get(result.logChannelId)
if (!logChannel) logChannel = interaction.guild.channels.fetch(result.logChannelId, {cache: true, force: true})
const logEmbed = new MessageEmbed()
.setTitle('Member warned!')
.setColor('RED')
.setDescription(`${member} was warned by ${interaction.member}.\nReason: **${reason}**\nEvidence: ${evidence}`)
.setFooter(`DM: ${dm} | Date: ${date}`)
await logChannel.send({embeds: [logEmbed]})
await result.save()
return
}
if (!user) {
const embed = new MessageEmbed()
.setTitle('No warnings')
.setDescription(`This user does not have any warnings.`)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
.setColor('RED')
await interaction.reply({ embeds: [embed], ephemeral: true })
return
}
else if (sub === 'delete') {
console.log(user.warns)
let warn = user.warns.find(warn => warn._id.toString() === warn_id)
if (warn) console.log('YEs')
if (!warn) {
const embed = new MessageEmbed()
.setDescription(`Invalid warn ID.`)
.setColor('RED')
await interaction.reply({ embeds: [embed], ephemeral: true })
return
}
const embed = new MessageEmbed()
.setTitle('Warning deleted!')
.setDescription(`:white_check_mark: Warning for ${warn.reason} removed from ${member} by ${interaction.member}.`)
.setFooter(`Warn ID: ${warn_id}`)
await interaction.reply({ embeds: [embed], ephemeral: true })
const newUserWarns = user.warns.splice((user.warns.indexOf(warn)), 1)
user.warns = newUserWarns
if (!user.warns.length) result.warnings.splice(result.warnings.indexOf(user), 1)
if (!result.logChannelId) return
let channel = interaction.guild.channels.cache.get(result.logChannelId)
if (!channel) channel === interaction.guild.channels.fetch(results.logChannelId, { cache: true })
const logEmbed = new MessageEmbed()
.setTitle('Warning removed!')
.setDescription(`A warning has been removed from ${member} by ${interaction.member}.`)
.setFooter(`Date: ${date} | Warn ID: ${warn_id}`)
await channel.send({embeds: [logEmbed]})
}
else if (sub === 'clear') {
const embed = new MessageEmbed()
.setDescription(`:white_check_mark: All warnings have been removed from ${member}.`)
await interaction.reply({embeds: [embed], ephemeral: true})
result.warnings.splice(result.warnings.indexOf(user), 1)
if (!result.logChannelId) return
let channel = interaction.guild.channels.cache.get(result.logChannelId)
if (!channel) channel === interaction.guild.channels.fetch(results.logChannelId, { cache: true })
const logEmbed = new MessageEmbed()
.setTitle('Warnings cleared!')
.setDescription(`All warnings have been removed from ${member} by ${interaction.member}.`)
.setFooter(`Date: ${date}`)
await channel.send({embeds: [logEmbed]})
}
await result.save()
if (sub === 'list') {
const embed = new MessageEmbed()
.setTitle(`Warnings for ${member.user.tag}`)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
.setDescription('Use the warn number in the `/delwarn` command to delete the warning.')
for (const warn of user.warns) {
embed.addField((warn._id).toString(), `Reason: ${warn.reason}\nExecutor: <@${warn.executorId}>\nEvidence: ${warn.evidence}\nDate: ${warn.date}`)
}
await interaction.reply({embeds: [embed], ephemeral: true})
}
},
}
My welcome.js model
JavaScript:
const mongoose = require('mongoose');
const schema = mongoose.Schema({
_id: {
type: String,
required: true
},
welcomeChannelId: {
type: String,
required: true
},
welcomeText: {
type: String,
required: true
},
chatChannelId: {
type: String,
required: true
},
logChannelId: {
type: String,
required: true
},
ticketInfo: {
type: {
categoryId: {
type: String,
required: true
},
roleId: {
type: String,
required: true
}
}
},
starboardChannelId: {
type: String,
required: true
},
warnings: {
type: [{ userId: String, warns: [{
reason: String,
executorId: String,
evidence: String,
date: String }] }],
required: true
}
});
module.exports = mongoose.model('welcome', schema);
Please help me out, thanks in advance!