Equicord/src/equicordplugins/grammarFix/index.ts

99 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-07-18 03:07:01 -04:00
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import {
addPreSendListener,
removePreSendListener,
SendListener,
} from "@api/MessageEvents";
2024-10-01 11:56:19 -04:00
import { definePluginSettings } from "@api/Settings";
2024-07-18 03:07:01 -04:00
import { EquicordDevs } from "@utils/constants";
2024-10-01 11:56:19 -04:00
import definePlugin, { OptionType } from "@utils/types";
const settings = definePluginSettings({
autoCapitalization: {
type: OptionType.BOOLEAN,
description: "Auto Capitalization to the first letter",
},
autoPunctuation: {
type: OptionType.BOOLEAN,
description: "Auto Punctuation at the end of a sentence",
},
autoWordReplacement: {
type: OptionType.BOOLEAN,
description: "Auto Capitalizes the first letter",
},
});
2024-07-18 03:07:01 -04:00
const getPresend = dictionary => {
2024-07-18 03:07:01 -04:00
const presendObject: SendListener = (_, msg) => {
msg.content = msg.content.trim();
if (!msg.content.includes("```") && /\w/.test(msg.content.charAt(0))) {
2024-10-01 11:56:19 -04:00
if (settings.store.autoWordReplacement) {
const re = new RegExp(
`(^|(?<=[^A-Z0-9]+))(${Object.keys(dictionary)
.map(k => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
2024-10-01 11:56:19 -04:00
.join("|")})((?=[^A-Z0-9]+)|$)`,
"gi",
2024-10-01 11:56:19 -04:00
);
if (re !== null) {
msg.content = msg.content.replace(re, match => {
2024-10-01 11:56:19 -04:00
return dictionary[match.toLowerCase()] || match;
});
}
2024-07-18 03:07:01 -04:00
}
2024-10-01 11:56:19 -04:00
if (settings.store.autoPunctuation) {
if (
/[A-Z0-9]/i.test(msg.content.charAt(msg.content.length - 1))
) {
if (
!msg.content.startsWith(
"http",
msg.content.lastIndexOf(" ") + 1,
)
)
2024-10-01 11:56:19 -04:00
msg.content += ".";
}
2024-07-18 03:07:01 -04:00
}
// Ensure sentences are capitalized after punctuation
2024-10-01 11:56:19 -04:00
if (settings.store.autoCapitalization) {
msg.content = msg.content.replace(/([.!?])\s*(\w)/g, match =>
match.toUpperCase(),
2024-10-01 11:56:19 -04:00
);
2024-07-18 03:07:01 -04:00
2024-10-01 11:56:19 -04:00
// Ensure the first character of the entire message is capitalized
if (!msg.content.startsWith("http")) {
msg.content =
msg.content.charAt(0).toUpperCase() +
msg.content.slice(1);
2024-10-01 11:56:19 -04:00
}
2024-07-18 03:07:01 -04:00
}
}
};
return presendObject;
};
export default definePlugin({
name: "GrammarFix",
description: "Automatic punctuation, capitalization, and word replacement.",
authors: [EquicordDevs.unstream],
dependencies: ["MessageEventsAPI"],
2024-10-01 11:56:19 -04:00
settings,
2024-07-18 03:07:01 -04:00
async start() {
let dictionary = await fetch(
"https://raw.githubusercontent.com/wont-stream/dictionary/3d52fecd9aca5dfee0fcde0df2c2af357f977df7/index.min.json",
2024-07-18 03:07:01 -04:00
);
dictionary = await dictionary.json();
addPreSendListener(getPresend(dictionary));
},
stop() {
removePreSendListener(getPresend({}));
},
});