Refactor for centralized regex check module and regex syntax. Added comments.

This commit is contained in:
Lucid Kobold
2025-07-04 18:34:36 -04:00
parent 9d8c026670
commit 72a6e53d49
4 changed files with 69 additions and 6 deletions

View File

@@ -1,37 +1,47 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { metaRegex } from "#root/lib/metaLinkCheck.js";
const composer = new Composer<Context>();
const feature = composer.chatType(["group", "supergroup"]);
/**
* What triggers this feature and adds to the log when it has been triggered.
* The trigger uses the global Twitter regex to detect Twitter and X links within messages.
*/
feature.hears(
/(facebook\.com|meta\.com|instagram\.com|threads\.net|whatsapp\.com)/gi,
metaRegex,
logHandle("blacklist-detection-meta"),
async (ctx: Context) => {
// Pulling the group IDs from the env variables.
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
if (ctx.chat && ctx.msg) {
if (GROUP_IDS !== undefined) {
// Checking if the message is from a whitelisted group.
const groupID = ctx.chat.id;
const flag = GROUP_IDS.includes(`${groupID}`);
const username = ctx.msg.from?.username;
if (flag) {
// Deletes the offending message.
ctx.msg.delete();
await ctx.reply(
// Replies to the user informing them of the action.
return await ctx.reply(
`@${username} Facebook and meta links along with with links to meta\\-owned services are not allowed here\\. Please consider sharing the media directly or from other social media sources or websites\\. No administration action was taken against you other than the message being deleted\\.`,
{ parse_mode: "MarkdownV2" }
);
}
}
// If the env variables are misconfigured an error is sent to the group.
if (!GROUP_IDS) {
console.info("Group IDS:", process.env.GROUP_IDS);
await ctx.reply(
return await ctx.reply(
`There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
{
parse_mode: "MarkdownV2",

View File

@@ -1,34 +1,44 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { twitterRegex } from "#root/lib/twitterLinkCheck.js";
const composer = new Composer<Context>();
const feature = composer.chatType(["group", "supergroup"]);
/**
* What triggers this feature and adds to the log when it has been triggered.
* The trigger uses the global Twitter regex to detect Twitter and X links within messages.
*/
feature.hears(
/(x\.com|twitter\.com)/gi,
twitterRegex,
logHandle("blacklist-detection-twitter"),
async (ctx: Context) => {
// Pulling the group IDs from the env variables.
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
if (ctx.chat && ctx.msg) {
if (GROUP_IDS !== undefined) {
// Checking if the message is from a whitelisted group.
const groupID = ctx.chat.id;
const flag = GROUP_IDS.includes(`${groupID}`);
const username = ctx.msg.from?.username;
if (flag) {
// Deletes the offending message.
ctx.msg.delete();
await ctx.reply(
// Replies to the user informing them of the action.
return await ctx.reply(
`@${username} Twitter and X links along with reformatting services for Twitter posts are not allowed here\\. Please consider sharing the media directly or from other social media sources or websites\\. No administration action was taken against you other than the message being deleted\\.`,
{ parse_mode: "MarkdownV2" }
);
}
}
// If the env variables are misconfigured an error is sent to the group.
if (!GROUP_IDS) {
console.info(
"Group IDS:",
@@ -36,7 +46,7 @@ feature.hears(
GROUP_IDS,
GROUP_IDS !== undefined
);
await ctx.reply(
return await ctx.reply(
`There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
{
parse_mode: "MarkdownV2",

22
src/lib/metaLinkCheck.ts Normal file
View File

@@ -0,0 +1,22 @@
// Global regex used to detect all meta services.
const metaRegex =
/(facebook\.com|meta\.com|instagram\.com|threads\.net|whatsapp\.com)/gi;
/**
* This function will check if a url matches Meta services links using regex.
*
* @param linkUrl representing a suspected blacklisted url
* @returns flag
*/
const metaLinkCheck = (linkUrl: string): boolean => {
let flag = false;
if (linkUrl.match(metaRegex)) {
flag = true;
}
return flag;
};
export { metaRegex };
export default metaLinkCheck;

View File

@@ -0,0 +1,21 @@
// Global regex used to detect Twitter and X links.
const twitterRegex = /(x\.com|twitter\.com)/gi;
/**
* This function will check if a url matches Twitter/X services links using regex.
*
* @param linkUrl representing a suspected blacklisted url
* @return flag
*/
const twitterLinkCheck = (linkUrl: string): boolean => {
let flag = false;
if (linkUrl.match(twitterRegex)) {
flag = true;
}
return flag;
};
export { twitterRegex };
export default twitterLinkCheck;