Added graphql calls every time a feature is triggered, the bot responds to a command, and deletes a link

This commit is contained in:
2025-11-27 18:17:34 -05:00
parent 59ff831b45
commit 926df56972
6 changed files with 296 additions and 294 deletions

View File

@@ -1,6 +1,8 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
const composer = new Composer<Context>();
@@ -14,6 +16,8 @@ feature.hears(
/^\/botInfo/,
logHandle("bot-info-command"),
async (ctx: Context) => {
await urql.mutation(increment, { trigger: true });
// Checks if the context includes a message property.
if (ctx.msg && ctx.chat && ctx.msg.from) {
// Doesn't respond to regular users in groups. This is to prevent users spamming the command.
@@ -25,6 +29,11 @@ feature.hears(
}
}
await urql
.mutation(increment, { command: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the message.
await ctx.reply(
`I am a bot designed to delete any Twitter/X and Meta links along with corresponding reformatting services\\. I now check embedded links\\!`,
@@ -48,13 +57,15 @@ feature.hears(
}
);
return await ctx.reply(
`Lucid decided it was time to make a statement\\. Twitter\\/X is not safe anymore\\. The fandom doesn\\'t need it\\. It should be boycotted and not allowed anymore\\. Thus decided to try making this bot public-use to test it's viability\\. Feel free to add this bot into your own group\\. All it needs is an admin role with the permission to delete messages\\.`,
`Lucid decided it was time to make a statement\\. Twitter\\/X is not safe anymore\\. The fandom doesn\\'t need it\\. It should be boycotted and not allowed anymore\\. Thus decided to try making this bot public\\-use to test it's viability\\. Feel free to add this bot into your own group\\. All it needs is an admin role with the permission to delete messages\\.`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
);
}
});
}
}
);

View File

@@ -3,6 +3,10 @@ import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import metaLinkCheck from "#root/lib/metaLinkCheck.js";
import twitterLinkCheck from "#root/lib/twitterLinkCheck.js";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
const composer = new Composer<Context>();
@@ -13,20 +17,16 @@ const feature = composer.chatType(["group", "supergroup"]);
* The trigger is anytime an embedded url is detected.
*/
feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
if (ctx.chat && ctx.msg) {
// Pulling the group IDs from the env variables.
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
await urql.mutation(increment, { trigger: true });
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 groupName = ctx.chat?.title;
const groupID = ctx.chat?.id;
const groupUsername = ctx.chat?.username;
let deletedLinks = 0;
const username = ctx.msg.from?.username;
if (flag) {
// Filters every message/caption entity that is a url into a new array.
const embeds = ctx.msg.entities
? ctx.msg.entities.filter(e => e.type === "text_link")
@@ -37,9 +37,7 @@ feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
// If the caption embeds array isn't empty filter through them to check if any is a Twitter/X or Meta url.
if (captionEmbeds !== null && captionEmbeds.length) {
const metaLinks = captionEmbeds.filter(({ url }) =>
metaLinkCheck(url)
);
const metaLinks = captionEmbeds.filter(({ url }) => metaLinkCheck(url));
const twitterLinks = captionEmbeds.filter(({ url }) =>
twitterLinkCheck(url)
);
@@ -48,86 +46,136 @@ feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
if (metaLinks.length && twitterLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
await ctx.reply(
`@${username} Twitter and X links along with reformatting services for Twitter posts are not allowed here\\. Also 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\\.\n\nIf this was forwarded from a channel consider forwarding without the caption so the media isn't deleted\\.`,
{ parse_mode: "MarkdownV2" }
);
}
});
}
// Handle action and response if only meta links are detected.
if (metaLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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\\\n\nIf this was forwarded from a channel consider forwarding without the caption so the media isn't deleted\\.`,
{ parse_mode: "MarkdownV2" }
);
}
});
}
// Handle action and response if only Twitter/X links are detected.
if (twitterLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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\\.\n\nIf this was forwarded from a channel consider forwarding without the caption so the media isn't deleted\\.`,
{ parse_mode: "MarkdownV2" }
);
}
});
}
}
// If the embeds array isn't empty filter through them to check if any is a Twitter/X or Meta url.
if (embeds !== null && embeds.length) {
const metaLinks = embeds.filter(({ url }) => metaLinkCheck(url));
const twitterLinks = embeds.filter(({ url }) =>
twitterLinkCheck(url)
);
const twitterLinks = embeds.filter(({ url }) => twitterLinkCheck(url));
// Handle action and response if both meta and Twitter/X links are detected.
if (metaLinks.length && twitterLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
await ctx.reply(
`@${username} Twitter and X links along with reformatting services for Twitter posts are not allowed here\\. Also 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" }
);
}
});
}
// Handle action and response if only meta links are detected.
if (metaLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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" }
);
}
});
}
// Handle action and response if only Twitter/X links are detected.
if (twitterLinks.length) {
// Deletes the offending message.
ctx.msg.delete();
deletedLinks++;
await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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 (!GROUP_IDS) {
console.info("Group IDS:", process.env.GROUP_IDS);
return await ctx.reply(
`There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
if (deletedLinks) {
return await urql
.mutation(addGroup, { groupID, groupName, groupUsername })
.toPromise()
.then(() =>
urql.mutation(incrementGroup, {
groupID,
linksDeleted: deletedLinks
})
);
}
}

View File

@@ -1,6 +1,8 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
const composer = new Composer<Context>();
@@ -11,12 +13,19 @@ const feature = composer.chatType(["group", "supergroup", "private"]);
* The trigger is the command "/help"
*/
feature.hears(/^\/help/, logHandle("help"), async (ctx: Context) => {
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
await urql.mutation(increment, { trigger: true });
// const GROUP_IDS = process.env.GROUP_IDS
// ? process.env.GROUP_IDS.split(",")
// : undefined;
// Checks there is a chat and msg property in the context.
if (ctx.chat && ctx.msg && ctx.msg.from) {
await urql
.mutation(increment, { command: true })
.toPromise()
.then(async () => {
if (ctx.msg && ctx.chat && ctx.msg.from) {
// Checks if the chat is private
if (ctx.chat.type === "private") {
// Responds with message.
@@ -67,58 +76,8 @@ feature.hears(/^\/help/, logHandle("help"), async (ctx: Context) => {
reply_parameters: { message_id: ctx.msg.message_id }
}
);
// Checks if the whitelist is set up.
// if (GROUP_IDS !== undefined) {
// const groupID = ctx.chat.id;
// const flag = GROUP_IDS.includes(`${groupID}`);
// // Checks if the chat is in the whitelist.
// if (flag) {
// // Responds with the command list.
// return await ctx.reply(
// `**Here are the available commands you can use:**\n\n/getGroupID _ADMIN ONLY_ \\- Replies with the ID of the group I am in\\.\n\n/isWKCGRoup _ADMIN ONLY_ \\- Checks if this group's ID is on the whitelist and responds accordingly\\.\n\n/botInfo _Private Command_\\- Info about me and how to fork me to deploy for your own use\\.\n\n/help\\- Displays this help message\\.`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// );
// }
// Checks if the chat is not in the whitelist.
// if (!flag) {
// await ctx
// // Responds with the command list with a warning that the available commands are limited.
// .reply(
// `**Since this is not a whitelisted group the features are limited\\!\\!**\n\nHere are the available commands you can use:\n\n/isWKCGRoup _ADMIN ONLY_\\- Checks if this group's ID is on the whitelist and responds accordingly\\.\n\n/botInfo _Private Command_\\- Info about me and how to fork me to deploy for your own use\\.\n\n/help \\- Displays this help message\\.`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// )
// .then(() => {});
// // Sends a follow-up message with information about the bot.
// return await ctx.reply(
// `This group is NOT in the whitelisted and is NOT a part of the WKC Telegram groups/communities\\. I am a bot designed to delete any Twitter/X and Meta links along with corresponding reformatting services within whitelisted groups\\. You can fork me from this link: https://github\\.com/lucid\\-creations\\-media/no\\-twitter\\-bot and deploy me for use in your own groups\\!`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// );
// }
// }
// Checks if the whitelist is not set up.
// if (!GROUP_IDS) {
// // Sends a warning that the whitelist is not set up or the bot cannot access it.
// return await ctx.reply(
// `There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// );
// }
}
});
}
});

View File

@@ -2,6 +2,10 @@ 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";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
const composer = new Composer<Context>();
@@ -15,52 +19,37 @@ feature.hears(
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;
await urql.mutation(increment, { trigger: true });
if (ctx.chat && ctx.msg) {
const username = ctx.msg.from?.username;
// Deletes the offending message.
ctx.msg.delete();
return await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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" }
);
const groupName = ctx.chat?.title;
const groupID = ctx.chat?.id;
const groupUsername = ctx.chat?.username;
return await urql
.mutation(addGroup, { groupID, groupName, groupUsername })
.toPromise()
.then(() =>
urql.mutation(incrementGroup, { groupID, linksDeleted: 1 })
);
}
});
}
// 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();
// // 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);
// return await ctx.reply(
// `There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// );
// }
// }
}
);

View File

@@ -2,6 +2,10 @@ 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";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
const composer = new Composer<Context>();
@@ -15,57 +19,37 @@ feature.hears(
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;
await urql.mutation(increment, { trigger: true });
if (ctx.chat && ctx.msg) {
const username = ctx.msg.from?.username;
// Deletes the offending message.
ctx.msg.delete();
return await urql
.mutation(increment, { link: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Replies to the user informing them of the action.
return await ctx.reply(
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" }
);
const groupName = ctx.chat?.title;
const groupID = ctx.chat?.id;
const groupUsername = ctx.chat?.username;
return await urql
.mutation(addGroup, { groupID, groupName, groupUsername })
.toPromise()
.then(() =>
urql.mutation(incrementGroup, { groupID, linksDeleted: 1 })
);
}
});
}
// 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();
// // 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:",
// process.env.GROUP_IDS,
// GROUP_IDS,
// GROUP_IDS !== undefined
// );
// return await ctx.reply(
// `There was a problem retrieving the whitelist\\. Check the env variables and try again\\.`,
// {
// parse_mode: "MarkdownV2",
// reply_parameters: { message_id: ctx.msg.message_id }
// }
// );
// }
// }
}
);

View File

@@ -1,6 +1,8 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import urql from "#root/lib/urql.js";
import increment from "#root/lib/graphql/mutations/incrimentMutation.js";
const composer = new Composer<Context>();
@@ -9,12 +11,21 @@ const feature = composer.chatType("private");
* What triggers this feature and adds to the log when it has been triggered.
* The trigger is the command "/start" or "start"
*/
feature.command("start", logHandle("command-start"), ctx => {
feature.command("start", logHandle("command-start"), async ctx => {
await urql.mutation(increment, { trigger: true });
await urql
.mutation(increment, { command: true })
.toPromise()
.then(async () => {
if (ctx.msg) {
// Responds with information about the bot.
return ctx.reply(
`Welcome\\! I am a bot created by Lucid for [Werewolf Kid Creations](https://werewolfkid.monster/) I am designed to delete any Twitter/X and Meta links along with corresponding reformatting services within groups\\. I also check embedded links and forwarded messages\\! I am currently in a public beta mode\\! Simply add me to a group and make me an admin with the permission to delete messages\\. From there I will start deleting any Twitter\\/X and Meta links I detect\\.`,
{ parse_mode: "MarkdownV2" }
);
}
});
});
export { composer as welcomeFeature };