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