2022-10-17 16:33:18 +00:00
|
|
|
const Command = require("../lib/command.js");
|
|
|
|
const CATEGORY = "moderation";
|
|
|
|
|
2023-10-05 19:41:47 +00:00
|
|
|
const {lookupUser, formatUsername} = require("../lib/utils.js");
|
2022-10-17 16:33:18 +00:00
|
|
|
|
|
|
|
const tidy = new Command("tidy");
|
|
|
|
tidy.addAlias("prune");
|
|
|
|
tidy.addAlias("purge");
|
|
|
|
tidy.category = CATEGORY;
|
|
|
|
tidy.helpText = "Clean up messages";
|
|
|
|
tidy.usage = "[subcommand] <count> <extra>";
|
2022-11-30 01:15:41 +00:00
|
|
|
tidy.callback = async function (msg, line, [subcommand, count, extra]) {
|
2023-01-24 21:33:17 +00:00
|
|
|
if (!msg.channel.permissionsOf(msg.author.id).has("manageMessages")) {
|
2022-10-17 16:33:18 +00:00
|
|
|
return "You do not have `Manage Messages` permission.";
|
|
|
|
}
|
2023-01-24 21:33:17 +00:00
|
|
|
if (!msg.channel.permissionsOf(hf.bot.user.id).has("manageMessages")) {
|
2022-10-17 16:33:18 +00:00
|
|
|
return "I do not have `Manage Messages` permission.";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (subcommand) {
|
|
|
|
case "all": {
|
|
|
|
const messages = await msg.channel.getMessages({
|
2023-01-22 05:01:31 +00:00
|
|
|
before: msg.id,
|
2022-10-17 16:33:18 +00:00
|
|
|
limit: count && parseInt(count) > 0 ? parseInt(count) : 10,
|
|
|
|
});
|
|
|
|
await msg.channel.deleteMessages(
|
|
|
|
messages.map((m) => m.id),
|
2023-10-05 19:41:47 +00:00
|
|
|
`Message purge by ${formatUsername(msg.author)}`
|
2022-10-17 16:33:18 +00:00
|
|
|
);
|
|
|
|
|
2023-01-22 05:01:31 +00:00
|
|
|
return `Deleted ${messages.length} message(s).`;
|
2022-10-17 16:33:18 +00:00
|
|
|
}
|
|
|
|
case "user": {
|
|
|
|
const user = await lookupUser(count);
|
|
|
|
if (typeof user === "string") {
|
|
|
|
return user;
|
|
|
|
} else {
|
|
|
|
const messages = await msg.channel.getMessages({
|
2023-01-22 05:01:31 +00:00
|
|
|
before: msg.id,
|
2022-10-17 16:33:18 +00:00
|
|
|
limit: extra && parseInt(extra) > 0 ? parseInt(extra) : 10,
|
|
|
|
});
|
|
|
|
await msg.channel.deleteMessages(
|
|
|
|
messages.filter((m) => m.author.id == user.id).map((m) => m.id),
|
2023-10-05 19:41:47 +00:00
|
|
|
`Message purge by ${formatUsername(
|
|
|
|
msg.author
|
|
|
|
)} targeting ${formatUsername(user)}`
|
2022-10-17 16:33:18 +00:00
|
|
|
);
|
|
|
|
|
2023-01-22 05:01:31 +00:00
|
|
|
return `Deleted ${messages.length} message(s).`;
|
2022-10-17 16:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "bots": {
|
|
|
|
const messages = await msg.channel.getMessages({
|
2023-01-22 05:01:31 +00:00
|
|
|
before: msg.id,
|
2022-10-17 16:33:18 +00:00
|
|
|
limit: count && parseInt(count) > 0 ? parseInt(count) : 50,
|
|
|
|
});
|
|
|
|
await msg.channel.deleteMessages(
|
|
|
|
messages.filter((m) => msg.author.bot).map((m) => m.id),
|
2023-10-05 19:41:47 +00:00
|
|
|
`Message purge by ${formatUsername(msg.author)} targeting bots`
|
2022-10-17 16:33:18 +00:00
|
|
|
);
|
|
|
|
|
2023-01-22 05:01:31 +00:00
|
|
|
return `Deleted ${messages.length} message(s).`;
|
2022-10-17 16:33:18 +00:00
|
|
|
}
|
|
|
|
case "filter": {
|
|
|
|
if (count.length === 0)
|
|
|
|
return "Filter must be at least 1 character long.";
|
|
|
|
|
|
|
|
const messages = await msg.channel.getMessages({
|
2023-01-22 05:01:31 +00:00
|
|
|
before: msg.id,
|
2022-10-17 16:33:18 +00:00
|
|
|
limit: extra && parseInt(extra) > 0 ? parseInt(extra) : 10,
|
|
|
|
});
|
|
|
|
await msg.channel.deleteMessages(
|
|
|
|
messages.filter((m) => m.content.indexOf(count) > -1).map((m) => m.id),
|
2023-10-05 19:41:47 +00:00
|
|
|
`Message purge by ${formatUsername(msg.author)} targeting "${count}"`
|
2022-10-17 16:33:18 +00:00
|
|
|
);
|
|
|
|
|
2023-01-22 05:01:31 +00:00
|
|
|
return `Deleted ${messages.length} message(s).`;
|
2022-10-17 16:33:18 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return `__Usage__
|
|
|
|
• \`all <count = 10>\` - Last \`count\` messages.
|
|
|
|
• \`user [user] <count = 10>\` - Last \`count\` messages from \`user\`.
|
|
|
|
• \`bots <count = 50>\` - Last \`count\` messages from bots.
|
|
|
|
• \`filter [string] <count = 10>\` - Last \`count\` messages whose content matches \`string\`.`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
hf.registerCommand(tidy);
|