const { PermissionFlagsBits } = require("discord.js"); const ms = require("ms"); module.exports = { name: "mute", description: "Mutes (timeouts) a user for a specified duration.", async execute(message, args) { try { // ✅ Check if the user running the command has "Timeout Members" permission if (!message.member.permissions.has(PermissionFlagsBits.ModerateMembers)) { return message.reply("❌ You do not have permission to timeout members!"); } // ✅ Get bot's member object and check its permissions const botMember = await message.guild.members.fetchMe(); if (!botMember.permissions.has(PermissionFlagsBits.ModerateMembers)) { return message.reply("❌ I need the 'Timeout Members' permission to mute users!"); } // ✅ Ensure a user is provided (mention or ID) if (!args[0]) { return message.reply("❌ Please provide a user mention or ID to mute!"); } const target = message.mentions.members.first() || await message.guild.members.fetch(args[0]).catch(() => null); if (!target) { return message.reply("❌ Invalid user! Please mention a valid user or provide their ID."); } // ✅ Ensure a duration was provided if (!args[1]) { return message.reply("❌ Please specify a duration (e.g., `1s`, `5m`, `2h`, `4d`)."); } // ✅ Extract the time argument (ignores case sensitivity) let timeArg = args[1].toLowerCase(); // ✅ If only a number is given, assume minutes if (!isNaN(timeArg)) { timeArg = `${timeArg}m`; // Default to minutes if no unit is provided } const timeMs = ms(timeArg); // ✅ Validate the parsed time if (!timeMs || timeMs < 1000) { // Minimum timeout: 1 second return message.reply("❌ Invalid time format! Example: `1s`, `30s`, `5m`, `2h`, `7d`."); } // ✅ Ensure the time does not exceed Discord’s max timeout (28 days) const maxTimeout = 2419200000; // 28 days in milliseconds if (timeMs > maxTimeout) { return message.reply("❌ You cannot mute someone for more than 28 days!"); } // ✅ Ensure the bot can actually mute (timeout) the user if (!target.moderatable) { return message.reply("❌ I can't timeout this user! They might have a higher role than me."); } // ✅ Check role hierarchy if (target.roles.highest.position >= botMember.roles.highest.position) { return message.reply("❌ I can't timeout this user because their role is equal to or higher than mine."); } // ✅ Apply the timeout await target.timeout(timeMs, `Muted by ${message.author.tag}`); return message.reply(`✅ **${target.user.tag}** has been muted (timed out) for **${ms(timeMs, { long: true })}**.`); } catch (error) { console.error("Mute command error:", error); return message.reply("❌ Failed to mute (timeout) the user!"); } } };