tempmute discord.js code example
Example: discord.js tempmute command
function tempmute(message, args, prefix, client) {
var muterolename = "muted";
var muteRole = message.guild.roles.cache.find(r => r.name === muterolename);
var muteUser = message.mentions.members.first();
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;
var minutes = args[2];
var muteReason = message.content.slice(6 + prefixLeangth, prefixLeangth + message.content.length);
if (!muteReason) var muteReason = "No reason given";
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)
muteUser.roles.add(muteRole, `Muted by ${message.author.tag} for ${minutes} minutes. Reason: ${muteReason}`);
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);
}