mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
Censor: hold a lock for the evaluated message and interrupt said lock if the message is deleted
This commit is contained in:
parent
515c91d2c2
commit
e1eb110ecd
1 changed files with 37 additions and 15 deletions
|
@ -115,8 +115,12 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async applyFiltersToMsg(savedMessage: SavedMessage) {
|
/**
|
||||||
if (!savedMessage.data.content) return;
|
* Applies word censor filters to the message, if any apply.
|
||||||
|
* @return {boolean} Indicates whether the message was removed
|
||||||
|
*/
|
||||||
|
async applyFiltersToMsg(savedMessage: SavedMessage): Promise<boolean> {
|
||||||
|
if (!savedMessage.data.content) return false;
|
||||||
|
|
||||||
const config = this.getConfigForMemberIdAndChannelId(savedMessage.user_id, savedMessage.channel_id);
|
const config = this.getConfigForMemberIdAndChannelId(savedMessage.user_id, savedMessage.channel_id);
|
||||||
|
|
||||||
|
@ -126,7 +130,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
const result = ZalgoRegex.exec(savedMessage.data.content);
|
const result = ZalgoRegex.exec(savedMessage.data.content);
|
||||||
if (result) {
|
if (result) {
|
||||||
this.censorMessage(savedMessage, "zalgo detected");
|
this.censorMessage(savedMessage, "zalgo detected");
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +152,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
for (const invite of invites) {
|
for (const invite of invites) {
|
||||||
if (!invite.guild && !allowGroupDMInvites) {
|
if (!invite.guild && !allowGroupDMInvites) {
|
||||||
this.censorMessage(savedMessage, `group dm invites are not allowed`);
|
this.censorMessage(savedMessage, `group dm invites are not allowed`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inviteGuildWhitelist && !inviteGuildWhitelist.includes(invite.guild.id)) {
|
if (inviteGuildWhitelist && !inviteGuildWhitelist.includes(invite.guild.id)) {
|
||||||
|
@ -156,7 +160,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
savedMessage,
|
savedMessage,
|
||||||
`invite guild (**${invite.guild.name}** \`${invite.guild.id}\`) not found in whitelist`,
|
`invite guild (**${invite.guild.name}** \`${invite.guild.id}\`) not found in whitelist`,
|
||||||
);
|
);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inviteGuildBlacklist && inviteGuildBlacklist.includes(invite.guild.id)) {
|
if (inviteGuildBlacklist && inviteGuildBlacklist.includes(invite.guild.id)) {
|
||||||
|
@ -164,17 +168,17 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
savedMessage,
|
savedMessage,
|
||||||
`invite guild (**${invite.guild.name}** \`${invite.guild.id}\`) found in blacklist`,
|
`invite guild (**${invite.guild.name}** \`${invite.guild.id}\`) found in blacklist`,
|
||||||
);
|
);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inviteCodeWhitelist && !inviteCodeWhitelist.includes(invite.code)) {
|
if (inviteCodeWhitelist && !inviteCodeWhitelist.includes(invite.code)) {
|
||||||
this.censorMessage(savedMessage, `invite code (\`${invite.code}\`) not found in whitelist`);
|
this.censorMessage(savedMessage, `invite code (\`${invite.code}\`) not found in whitelist`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inviteCodeBlacklist && inviteCodeBlacklist.includes(invite.code)) {
|
if (inviteCodeBlacklist && inviteCodeBlacklist.includes(invite.code)) {
|
||||||
this.censorMessage(savedMessage, `invite code (\`${invite.code}\`) found in blacklist`);
|
this.censorMessage(savedMessage, `invite code (\`${invite.code}\`) found in blacklist`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,12 +193,12 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
for (const thisUrl of urls) {
|
for (const thisUrl of urls) {
|
||||||
if (domainWhitelist && !domainWhitelist.includes(thisUrl.hostname)) {
|
if (domainWhitelist && !domainWhitelist.includes(thisUrl.hostname)) {
|
||||||
this.censorMessage(savedMessage, `domain (\`${thisUrl.hostname}\`) not found in whitelist`);
|
this.censorMessage(savedMessage, `domain (\`${thisUrl.hostname}\`) not found in whitelist`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (domainBlacklist && domainBlacklist.includes(thisUrl.hostname)) {
|
if (domainBlacklist && domainBlacklist.includes(thisUrl.hostname)) {
|
||||||
this.censorMessage(savedMessage, `domain (\`${thisUrl.hostname}\`) found in blacklist`);
|
this.censorMessage(savedMessage, `domain (\`${thisUrl.hostname}\`) found in blacklist`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -204,7 +208,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
for (const token of blockedTokens) {
|
for (const token of blockedTokens) {
|
||||||
if (savedMessage.data.content.toLowerCase().includes(token.toLowerCase())) {
|
if (savedMessage.data.content.toLowerCase().includes(token.toLowerCase())) {
|
||||||
this.censorMessage(savedMessage, `blocked token (\`${token}\`) found`);
|
this.censorMessage(savedMessage, `blocked token (\`${token}\`) found`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +218,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
const regex = new RegExp(`\\b${escapeStringRegexp(word)}\\b`, "i");
|
const regex = new RegExp(`\\b${escapeStringRegexp(word)}\\b`, "i");
|
||||||
if (regex.test(savedMessage.data.content)) {
|
if (regex.test(savedMessage.data.content)) {
|
||||||
this.censorMessage(savedMessage, `blocked word (\`${word}\`) found`);
|
this.censorMessage(savedMessage, `blocked word (\`${word}\`) found`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,18 +228,36 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
|
||||||
const regex = new RegExp(regexStr, "i");
|
const regex = new RegExp(regexStr, "i");
|
||||||
if (regex.test(savedMessage.data.content)) {
|
if (regex.test(savedMessage.data.content)) {
|
||||||
this.censorMessage(savedMessage, `blocked regex (\`${regexStr}\`) found`);
|
this.censorMessage(savedMessage, `blocked regex (\`${regexStr}\`) found`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async onMessageCreate(savedMessage: SavedMessage) {
|
async onMessageCreate(savedMessage: SavedMessage) {
|
||||||
if (savedMessage.is_bot) return;
|
if (savedMessage.is_bot) return;
|
||||||
this.applyFiltersToMsg(savedMessage);
|
const lock = await this.locks.acquire(`message-${savedMessage.id}`);
|
||||||
|
|
||||||
|
const wasDeleted = await this.applyFiltersToMsg(savedMessage);
|
||||||
|
|
||||||
|
if (wasDeleted) {
|
||||||
|
lock.interrupt();
|
||||||
|
} else {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onMessageUpdate(savedMessage: SavedMessage) {
|
async onMessageUpdate(savedMessage: SavedMessage) {
|
||||||
if (savedMessage.is_bot) return;
|
if (savedMessage.is_bot) return;
|
||||||
this.applyFiltersToMsg(savedMessage);
|
const lock = await this.locks.acquire(`message-${savedMessage.id}`);
|
||||||
|
|
||||||
|
const wasDeleted = await this.applyFiltersToMsg(savedMessage);
|
||||||
|
|
||||||
|
if (wasDeleted) {
|
||||||
|
lock.interrupt();
|
||||||
|
} else {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue