-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Plugin] BlockKeywords #2238
Open
CatCraftYT
wants to merge
26
commits into
Vendicated:main
Choose a base branch
from
CatCraftYT:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−0
Open
[Plugin] BlockKeywords #2238
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f2f31dc
first working version :3
CatCraftYT 636f51a
feature complete v1
CatCraftYT d930601
removed old if statement and added caseSensitiveFlag const
CatCraftYT 5ce74e5
fixed issue preventing messages from blocked people actually being bl…
CatCraftYT 53282ff
synced with upstream
CatCraftYT c3e619a
Merge with upstream
CatCraftYT a90bafd
Merge upstream changes
CatCraftYT 7921b4d
Merge upstream changes
CatCraftYT b258028
Merge branch 'main' into main
CatCraftYT 5015515
Merge remote-tracking branch 'upstream/main'
CatCraftYT 734e4bc
Fixed for new update
CatCraftYT bd1400c
Merge remote-tracking branch 'upstream/main'
CatCraftYT 8c5c4c7
Merge remote-tracking branch 'upstream/main'
CatCraftYT 5d64c2a
Merge branch 'Vendicated:main' into main
CatCraftYT 52c35cb
Merge branch 'Vendicated:main' into main
CatCraftYT 7c4c04d
Merge branch 'Vendicated:main' into main
CatCraftYT ced2e51
Merge branch 'Vendicated:main' into main
CatCraftYT 49c3afe
Merge branch 'Vendicated:main' into main
CatCraftYT 5c5582c
Merge branch 'Vendicated:main' into main
CatCraftYT 78334d1
Merge branch 'Vendicated:main' into main
CatCraftYT a4a3a2b
Merge branch 'Vendicated:main' into main
CatCraftYT 8f65f70
Merge branch 'Vendicated:main' into main
CatCraftYT db0d208
Merge branch 'Vendicated:main' into main
CatCraftYT f37de2b
Merge branch 'Vendicated:main' into main
CatCraftYT 74cc1a1
Merge branch 'Vendicated:main' into main
CatCraftYT 40fd44a
Merge branch 'Vendicated:main' into main
CatCraftYT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import definePlugin, { OptionType } from "@utils/types"; | ||
import { Settings, definePluginSettings } from "@api/Settings"; | ||
import { Devs } from "@utils/constants"; | ||
import { MessageJSON } from "discord-types/general"; | ||
|
||
var blockedKeywords: Array<RegExp>; | ||
|
||
const settings = definePluginSettings({ | ||
blockedWords: { | ||
type: OptionType.STRING, | ||
description: "Comma-seperated list of words to block", | ||
default: "", | ||
restartNeeded: true | ||
}, | ||
useRegex: { | ||
type: OptionType.BOOLEAN, | ||
description: "Use each value as a regular expression when checking message content (advanced)", | ||
default: false, | ||
restartNeeded: true | ||
}, | ||
caseSensitive: { | ||
type: OptionType.BOOLEAN, | ||
description: "Whether to use a case sensitive search or not", | ||
default: false, | ||
restartNeeded: true | ||
} | ||
}); | ||
|
||
export default definePlugin({ | ||
name: "BlockKeywords", | ||
description: "Blocks messages containing specific user-defined keywords, as if the user sending them was blocked.", | ||
authors: [Devs.catcraft], | ||
patches: [ | ||
{ | ||
find: '.default("ChannelMessages")', | ||
replacement: { | ||
match: /static commit\((.{1,2})\){/g, | ||
replace: "$&$1=$self.blockMessagesWithKeywords($1);" | ||
} | ||
}, | ||
], | ||
|
||
settings, | ||
|
||
start() { | ||
let blockedWordsList: Array<string> = Settings.plugins.BlockKeywords.blockedWords.split(","); | ||
const caseSensitiveFlag = Settings.plugins.BlockKeywords.caseSensitive ? "" : "i"; | ||
|
||
if (Settings.plugins.BlockKeywords.useRegex) { | ||
blockedKeywords = blockedWordsList.map((word) => { | ||
return new RegExp(word, caseSensitiveFlag); | ||
}); | ||
} | ||
else { | ||
blockedKeywords = blockedWordsList.map((word) => { | ||
// escape regex chars in word https://stackoverflow.com/a/6969486 | ||
return new RegExp(`\\b${word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, caseSensitiveFlag); | ||
}); | ||
} | ||
console.log(blockedKeywords); | ||
}, | ||
|
||
containsBlockedKeywords(message: MessageJSON) { | ||
if (blockedKeywords.length === 0) { return false; } | ||
|
||
// can't use forEach because we need to return from inside the loop | ||
// message content loop | ||
for (let wordIndex = 0; wordIndex < blockedKeywords.length; wordIndex++) { | ||
if (blockedKeywords[wordIndex].test(message.content)) { | ||
return true; | ||
} | ||
} | ||
|
||
// embed content loop (e.g. twitter embeds) | ||
for (let embedIndex = 0; embedIndex < message.embeds.length; embedIndex++) { | ||
const embed = message.embeds[embedIndex]; | ||
for (let wordIndex = 0; wordIndex < blockedKeywords.length; wordIndex++) { | ||
// doing this because undefined strings get converted to the string "undefined" in regex tests | ||
const descriptionHasKeywords = embed["rawDescription"] != null && blockedKeywords[wordIndex].test(embed["rawDescription"]); | ||
const titleHasKeywords = embed["rawTitle"] != null && blockedKeywords[wordIndex].test(embed["rawTitle"]); | ||
if (descriptionHasKeywords || titleHasKeywords) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}, | ||
|
||
blockMessagesWithKeywords(messageList: any) { | ||
return messageList.reset(messageList.map( | ||
message => message.set("blocked", message["blocked"] || this.containsBlockedKeywords(message)) | ||
)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a restart actually needed for all of these settings? If not, an onChange function that updates
blockedKeywords
could instead be added to these setting definitions. Maybe the code instart()
could be moved to a top-level function that is used as the value ofstart
and theonChange
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a new branch on my repo with changes that almost implement this, but it only actually visibly blocks the messages when the user switches channels. The messages in the currently viewed channel aren't updated when the message list is updated for some reason. The block feature gets around this but I'm not sure how rn