1
0
Fork 1
mirror of https://github.com/Vendicated/Vencord.git synced 2025-01-10 09:56:24 +00:00

make textreplace work

This commit is contained in:
Elvyra 2025-01-03 01:50:32 +01:00
parent 63eb6358fb
commit 826162952c
2 changed files with 32 additions and 14 deletions

View file

@ -34,7 +34,7 @@ function renderRegisteredPlugins(name: "Guild" | "User" | "Channel", value: any)
...prevState, ...prevState,
[key]: !prevState[key] [key]: !prevState[key]
})); }));
// @ts-ignore (cannot be undefined because settings have to exist for this to be called in the first place) // @ts-ignore (can't be undefined because settings have to exist for this to be called in the first place)
const s = Vencord.Plugins.plugins[plugin].settings.store[setting]; const s = Vencord.Plugins.plugins[plugin].settings.store[setting];
Vencord.Plugins.plugins[plugin].settings.store[setting] = s.includes(value.id) Vencord.Plugins.plugins[plugin].settings.store[setting] = s.includes(value.id)
? s.filter(id => id !== value.id) ? s.filter(id => id !== value.id)

View file

@ -46,9 +46,6 @@ const makeEmptyRule: () => Rule = () => ({
}); });
const makeEmptyRuleArray = () => [makeEmptyRule()]; const makeEmptyRuleArray = () => [makeEmptyRule()];
let stringRules = makeEmptyRuleArray();
let regexRules = makeEmptyRuleArray();
const settings = definePluginSettings({ const settings = definePluginSettings({
replace: { replace: {
type: OptionType.COMPONENT, type: OptionType.COMPONENT,
@ -59,13 +56,13 @@ const settings = definePluginSettings({
<> <>
<TextReplace <TextReplace
title="Using String" title="Using String"
rulesArray={stringRules} rulesArray={settings.store.stringRules}
rulesKey={STRING_RULES_KEY} rulesKey={STRING_RULES_KEY}
update={update} update={update}
/> />
<TextReplace <TextReplace
title="Using Regex" title="Using Regex"
rulesArray={regexRules} rulesArray={settings.store.regexRules}
rulesKey={REGEX_RULES_KEY} rulesKey={REGEX_RULES_KEY}
update={update} update={update}
/> />
@ -74,6 +71,19 @@ const settings = definePluginSettings({
); );
} }
}, },
stringRules: {
type: OptionType.LIST,
hidden: true,
},
regexRules: {
type: OptionType.LIST,
hidden: true,
},
migrated: {
type: OptionType.BOOLEAN,
hidden: true,
default: false,
}
}); });
function stringToRegex(str: string) { function stringToRegex(str: string) {
@ -127,7 +137,8 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps)
if (index === rulesArray.length - 1) return; if (index === rulesArray.length - 1) return;
rulesArray.splice(index, 1); rulesArray.splice(index, 1);
await DataStore.set(rulesKey, rulesArray); if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray];
else settings.store.regexRules = [...rulesArray];
update(); update();
} }
@ -140,7 +151,8 @@ function TextReplace({ title, rulesArray, rulesKey, update }: TextReplaceProps)
if (rulesArray[index].find === "" && rulesArray[index].replace === "" && rulesArray[index].onlyIfIncludes === "" && index !== rulesArray.length - 1) if (rulesArray[index].find === "" && rulesArray[index].replace === "" && rulesArray[index].onlyIfIncludes === "" && index !== rulesArray.length - 1)
rulesArray.splice(index, 1); rulesArray.splice(index, 1);
await DataStore.set(rulesKey, rulesArray); if (rulesKey === STRING_RULES_KEY) settings.store.stringRules = [...rulesArray];
else settings.store.regexRules = [...rulesArray];
update(); update();
} }
@ -211,8 +223,8 @@ function applyRules(content: string): string {
if (content.length === 0) if (content.length === 0)
return content; return content;
if (stringRules) { if (settings.store.stringRules) {
for (const rule of stringRules) { for (const rule of settings.store.stringRules) {
if (!rule.find) continue; if (!rule.find) continue;
if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue;
@ -220,8 +232,8 @@ function applyRules(content: string): string {
} }
} }
if (regexRules) { if (settings.store.stringRulesregexRules) {
for (const rule of regexRules) { for (const rule of settings.store.stringRulesregexRules) {
if (!rule.find) continue; if (!rule.find) continue;
if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue; if (rule.onlyIfIncludes && !content.includes(rule.onlyIfIncludes)) continue;
@ -249,8 +261,14 @@ export default definePlugin({
settings, settings,
async start() { async start() {
stringRules = await DataStore.get(STRING_RULES_KEY) ?? makeEmptyRuleArray(); if (!settings.store.migrated) {
regexRules = await DataStore.get(REGEX_RULES_KEY) ?? makeEmptyRuleArray(); const stringRules = await DataStore.get(STRING_RULES_KEY) ?? makeEmptyRuleArray();
const regexRules = await DataStore.get(REGEX_RULES_KEY) ?? makeEmptyRuleArray();
settings.store.stringRules = stringRules;
settings.store.regexRules = regexRules;
settings.store.migrated = true;
}
this.preSend = addPreSendListener((channelId, msg) => { this.preSend = addPreSendListener((channelId, msg) => {
// Channel used for sharing rules, applying rules here would be messy // Channel used for sharing rules, applying rules here would be messy