JaBa/commands/Moderation/clear.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

const Command = require("../../base/Command");
2022-01-04 02:18:28 +05:00
class Clear extends Command {
constructor(client) {
super(client, {
name: "clear",
dirname: __dirname,
enabled: true,
guildOnly: true,
2022-01-05 22:04:46 +05:00
aliases: ["cl", "purge"],
2022-01-04 02:18:28 +05:00
memberPermissions: ["MANAGE_MESSAGES"],
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS", "MANAGE_MESSAGES"],
nsfw: false,
ownerOnly: false,
cooldown: 1000
});
}
async run(message, args) {
if (args[0] === "all") {
2022-02-15 01:40:18 +05:00
message.channel.send(message.translate("moderation/clear:ALL_CONFIRM"));
2022-03-27 21:57:01 +05:00
const filter = m => m.author.id === message.author.id && m.content === "confirm";
const collector = message.channel.createMessageCollector({
filter,
time: 120000 // 2 minutes
2022-01-04 02:18:28 +05:00
});
2022-02-15 01:40:18 +05:00
2022-03-27 21:57:01 +05:00
collector.on("collect", async message => {
const position = message.channel.position;
const newChannel = await message.channel.clone();
await message.channel.delete();
newChannel.setPosition(position);
return newChannel.send({
content: message.translate("moderation/clear:CHANNEL_CLEARED")
});
});
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
collector.on("end", (_, reason) => {
if (reason === "time") return message.error("misc:TIMES_UP");
});
} else {
const amount = args[0];
if (!amount || isNaN(amount) || parseInt(amount) < 1) return message.error("moderation/clear:MISSING_AMOUNT");
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
await message.delete();
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
const user = message.mentions.users.first();
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
let messages = await message.channel.messages.fetch({
limit: amount
});
if (user) messages = messages.filter((m) => m.author.id === user.id);
if (messages.length > amount) messages.length = parseInt(amount, 10);
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
messages = messages.filter((m) => !m.pinned);
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
message.channel.bulkDelete(messages, true);
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
let toDelete = null;
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
if (user) {
toDelete = await message.channel.send(message.translate("moderation/clear:CLEARED_MEMBER", {
amount: `${amount} ${message.getNoun(amount, message.translate("misc:NOUNS:MESSAGES:1"), message.translate("misc:NOUNS:MESSAGES:2"), message.translate("misc:NOUNS:MESSAGES:5"))}`,
username: user.tag
}));
} else {
toDelete = await message.channel.send(message.translate("moderation/clear:CLEARED", {
amount: `${amount} ${message.getNoun(amount, message.translate("misc:NOUNS:MESSAGES:1"), message.translate("misc:NOUNS:MESSAGES:2"), message.translate("misc:NOUNS:MESSAGES:5"))}`
}));
}
2022-01-04 02:18:28 +05:00
2022-03-27 21:57:01 +05:00
setTimeout(function () {
toDelete.delete();
}, 2000);
}
2022-01-04 02:18:28 +05:00
}
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
module.exports = Clear;