diff --git a/src/plugins/keywordNotify/index.tsx b/src/plugins/keywordNotify/index.tsx index 96095a1c5..b32af3370 100644 --- a/src/plugins/keywordNotify/index.tsx +++ b/src/plugins/keywordNotify/index.tsx @@ -10,11 +10,12 @@ import { definePluginSettings } from "@api/Settings"; import { DeleteIcon } from "@components/Icons"; import { Devs } from "@utils/constants"; import { Flex } from "@components/Flex"; -import { TextInput, useState, Forms, Button } from "@webpack/common"; +import { TextInput, useState, Forms, Button, UserStore, UserUtils } from "@webpack/common"; import { useForceUpdater } from "@utils/react"; import "./style.css"; let regexes = []; +let me = null; async function setRegexes(idx: number, reg: string) { regexes[idx] = reg; @@ -90,19 +91,35 @@ export default definePlugin({ authors: [Devs.camila314], description: "Sends a notification if a given message matches certain keywords or regexes", settings, - patches: [{ - find: "isRawMessageMentioned:", - replacement: { - match: /isRawMessageMentioned:function\(\){return (.{1,2}).{1,512}function \1\(.{1,512}?=(.{1,2});return/, - replace: "$& $self.contains($2) ||" - } - }], + patches: [ + { + find: "}_dispatch(", + replacement: { + match: /}_dispatch\((.{1,2}),.{1,2}\){/, + replace: "$&$1=$self.modify($1);" + } + } + ], async start() { regexes = await DataStore.get("KeywordNotify_rules") ?? []; + me = await UserUtils.getUser(UserStore.getCurrentUser().id); }, - contains(e) { - return regexes.some(a => a != "" && e.rawMessage.content.match(new RegExp(a))); + applyRegexes(m) { + if (regexes.some(r => m.content.match(new RegExp(r)))) { + m.mentions.push(me); + } + }, + + modify(e) { + if (e.type == "MESSAGE_CREATE") { + this.applyRegexes(e.message); + } else if (e.type == "LOAD_MESSAGES_SUCCESS") { + for (let msg = 0; msg < e.messages.length; ++msg) { + this.applyRegexes(e.messages[msg]); + } + } + return e; } }); \ No newline at end of file