tempmute doscord.js code example

Example: discord.js tempmute command

function tempmute(message, args, prefix, client) {
    var muterolename = "muted"; //Put the name of the muted role

    var muteRole = message.guild.roles.cache.find(r => r.name === muterolename); //Gets data of muted role

    var muteUser = message.mentions.members.first(); //Gets role your trying to mute
  
	// Conditions
	if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.reply(":no_entry_sign: You do not have permissions to **mute** that user :no_entry_sign:");
	if (!muteUser) return message.reply("You have to mention a valid member.");
	if (!muteChannel) return message.reply("There's no channel called " + logchannel);
	if (!muteRole) return message.reply("There's no role called " + muterolename);
	if (!message.guild.member(client.user.id).hasPermission("MANAGE_ROLES")) return message.reply("I Don't have permissions");

	var prefixLeangth = prefix.length; //Gets lenth of prefix
	var minutes = args[2]; //Time in minutes
	var muteReason = message.content.slice(6 + prefixLeangth, prefixLeangth + message.content.length); //Gets reason if one is one
	if (!muteReason) var muteReason = "No reason given"; //Make the reason "No reason given" if field is left empty"

	// Embed
	var muteEmbed = new Discord.MessageEmbed()
	muteEmbed.setTitle("Mute")
	muteEmbed.addField("Muted user", muteUser)
	muteEmbed.addField("Reason", muteReason)
	muteEmbed.addField("Minutes", minutes)
	muteEmbed.setFooter(`Muted by ${message.author.tag}`)
	muteEmbed.setTimestamp();
	
  	message.content.send(muteEmbed)

	// You need to parse those arguments, I'll leave that to you.

	// Mute the user
	muteUser.roles.add(muteRole, `Muted by ${message.author.tag} for ${minutes} minutes. Reason: ${muteReason}`);

	// Unmute them after x minutes
	timeout(minutes, muteUser, muteRole, message)
}

function timeout(minutes, muteUser, mutedRole, message) {
	setTimeout(() => {
    muteUser.roles.remove(mutedRole, `Temporary mute expired.`);

    var muteEmbed = new Discord.MessageEmbed()
    muteEmbed.setTitle("Unmute")
    muteEmbed.addField("Unmuted user", muteUser)
    muteEmbed.addField("Reason", 'Time ran out.')
    muteEmbed.setFooter(`Unmuted by me`)
    muteEmbed.setTimestamp();
    message.channel.send(muteEmbed)
  }, minutes * 60000); // time in ms
}