admin-protections #69

Merged
werewolfkid merged 9 commits from admin-protections into main 2025-09-29 01:10:49 -04:00
10 changed files with 190 additions and 177 deletions

View File

@@ -1,20 +0,0 @@
import { chatAction } from "@grammyjs/auto-chat-action";
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
// import { isAdmin } from "#root/bot/filters/is-admin.js";
import { setCommandsHandler } from "#root/bot/handlers/commands/setcommands.js";
import { logHandle } from "#root/bot/helpers/logging.js";
const composer = new Composer<Context>();
const feature = composer.chatType("private");
// .filter(ctx => isAdmin(ctx.config.botAdmins)(ctx));
feature.command(
"setcommands",
logHandle("command-setcommands"),
chatAction("typing"),
setCommandsHandler
);
export { composer as adminFeature };

View File

@@ -4,19 +4,21 @@ import { logHandle } from "#root/bot/helpers/logging.js";
const composer = new Composer<Context>();
const feature = composer.chatType(["group", "supergroup", "private"]);
// const GROUP_IDS = process.env.GROUP_IDS
// ? process.env.GROUP_IDS.split(",")
// : undefined;
const feature = composer.chatType(["private"]);
/**
* What triggers this feature and adds to the log when it has been triggered.
* The trigger is the command "/botInfo"
*/
feature.hears(
"/botInfo",
logHandle("bot-info-command"),
async (ctx: Context) => {
if (ctx.chat && ctx.msg) {
await ctx.reply(
`I am a bot designed to delete any Twitter/X links and reformatting services within groups\\. I now check embedded links\\! By default I only work with whitelisted group IDs\\.\n\nYou can fork me from this link: https://github\\.com/lucid\\-creations\\-media/no\\-twitter\\-bot and deploy me for use in your own groups\\!`,
// Checks if the context includes a message property.
if (ctx.msg) {
// Replies to the message.
return await ctx.reply(
`I am a bot designed to delete any Twitter/X and Meta links along with corresponding reformatting services within whitelisted groups\\. I now check embedded links\\! By default I only work with whitelisted group IDs\\.\n\nYou 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 }

View File

@@ -122,7 +122,7 @@ feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
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

@@ -6,23 +6,45 @@ 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 is the command "/botInfo"
*/
feature.hears(
"/getGroupID",
logHandle("get-group-id"),
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) {
// Checks if the context has a chat, msg, and from property.
if (ctx.chat && ctx.msg && ctx.msg.from) {
if (GROUP_IDS !== undefined) {
const groupID = ctx.chat.id;
const flag = GROUP_IDS.includes(`${groupID}`);
// Checks if the group is whitelisted
if (flag) {
await ctx.reply(`The group id is: \`${groupID}\``, {
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
});
const chatMember = await ctx.getChatMember(ctx.msg.from.id);
// Checks if the user is an admin
if (["creator", "administrator"].includes(chatMember.status)) {
return await ctx.reply(`The group id is: \`${groupID}\``, {
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
});
}
// Send a default message if the user is not an admin
return await ctx.reply(
`You have to be an admin to use this command\\!`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
);
}
}
}

View File

@@ -4,53 +4,63 @@ import { logHandle } from "#root/bot/helpers/logging.js";
const composer = new Composer<Context>();
const feature = composer.chatType(["group", "supergroup"]);
const feature = composer.chatType(["group", "supergroup", "private"]);
feature.hears(
"/help",
logHandle("blacklist-detection"),
async (ctx: Context) => {
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
/**
* What triggers this feature and adds to the log when it has been triggered.
* 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;
if (ctx.chat && ctx.msg) {
if (GROUP_IDS !== undefined) {
const groupID = ctx.chat.id;
const flag = GROUP_IDS.includes(`${groupID}`);
// const username = ctx.msg.from?.username;
if (flag) {
await ctx.reply(
`**Here are the availible commands you can use:**\n\n/getGroupID \\- replied with the ID of the group I am in\\.\n\n/isLCMGRoup \\- Checks if this group's ID is on the whitelist and responds accordingly\\.\n\n/botInfo \\- 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 there is a chat and msg property in the context.
if (ctx.chat && ctx.msg) {
// CHecks if the chat is private
if (ctx.chat.type === "private") {
// Responds with the command list.
return await ctx.reply(
`**Here are the available commands you can use:**\n\n/getGroupID _ADMIN ONLY | Only available in groups_\\- 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 }
}
);
}
if (!flag) {
await ctx.reply(
`**Since this is not a whitelisted group the features are limited\\!\\!**\n\nHere are the availible commands you can use:\n\n/isLCMGRoup \\- Checks if this group's ID is on the whitelist and responds accordingly\\.\n\n/botInfo \\- 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 }
}
);
await ctx.reply(
`This group is NOT in the whitelisted and is NOT a part of the LCM Telegram groups/communities\\. I am a bot designed to delete any Twitter/X links and reformatting services within 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 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 }
}
);
}
if (!GROUP_IDS) {
await ctx.reply(
`There was a problem retrieving the whitelist. Check the env variables and try again\\.`,
// 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 }
@@ -58,7 +68,19 @@ feature.hears(
);
}
}
// 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 }
}
);
}
}
);
});
export { composer as helpCommand };

View File

@@ -1,57 +0,0 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
const composer = new Composer<Context>();
const feature = composer.chatType(["group", "supergroup"]);
feature.hears(
"/isLCMGroup",
logHandle("is-LCM-group"),
async (ctx: Context) => {
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
if (ctx.chat && ctx.msg) {
const groupID = ctx.chat.id;
if (GROUP_IDS !== undefined) {
const flag = GROUP_IDS.includes(`${groupID}`);
if (flag) {
await ctx.reply(
`This group is in the whitelisted and is a part of the LCM Telegram groups/communities\\. I should be deleting any Twitter/X links and reformatting services within this group\\.`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
);
}
if (!flag) {
await ctx.reply(
`This group is NOT in the whitelisted and is NOT a part of the LCM Telegram groups/communities\\. I am a bot designed to delete any Twitter/X links and reformatting services within 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 }
}
);
}
}
if (!GROUP_IDS) {
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 }
}
);
}
}
}
);
export { composer as isLCMGroup };

View File

@@ -0,0 +1,80 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.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 is the command "/isWKCGroup"
*/
feature.hears(
"/isWKCGroup",
logHandle("is-WKC-group"),
async (ctx: Context) => {
// Pulling the group IDs from the env variables.
const GROUP_IDS = process.env.GROUP_IDS
? process.env.GROUP_IDS.split(",")
: undefined;
// Checking that context has chat, msg, and msg.from properties
if (ctx.chat && ctx.msg && ctx.msg.from) {
const groupID = ctx.chat.id;
const chatMember = await ctx.getChatMember(ctx.msg.from.id);
// Checking if the user is an admin.
if (["creator", "administrator"].includes(chatMember.status)) {
if (GROUP_IDS !== undefined) {
const flag = GROUP_IDS.includes(`${groupID}`);
// Checking if the group is whitelisted.
if (flag) {
// Confirming that the group is whitelisted and should be deleting detected messages.
await ctx.reply(
`This group is in the whitelisted and is a part of the WKC Telegram groups/communities\\. I should be deleting any Twitter/X and Meta links along with corresponding reformatting services within this group\\.`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
);
}
if (!flag) {
// Informing the user that features are blocked because the group is not in the whitelist.
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 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 }
}
);
}
}
if (!GROUP_IDS) {
// Altering that the whitelist is not set or the bot cannot access it.
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 }
}
);
}
} else {
// Informing the user that they need to be an admin to use this command.
await ctx.reply(
`You have to be an admin of this group to use this command\\!`,
{
parse_mode: "MarkdownV2",
reply_parameters: { message_id: ctx.msg.message_id }
}
);
}
}
}
);
export { composer as isWKCGroup };

View File

@@ -1,36 +0,0 @@
import { Composer } from "grammy";
import { changeLanguageData } from "#root/bot/callback-data/change-language.js";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { i18n } from "#root/bot/i18n.js";
import { createChangeLanguageKeyboard } from "#root/bot/keyboards/change-language.js";
const composer = new Composer<Context>();
const feature = composer.chatType("private");
feature.command("language", logHandle("command-language"), async ctx => {
return ctx.reply(ctx.t("language-select"), {
reply_markup: await createChangeLanguageKeyboard(ctx)
});
});
feature.callbackQuery(
changeLanguageData.filter(),
logHandle("keyboard-language-select"),
async ctx => {
const { code: languageCode } = changeLanguageData.unpack(
ctx.callbackQuery.data
);
if (i18n.locales.includes(languageCode)) {
await ctx.i18n.setLocale(languageCode);
return ctx.editMessageText(ctx.t("language-changed"), {
reply_markup: await createChangeLanguageKeyboard(ctx)
});
}
}
);
export { composer as languageFeature };

View File

@@ -5,10 +5,14 @@ import { logHandle } from "#root/bot/helpers/logging.js";
const composer = new Composer<Context>();
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 => {
// Responds with information about the bot.
return ctx.reply(
`Welcome\\! I am a bot created by Lucid for [Lucid Creations Media groups\\.](https://community.lucidcreations.media/) I am designed to delete any Twitter/X and Facebook/Meta links, links to reformatting services for Twitter posts, and other Meta products within groups\\. I now check embedded links\\! By default I only work with whitelisted group IDs\\. 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\\!\n\nLucid would consider hosting this bot for public use if crowd\\-funding would cover the hosting cost of the bot\\. Reach out if you would like to help\\.`,
`Welcome\\! I am a bot created by Lucid for [Werewolf Kid Creations Media groups\\.](https://community.lucidcreations.media/) I am designed to delete any Twitter/X and Meta links along with corresponding reformatting services within the WKC groups\\. I now check embedded links\\! By default I only work with whitelisted group IDs\\. 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\\!\n\nLucid would consider hosting this bot for public use if crowd\\-funding would cover the hosting cost of the bot\\. Reach out if you would like to help\\.`,
{ parse_mode: "MarkdownV2" }
);
});

View File

@@ -2,8 +2,6 @@ import type { Context } from "#root/bot/context.js";
import type { Config } from "#root/config.js";
import type { Logger } from "#root/logger.js";
import type { BotConfig } from "grammy";
// import { adminFeature } from "#root/bot/features/admin.js";
import { languageFeature } from "#root/bot/features/language.js";
import { unhandledFeature } from "#root/bot/features/unhandled.js";
import { welcomeFeature } from "#root/bot/features/welcome.js";
import { errorHandler } from "#root/bot/handlers/error.js";
@@ -20,8 +18,8 @@ import { metaBlacklist } from "./features/metaBlacklist.js";
import { botInfoCommand } from "./features/botInfoCommand.js";
import { getGroupIDCommand } from "./features/getGroupIDCommand.js";
import { helpCommand } from "./features/helpCommand.js";
import { isLCMGroup } from "./features/isLCMGroup.js";
import { embedCheck } from "./features/embedCheck.js";
import { isWKCGroup } from "./features/isWKCGroup.js";
interface Dependencies {
config: Config;
@@ -70,13 +68,11 @@ export function createBot(
// Handlers
protectedBot.use(welcomeFeature);
// protectedBot.use(adminFeature);
if (isMultipleLocales) protectedBot.use(languageFeature);
// Commands
protectedBot.use(botInfoCommand);
protectedBot.use(getGroupIDCommand);
protectedBot.use(isLCMGroup);
protectedBot.use(isWKCGroup);
protectedBot.use(helpCommand);
// Blacklist Feature