discord bot mute command

example .mute @Ashleydafox 5m i try 1s something 10m, 15m 20d, 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() 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!");
}
}
};





thats the cide
Was this page helpful?