vylbot-app/commands/unmute.js

99 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-11-01 19:29:12 +00:00
// Required components
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
const embedColor = "0x3050ba";
// Command Class
class unmute extends command {
constructor() {
// Set run method, description, category, usage
super("unmute");
super.description = "Unmutes the mentioned user with an optional reason";
super.category = "Moderation";
super.usage = "<@user> [reason]";
// Set required configs
super.requiredConfigs = "modrole";
super.requiredConfigs = "logchannel";
super.requiredConfigs = "muterole";
}
// The command's run method
unmute(context) {
// Check if the user has the mod role
if (context.message.member.roles.cache.find(role => role.name == context.client.config.mute.modrole)) {
// Get the user first pinged in the message
2021-02-17 18:12:45 +00:00
const user = context.message.mentions.users.first();
2020-11-01 19:29:12 +00:00
// If the user object exists
if (user) {
// Get the guild member object from the pinged user
2021-02-17 18:12:45 +00:00
const member = context.message.guild.member(user);
2020-11-01 19:29:12 +00:00
// If the member object exists, i.e. if the user is in the server
if (member) {
// Get the part of the argument array which contains the reason
2021-02-17 18:12:45 +00:00
const reasonArgs = context.arguments;
2020-11-01 19:29:12 +00:00
reasonArgs.splice(0, 1);
// Join the array into a string
2021-02-17 18:12:45 +00:00
const reason = reasonArgs.join(" ");
2020-11-01 19:29:12 +00:00
// If the server is available
if (context.message.guild.available) {
// If the bot client can manage the user
if (member.manageable) {
// The embed to go into the bot log
2021-02-17 18:12:45 +00:00
const embedLog = new MessageEmbed()
2020-11-01 19:29:12 +00:00
.setColor(embedColor)
.setTitle("Member Unmuted")
.addField("User", `${user} \`${user.tag}\``, true)
.addField("Moderator", `${context.message.author} \`${context.message.author.tag}\``, true)
.addField("Reason", reason || "*none*")
.setThumbnail(user.displayAvatarURL);
// The embed to go into the channel the command was sent in
2021-02-17 18:12:45 +00:00
const embedPublic = new MessageEmbed()
2020-11-01 19:29:12 +00:00
.setColor(embedColor)
.setDescription(`${user} has been unmuted`)
.addField("Reason", reason || "*none*");
// Get the muted role
2021-02-17 18:12:45 +00:00
const mutedRole = context.message.guild.roles.cache.find(role => role.name == context.client.config.unmute.muterole);
2020-11-01 19:29:12 +00:00
// Attempt to remove the role from the user, and then send the embeds. If unsuccessful log the error
member.roles.remove(mutedRole, reason).then(() => {
context.message.guild.channels.cache.find(channel => channel.name == context.client.config.unmute.logchannel).send(embedLog);
context.message.channel.send(embedPublic);
context.message.delete();
}).catch(err => {
errorEmbed(context, "An error occurred");
console.log(err);
});
} else { // If the bot can't manage the user
errorEmbed(context, "I am unable to unmute this user");
}
}
} else { // If the member object doesn't exist
errorEmbed(context, "Please specify a valid user");
}
} else { // If the user object doesn't exist
errorEmbed(context, "Please specify a valid user");
}
}
}
}
// Send an embed in case of an error
function errorEmbed(context, message) {
2021-02-17 18:12:45 +00:00
const embed = new MessageEmbed()
2020-11-01 19:29:12 +00:00
.setColor(embedColor)
.setDescription(message);
context.message.channel.send(embed);
}
module.exports = unmute;