feature complete v1

This commit is contained in:
CatCraftYT 2024-03-07 00:27:23 +10:30
parent f2f31dcded
commit 636f51ac95

View file

@ -1,75 +1,36 @@
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { Settings } from "@api/Settings"; import { Settings, definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import { MessageJSON } from "discord-types/general"; import { MessageJSON } from "discord-types/general";
// o: has ._channelMessages and .commit() var blockedKeywords: Array<RegExp>;
// o is ChannelMessages
// o is h class
// foreach iterates through _messages
// D is RelationshipStore?
// o.default.forEach(e=>{o.default.commit(e.reset(e.map(e=>e.set('blocked',$self.containsBlockedKeywords($1)))))}) 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({ export default definePlugin({
name: "BlockKeywords", name: "BlockKeywords",
description: "Blocks messages containing specific user-defined keywords, as if the user sending them was blocked.", description: "Blocks messages containing specific user-defined keywords, as if the user sending them was blocked.",
authors: [Devs.catcraft], authors: [Devs.catcraft],
patches: [ patches: [
/*
{
// alternative find string: "d.getPrivateChannelIntegrationRemovedSystemMessageASTContent)({"
find: '.MessageTypes.PRIVATE_CHANNEL_INTEGRATION_ADDED)return(',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
find: 'displayName="MessageStore"',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
find: 'displayName="ReadStateStore"',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
find: 'default.Messages.NEW_MESSAGES_ESTIMATED_SUMMARIES):(',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
find: '.default.Messages.BLOCKED_MESSAGE_COUNT:(',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
find: '.displayName="ChannelPinsStore"',
replacement: {
match: /default\.isBlocked\((.{1,2})\.author.id\)/g,
replace: "$&||$self.containsBlockedKeywords($1)"
}
},
{
// alternative find string: "()},MESSAGE_CREATE:function"
// replace arg with arg.set('blocked', $self.containsBlockedKeywords(arg))
find: 'LOAD_MESSAGE_INTERACTION_DATA_SUCCESS:function',
replacement: {
match: /(\)\),.{0,2}\.default\.commit\()(.{0,2})(\)\},MESSAGE_SEND_FAILED:function)/g,
replace: "$1$self.blockMessagesWithKeywords($2)$3"
}
},
*/
{ {
find: '.default("ChannelMessages")', find: '.default("ChannelMessages")',
replacement: { replacement: {
@ -79,25 +40,49 @@ export default definePlugin({
}, },
], ],
options: { settings,
blockedWords: {
type: OptionType.STRING, start() {
description: "Comma-seperated list of words to block.", let blockedWordsList: Array<string> = Settings.plugins.BlockKeywords.blockedWords.split(",");
default: "" if (Settings.plugins.BlockKeywords.useRegex) {
blockedKeywords = blockedWordsList.map((word) => {
return new RegExp(word, Settings.plugins.BlockKeywords.caseSensitive ? "" : "i");
});
} }
else {
blockedKeywords = blockedWordsList.map((word) => {
// escape regex chars in word https://stackoverflow.com/a/6969486
return new RegExp(`\\b${word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, Settings.plugins.BlockKeywords.caseSensitive ? "" : "i");
});
}
console.log(blockedKeywords);
}, },
containsBlockedKeywords(message: MessageJSON) { containsBlockedKeywords(message: MessageJSON) {
if (!Settings.plugins.BlockKeywords.blockedWords) { return false; } if (blockedKeywords.length === 0) { return false; }
const blockedWordsList: Array<string> = Settings.plugins.BlockKeywords.blockedWords.split(",");
// can't use blockedWordsList.forEach because we need to return from inside the loop // can't use forEach because we need to return from inside the loop
for (let wordIndex = 0; wordIndex < blockedWordsList.length; wordIndex++) { // message content loop
if (message.content.includes(blockedWordsList[wordIndex])) { for (let wordIndex = 0; wordIndex < blockedKeywords.length; wordIndex++) {
if (blockedKeywords[wordIndex].test(message.content)) {
return true; return true;
} }
} }
// embed content loop (e.g. twitter embeds)
for (let embedIndex = 0; embedIndex < message.embeds.length; embedIndex++) {
const embed = message.embeds[embedIndex];
if (embed["rawDescription"] == null || embed["rawTitle"] == null) { continue; }
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; return false;
}, },