JaBa/commands/Moderation/ban.js

106 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const Command = require("../../base/Command.js"),
Discord = require("discord.js");
class Ban extends Command {
2021-12-26 19:29:37 +05:00
constructor(client) {
2021-12-10 21:39:54 +05:00
super(client, {
name: "ban",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: [],
2021-12-26 19:29:37 +05:00
memberPermissions: ["BAN_MEMBERS"],
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS", "BAN_MEMBERS"],
2021-12-10 21:39:54 +05:00
nsfw: false,
ownerOnly: false,
cooldown: 2000
2021-12-10 21:39:54 +05:00
});
}
2021-12-26 19:29:37 +05:00
async run(message, args, data) {
2021-12-10 21:39:54 +05:00
const user = await this.client.resolveUser(args[0]);
2021-12-11 01:11:50 +05:00
if (!user) return message.error("moderation/ban:MISSING_MEMBER");
2021-12-26 19:29:37 +05:00
const memberData = message.guild.members.cache.get(user.id) ? await this.client.findOrCreateMember({
id: user.id,
guildID: message.guild.id
}) : null;
2021-12-10 21:39:54 +05:00
2021-12-11 01:11:50 +05:00
if (user.id === message.author.id) return message.error("moderation/ban:YOURSELF");
2021-12-10 21:39:54 +05:00
// If the user is already banned
const banned = await message.guild.fetchBans();
2021-12-26 19:29:37 +05:00
if (banned.some((m) => m.user.id === user.id)) return message.error("moderation/ban:ALREADY_BANNED", {
username: user.tag
});
2021-12-11 01:11:50 +05:00
2021-12-10 21:39:54 +05:00
// Gets the ban reason
let reason = args.slice(1).join(" ");
2021-12-11 01:11:50 +05:00
if (!reason) reason = message.translate("misc:NO_REASON_PROVIDED");
2021-12-10 21:39:54 +05:00
const member = await message.guild.members.fetch(user.id).catch(() => {});
2021-12-11 01:11:50 +05:00
if (member) {
2021-12-10 21:39:54 +05:00
const memberPosition = member.roles.highest.position;
const moderationPosition = message.member.roles.highest.position;
2021-12-26 19:29:37 +05:00
if (message.member.ownerID !== message.author.id && !(moderationPosition > memberPosition)) return message.error("moderation/ban:SUPERIOR");
2021-12-11 01:11:50 +05:00
if (!member.bannable) return message.error("moderation/ban:MISSING_PERM");
};
2021-12-10 21:39:54 +05:00
await user.send(message.translate("moderation/ban:BANNED_DM", {
username: user.tag,
server: message.guild.name,
moderator: message.author.tag,
reason
})).catch(() => {});
// Ban the user
2021-12-26 19:29:37 +05:00
message.guild.members.ban(user, {
reason
}).then(() => {
2021-12-10 21:39:54 +05:00
// Send a success message in the current channel
message.sendT("moderation/ban:BANNED", {
username: user.tag,
server: message.guild.name,
moderator: message.author.tag,
reason
});
const caseInfo = {
channel: message.channel.id,
moderator: message.author.id,
date: Date.now(),
type: "ban",
case: data.guild.casesCount,
reason
};
2021-12-11 01:11:50 +05:00
if (memberData) {
2021-12-10 21:39:54 +05:00
memberData.sanctions.push(caseInfo);
memberData.save();
2021-12-11 01:11:50 +05:00
};
2021-12-10 21:39:54 +05:00
data.guild.casesCount++;
data.guild.save();
2021-12-11 01:11:50 +05:00
if (data.guild.plugins.modlogs) {
2021-12-10 21:39:54 +05:00
const channel = message.guild.channels.cache.get(data.guild.plugins.modlogs);
2021-12-11 01:11:50 +05:00
if (!channel) return;
2021-12-10 21:39:54 +05:00
const embed = new Discord.MessageEmbed()
2021-12-26 19:29:37 +05:00
.setAuthor(message.translate("moderation/ban:CASE", {
count: data.guild.casesCount
}))
2021-12-10 21:39:54 +05:00
.addField(message.translate("common:USER"), `\`${user.tag}\` (${user.toString()})`, true)
.addField(message.translate("common:MODERATOR"), `\`${message.author.tag}\` (${message.author.toString()})`, true)
.addField(message.translate("common:REASON"), reason, true)
.setColor("#e02316");
channel.send(embed);
2021-12-11 01:11:50 +05:00
};
2021-12-10 21:39:54 +05:00
}).catch((err) => {
console.log(err);
return message.error("moderation/ban:MISSING_PERM");
});
}
2021-12-11 01:11:50 +05:00
};
2021-12-10 21:39:54 +05:00
2021-12-11 01:11:50 +05:00
module.exports = Ban;