const Discord = require("discord.js"); const db = require("../../utils/db"); const { hasPermissionOrDev } = require("../../utils/developers"); const { logAudit } = require("../../utils/auditLogger"); module.exports.run = async (client, inter) => { if (!hasPermissionOrDev(inter.member, "BAN_MEMBERS")) { const noPerms = new Discord.EmbedBuilder() .setDescription( "You do not have permissions for this command!\n" + "If this is an error please create a ticket for assistance!" ) .setColor(client.config.colors.negative) .setAuthor({ name: "Missing Permissions", iconURL: inter.guild.iconURL({ dynamic: true }), }); await inter.reply({ embeds: [noPerms], ephemeral: true }).catch(() => {}); return; } const user = inter.options.getUser("user"); const warnIndex = inter.options.getInteger("warning_number"); if (!user) { const userNotFound = new Discord.EmbedBuilder() .setDescription("This user was not found!") .setColor(client.config.colors.negative) .setAuthor({ name: "Something went wrong!", iconURL: inter.guild.iconURL({ dynamic: true }), }); await inter .reply({ embeds: [userNotFound], ephemeral: true }) .catch(() => {}); return; } let punishments = db.get(`punishments_${inter.guildId}_${user.id}`) || []; const warnings = punishments.filter((p) => p.type === "warn"); if (warnings.length === 0) { const noWarnings = new Discord.EmbedBuilder() .setDescription(`**${user.tag}** has no warnings!`) .setColor(client.config.colors.negative) .setAuthor({ name: "No Warnings", iconURL: inter.guild.iconURL({ dynamic: true }), }); await inter .reply({ embeds: [noWarnings], ephemeral: true }) .catch(() => {}); return; } if (warnIndex) { if (warnIndex < 1 || warnIndex > warnings.length) { const invalidIndex = new Discord.EmbedBuilder() .setDescription( `Invalid warning number! **${user.tag}** has ${warnings.length} warning(s).\n` + `Use a number between 1 and ${warnings.length}.` ) .setColor(client.config.colors.negative) .setAuthor({ name: "Something went wrong!", iconURL: inter.guild.iconURL({ dynamic: true }), }); await inter .reply({ embeds: [invalidIndex], ephemeral: true }) .catch(() => {}); return; } const warnToRemove = warnings[warnIndex - 1]; const warnIndexInAll = punishments.indexOf(warnToRemove); punishments.splice(warnIndexInAll, 1); db.set(`punishments_${inter.guildId}_${user.id}`, punishments); const successEmbed = new Discord.EmbedBuilder() .setAuthor({ name: "Warning Removed", iconURL: inter.guild.iconURL({ dynamic: true }), }) .setDescription( `Warning #${warnIndex} successfully removed from **${user.tag}**!\n\n` + "**Removed warning:**\n" + `> Reason: ${warnToRemove.reason}\n` + `> Given by: <@${warnToRemove.moderator}>\n` + `> Date: \n\n` + `**Remaining warnings:** ${warnings.length - 1}` ) .setColor(client.config.colors.positive); await inter.reply({ embeds: [successEmbed] }).catch(() => {}); await logAudit(inter.guild, "unwarn", { user: user, moderator: inter.user, reason: `Removed warning #${warnIndex}`, warnCount: warnings.length - 1, }); const logChannel = inter.guild.channels.cache.find( (x) => x.id === db.get(`modLog_${inter.guildId}`) ); if (logChannel) logChannel.send({ embeds: [successEmbed] }).catch(() => {}); } else { punishments = punishments.filter((p) => p.type !== "warn"); db.set(`punishments_${inter.guildId}_${user.id}`, punishments); const successEmbed = new Discord.EmbedBuilder() .setAuthor({ name: "Warnings Removed", iconURL: inter.guild.iconURL({ dynamic: true }), }) .setDescription( `All **${warnings.length}** warning(s) successfully removed from **${user.tag}**!\n\n` + `**Removed by:** ${inter.user}` ) .setColor(client.config.colors.positive); await inter.reply({ embeds: [successEmbed] }).catch(() => {}); await logAudit(inter.guild, "unwarn", { user: user, moderator: inter.user, reason: `Removed all ${warnings.length} warnings`, warnCount: 0, }); const logChannel = inter.guild.channels.cache.find( (x) => x.id === db.get(`modLog_${inter.guildId}`) ); if (logChannel) logChannel.send({ embeds: [successEmbed] }).catch(() => {}); } }; module.exports.help = { name: "unwarn", }; // Copyright - Made with ❤ by DevArqf