3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 22:21:51 +00:00

!clean: --has-invite -> --has-invites, allow specifying target channel

This commit is contained in:
Dragory 2019-10-13 00:52:06 +03:00
parent 27484d261b
commit f2251535ab

View file

@ -552,13 +552,18 @@ export class UtilityPlugin extends ZeppelinPlugin<TConfigSchema> {
type: "userId", type: "userId",
shortcut: "u", shortcut: "u",
}, },
{
name: "channel",
type: "channelId",
shortcut: "c",
},
{ {
name: "bots", name: "bots",
flag: true, flag: true,
shortcut: "b", shortcut: "b",
}, },
{ {
name: "has-invite", name: "has-invites",
flag: true, flag: true,
shortcut: "i", shortcut: "i",
}, },
@ -567,20 +572,41 @@ export class UtilityPlugin extends ZeppelinPlugin<TConfigSchema> {
@d.permission("can_clean") @d.permission("can_clean")
async cleanAllCmd( async cleanAllCmd(
msg: Message, msg: Message,
args: { count: number; user?: string; bots?: boolean; "has-invite"?: boolean; fresh?: boolean }, args: {
count: number;
user?: string;
channel?: string;
bots?: boolean;
"has-invites"?: boolean;
fresh?: boolean;
},
) { ) {
if (args.count > MAX_CLEAN_COUNT || args.count <= 0) { if (args.count > MAX_CLEAN_COUNT || args.count <= 0) {
msg.channel.createMessage(errorMessage(`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`)); msg.channel.createMessage(errorMessage(`Clean count must be between 1 and ${MAX_CLEAN_COUNT}`));
return; return;
} }
const targetChannel = args.channel ? this.guild.channels.get(args.channel) : msg.channel;
if (!targetChannel || !(targetChannel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage(`Invalid channel specified`));
return;
}
if (targetChannel.id !== msg.channel.id) {
const configForTargetChannel = this.getConfigForMemberIdAndChannelId(msg.member.id, targetChannel.id);
if (configForTargetChannel.can_clean !== true) {
msg.channel.createMessage(errorMessage(`Missing permissions to use clean on that channel`));
return;
}
}
const messagesToClean = []; const messagesToClean = [];
let beforeId = msg.id; let beforeId = msg.id;
const timeCutoff = msg.timestamp - MAX_CLEAN_TIME; const timeCutoff = msg.timestamp - MAX_CLEAN_TIME;
while (messagesToClean.length < args.count) { while (messagesToClean.length < args.count) {
const potentialMessagesToClean = await this.savedMessages.getLatestByChannelBeforeId( const potentialMessagesToClean = await this.savedMessages.getLatestByChannelBeforeId(
msg.channel.id, targetChannel.id,
beforeId, beforeId,
args.count, args.count,
); );
@ -589,7 +615,7 @@ export class UtilityPlugin extends ZeppelinPlugin<TConfigSchema> {
const filtered = potentialMessagesToClean.filter(message => { const filtered = potentialMessagesToClean.filter(message => {
if (args.user && message.user_id !== args.user) return false; if (args.user && message.user_id !== args.user) return false;
if (args.bots && !message.is_bot) return false; if (args.bots && !message.is_bot) return false;
if (args["has-invite"] && getInviteCodesInString(message.data.content || "").length === 0) return false; if (args["has-invites"] && getInviteCodesInString(message.data.content || "").length === 0) return false;
if (moment.utc(message.posted_at).valueOf() < timeCutoff) return false; if (moment.utc(message.posted_at).valueOf() < timeCutoff) return false;
return true; return true;
}); });
@ -597,25 +623,31 @@ export class UtilityPlugin extends ZeppelinPlugin<TConfigSchema> {
messagesToClean.push(...filtered); messagesToClean.push(...filtered);
beforeId = potentialMessagesToClean[potentialMessagesToClean.length - 1].id; beforeId = potentialMessagesToClean[potentialMessagesToClean.length - 1].id;
if (moment.utc(potentialMessagesToClean[potentialMessagesToClean.length - 1].posted_at).valueOf() < timeCutoff) if (moment.utc(potentialMessagesToClean[potentialMessagesToClean.length - 1].posted_at).valueOf() < timeCutoff) {
break; break;
}
} }
let responseMsg: Message; let responseMsg: Message;
if (messagesToClean.length > 0) { if (messagesToClean.length > 0) {
await this.cleanMessages(msg.channel, messagesToClean, msg.author); await this.cleanMessages(targetChannel, messagesToClean, msg.author);
responseMsg = await msg.channel.createMessage( let responseText = `Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`;
successMessage(`Cleaned ${messagesToClean.length} ${messagesToClean.length === 1 ? "message" : "messages"}`), if (targetChannel.id !== msg.channel.id) responseText += ` in <#${targetChannel.id}>`;
);
responseMsg = await msg.channel.createMessage(successMessage(responseText));
} else { } else {
responseMsg = await msg.channel.createMessage(errorMessage(`Found no messages to clean!`)); responseMsg = await msg.channel.createMessage(errorMessage(`Found no messages to clean!`));
} }
setTimeout(() => { if (targetChannel.id !== msg.channel.id) {
msg.delete().catch(noop); // Delete the !clean command and the bot response if a different channel wasn't specified
responseMsg.delete().catch(noop); // (so as not to spam the cleaned channel with the command itself)
}, CLEAN_COMMAND_DELETE_DELAY); setTimeout(() => {
msg.delete().catch(noop);
responseMsg.delete().catch(noop);
}, CLEAN_COMMAND_DELETE_DELAY);
}
} }
@d.command("info", "[user:resolvedUserLoose]") @d.command("info", "[user:resolvedUserLoose]")