From aecbaf4f464407060f8ab21ff3ef83f769320734 Mon Sep 17 00:00:00 2001 From: wrench Date: Thu, 28 Mar 2024 23:42:17 +0530 Subject: [PATCH] Added Allowed users list Co-authored-by: Deekshith SH --- .gitignore | 4 ++-- README.md | 5 ++++- WebStreamer/bot/plugins/start.py | 6 ++++++ WebStreamer/utils/bot_utils.py | 15 +++++++++++++-- WebStreamer/vars.py | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index cd2cf09..7d694f3 100644 --- a/.gitignore +++ b/.gitignore @@ -117,8 +117,8 @@ dmypy.json *.session *.session-journal -.env +*.env test.py /test -streambot.log.* \ No newline at end of file +streambot.log.* diff --git a/README.md b/README.md index b5eabd6..49d7cb6 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,10 @@ you may also add as many as bots you want. (max limit is not tested yet) `SESSION_NAME` : Name for the Database created on your MongoDB. Defaults to `F2LxBot` -`BANNED_CHANNELS` : Put IDs of Banned Channels where bot will not work. You can add multiple IDs & separate with Space. +`ALLOWED_USERS`: The user Telegram IDs of users to which the bot only reply to. +> **Note** +> Leave this field empty and anyone will be able to use your bot instance. +> You may also add multiple users by adding the IDs separated by comma (,) `KEEP_ALIVE` : If you want to make the server ping itself every diff --git a/WebStreamer/bot/plugins/start.py b/WebStreamer/bot/plugins/start.py index 4f950ee..1ee04d9 100644 --- a/WebStreamer/bot/plugins/start.py +++ b/WebStreamer/bot/plugins/start.py @@ -56,6 +56,8 @@ async def help_handler(bot, message): @StreamBot.on_message(filters.command('myfiles') & filters.private) async def my_files(bot: Client, message: Message): + if not await validate_user(message): + return user_files, total_files=await db.find_files(message.from_user.id, [1,10]) file_list=[] @@ -89,6 +91,8 @@ async def tos_handler(bot: Client, message: Message): @StreamBot.on_message(filters.command('info') & filters.private) async def info_handler(bot: Client, message: Message): lang = Language(message) + if not await validate_user(message, lang): + return i_cmd=message.text.split() if (message.from_user.id == Var.OWNER_ID) and (len(i_cmd) > 1): message.from_user.id=int(i_cmd[1]) @@ -101,6 +105,8 @@ async def info_handler(bot: Client, message: Message): @StreamBot.on_message(filters.command('getfile') & filters.private & filters.user(Var.OWNER_ID)) async def getfile(bot: Client, message: Message): + if not await validate_user(message): + return usr_cmd=message.text.split() if len(usr_cmd) < 2: return await message.reply_text("Invalid Format\nUsage: `/getfile _id`") diff --git a/WebStreamer/utils/bot_utils.py b/WebStreamer/utils/bot_utils.py index 71afbe8..ee3bfe6 100644 --- a/WebStreamer/utils/bot_utils.py +++ b/WebStreamer/utils/bot_utils.py @@ -89,13 +89,24 @@ async def is_user_accepted_tos(message: Message) -> bool: return False return True -async def validate_user(message: Message, lang) -> bool: - if await is_user_banned(message, lang): +async def is_allowed(message: Message): + if Var.ALLOWED_USERS and not ((str(message.from_user.id) in Var.ALLOWED_USERS) or (message.from_user.username in Var.ALLOWED_USERS)): + await message.reply("You are not in the allowed list of users who can use me.", quote=True) + return False + return True + +async def validate_user(message: Message, lang=None) -> bool: + if not await is_allowed(message): return False await is_user_exist(message) if Var.TOS: if not await is_user_accepted_tos(message): return False + + if not lang: + lang = Language(message) + if await is_user_banned(message, lang): + return False if Var.FORCE_UPDATES_CHANNEL: if not await is_user_joined(message,lang): return False diff --git a/WebStreamer/vars.py b/WebStreamer/vars.py index 2b714a0..c34e605 100644 --- a/WebStreamer/vars.py +++ b/WebStreamer/vars.py @@ -32,8 +32,8 @@ class Var(object): SESSION_NAME = str(environ.get('SESSION_NAME', 'F2LxBot')) FORCE_UPDATES_CHANNEL = environ.get('FORCE_UPDATES_CHANNEL', False) FORCE_UPDATES_CHANNEL = True if str(FORCE_UPDATES_CHANNEL).lower() == "true" else False + ALLOWED_USERS = [x.strip("@ ") for x in str(environ.get("ALLOWED_USERS", "") or "").split(",") if x.strip("@ ")] - BANNED_CHANNELS = list(set(int(x) for x in str(environ.get("BANNED_CHANNELS", "-1001296894100")).split())) KEEP_ALIVE = str(environ.get("KEEP_ALIVE", "0").lower()) in ("1", "true", "t", "yes", "y") IMAGE_FILEID = environ.get('IMAGE_FILEID', "https://deekshith.eu.org/static/MyFiles.png") TOS = environ.get("TOS", None)