thehamkercat 5 жил өмнө
parent
commit
f969d05410

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 *.m4a
+spam.py
 *.apk
 *.c
 exec

+ 1 - 1
requirements.txt

@@ -1,6 +1,6 @@
 ffmpeg-python
 gTTS
-carbonnow
+git+git://github.com/OpenRestfulAPI/carbon-now-sh-API-Wrapper@master
 aiohttp
 aiofiles
 cryptography

+ 5 - 1
wbb/__init__.py

@@ -104,11 +104,12 @@ USERBOT_NAME = ""
 USERBOT_USERNAME = ""
 USERBOT_DC_ID = 0
 USERBOT_MENTION = ""
-
+USERBOT_BOT_CHAT_DIFFERENCE = []
 
 def get_info(app, app2):
     global BOT_ID, BOT_NAME, BOT_USERNAME, BOT_DC_ID, BOT_MENTION
     global USERBOT_ID, USERBOT_NAME, USERBOT_USERNAME, USERBOT_DC_ID, USERBOT_MENTION
+    global USERBOT_BOT_CHAT_DIFFERENCE
     getme = app.get_me()
     getme2 = app2.get_me()
     BOT_ID = getme.id
@@ -130,6 +131,9 @@ def get_info(app, app2):
     USERBOT_USERNAME = getme2.username
     USERBOT_MENTION = getme2.mention
     USERBOT_DC_ID = getme2.dc_id
+    all_ub_chats = [i.chat.id for i in app2.iter_dialogs() if i.chat.type in ['group', 'supergroup', 'channel']]
+    ub_b_common_chats = [i.id for i in app2.get_common_chats(BOT_ID)]
+    USERBOT_BOT_CHAT_DIFFERENCE = list(set(all_ub_chats).difference(ub_b_common_chats))
 
 
 print("[INFO]: STARTING BOT CLIENT")

+ 26 - 22
wbb/modules/info.py

@@ -28,6 +28,7 @@ from pyrogram.types import Message
 
 from wbb import SUDOERS, app
 from wbb.core.decorators.errors import capture_err
+from wbb.modules.trust import get_spam_probability
 from wbb.utils.dbfunctions import is_gbanned_user, user_global_karma
 
 __MODULE__ = "Info"
@@ -45,22 +46,28 @@ async def get_user_info(user):
     username = user.username
     first_name = user.first_name
     mention = user.mention("Link")
-    status = user.status
     dc_id = user.dc_id
     photo_id = user.photo.big_file_id if user.photo else None
     is_gbanned = await is_gbanned_user(user_id)
     is_sudo = user_id in SUDOERS
     karma = await user_global_karma(user_id)
+    spam_probab, n_messages = await get_spam_probability(user_id)
+    isSpammer = True if spam_probab > 70 else False if spam_probab != 0 else "Uncertain"
+    isPotentialSpammer = True if spam_probab > 40 else False if spam_probab != 0 else "Uncertain"
+    spam_probab = str(round(spam_probab)) + " %" if spam_probab != 0 else "Uncertain"
     caption = f"""
 **ID:** `{user_id}`
 **DC:** {dc_id}
 **Name:** {first_name}
 **Username:** {("@" + username) if username else None}
-**Permalink:** {mention}
-**Status:** {status}
+**Mention:** {mention}
 **Sudo:** {is_sudo}
 **Karma:** {karma}
 **Gbanned:** {is_gbanned}
+**ARQ Spam Detection:**
+    **Spammer:** {isSpammer}
+    **Spam Probability:** {spam_probab}
+    __Stats Of Last {n_messages} Messages.__
 """
     return [caption, photo_id]
 
@@ -84,7 +91,7 @@ async def get_chat_info(chat):
 **Type:** {type}
 **Name:** {title}
 **Username:** {("@" + username) if username else None}
-**Permalink:** {link}
+**Mention:** {link}
 **Members:** {members}
 **Scam:** {is_scam}
 **Restricted:** {is_restricted}
@@ -96,24 +103,20 @@ async def get_chat_info(chat):
 @app.on_message(filters.command("info"))
 @capture_err
 async def info_func(_, message: Message):
-    try:
-        if message.reply_to_message:
-            user = message.reply_to_message.from_user.id
-        elif not message.reply_to_message and len(message.command) == 1:
-            user = message.from_user.id
-        elif not message.reply_to_message and len(message.command) != 1:
-            user = message.text.split(None, 1)[1]
-        m = await message.reply_text("Processing")
-        info_caption, photo_id = await get_user_info(user)
-        if not photo_id:
-            return await m.edit(info_caption, disable_web_page_preview=True)
-        photo = await app.download_media(photo_id)
-        await message.reply_photo(photo, caption=info_caption, quote=False)
-        await m.delete()
-        os.remove(photo)
-    except Exception as e:
-        await message.reply_text(str(e))
-        print(e)
+    if message.reply_to_message:
+        user = message.reply_to_message.from_user.id
+    elif not message.reply_to_message and len(message.command) == 1:
+        user = message.from_user.id
+    elif not message.reply_to_message and len(message.command) != 1:
+        user = message.text.split(None, 1)[1]
+    m = await message.reply_text("Processing")
+    info_caption, photo_id = await get_user_info(user)
+    if not photo_id:
+        return await m.edit(info_caption, disable_web_page_preview=True)
+    photo = await app.download_media(photo_id)
+    await message.reply_photo(photo, caption=info_caption, quote=False)
+    await m.delete()
+    os.remove(photo)
 
 
 @app.on_message(filters.command("chat_info"))
@@ -139,3 +142,4 @@ async def chat_info_func(_, message: Message):
     except Exception as e:
         await message.reply_text(e)
         print(e)
+        await m.delete()

+ 6 - 2
wbb/modules/pmpermit.py

@@ -35,9 +35,12 @@ flood = {}
 
 @app2.on_message(
     filters.private
+    & filters.incoming
+    & ~filters.service
     & ~filters.edited
     & ~filters.me
     & ~filters.bot
+    & ~filters.via_bot
     & ~filters.user(SUDOERS)
 )
 @capture_err
@@ -53,7 +56,7 @@ async def pmpermit_func(_, message):
     else:
         flood[str(user_id)] = 1
     if flood[str(user_id)] > 5:
-        await message.reply_text("SPAM DETECTED, USER BLOCKED.")
+        await message.reply_text("SPAM DETECTED, BLOCKED USER AUTOMATICALLY!")
         return await app2.block_user(user_id)
     results = await app2.get_inline_bot_results(BOT_ID, f"pmpermit {user_id}")
     await app2.send_inline_bot_result(
@@ -117,8 +120,9 @@ async def block_user_func(_, message):
             message, text="Reply to a user's message to block."
         )
     user_id = message.reply_to_message.from_user.id
-    await app2.block_user(user_id)
+    # Blocking user after editing the message so that other person can get the update.
     await edit_or_reply(message, text="Successfully blocked the user")
+    await app2.block_user(user_id)
 
 
 @app2.on_message(

+ 68 - 0
wbb/modules/trust.py

@@ -0,0 +1,68 @@
+"""
+MIT License
+
+Copyright (c) 2021 TheHamkerCat
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+"""
+from pyrogram import filters
+from pyrogram.types import Message
+
+from wbb import app, app2, arq, MESSAGE_DUMP_CHAT, USERBOT_BOT_CHAT_DIFFERENCE, BOT_ID
+from wbb.utils.dbfunctions import update_trust_db, get_trust_db
+from wbb.utils.filter_groups import trust_group
+
+@app2.on_message(
+        (filters.text | filters.caption)
+        & ~filters.chat([MESSAGE_DUMP_CHAT, BOT_ID])
+        & filters.chat(USERBOT_BOT_CHAT_DIFFERENCE)
+        & ~filters.me,
+        group=trust_group
+    )
+@app.on_message(
+        (filters.text | filters.caption)
+        & ~filters.chat(MESSAGE_DUMP_CHAT)
+        & ~filters.me
+        & ~filters.private,
+        group=trust_group
+    )
+async def trust_watcher_func(_, message: Message):
+    if message.command:
+        return
+    if not message.from_user:
+        return
+    user_id = message.from_user.id
+    text = message.text if message.text else message.caption
+    text = text.strip()
+    if not text:
+        return
+    if len(text) < 2:
+        return
+    data = (await arq.nlp(text)).result[0]
+    spam = data.spam
+    await update_trust_db(user_id, spam)
+
+
+async def get_spam_probability(user_id) -> list:
+    data = await get_trust_db(user_id)
+    if not data:
+        return [0, 0]
+    mean = lambda x: sum(x) / len(x)
+    spam = [i for i in data]
+    return [mean(spam), len(spam)]

+ 21 - 0
wbb/utils/dbfunctions.py

@@ -45,6 +45,7 @@ pipesdb = db.pipes
 sudoersdb = db.sudoers
 blacklist_chatdb = db.blacklistChat
 restart_stagedb = db.restart_stage
+trustdb = db.trust
 
 """ Notes functions """
 
@@ -727,3 +728,23 @@ async def clean_restart_stage() -> dict:
         return {}
     await restart_stagedb.delete_one({"something": "something"})
     return {"chat_id": data["chat_id"], "message_id": data["message_id"]}
+
+
+""" TRUST DB """
+
+async def get_trust_db(user_id: int) -> list:
+    user = await trustdb.find_one({"user_id": user_id})
+    if not user:
+        return []
+    return user['data']
+    
+
+async def update_trust_db(user_id: int, new_data: float):
+    user = await trustdb.find_one({"user_id": user_id})
+    data = user['data'] if user else []
+    if len(data) >= 2000:
+        data.remove(data[0])
+    data.append(new_data)
+    await trustdb.update_one(
+        {"user_id": user_id}, {"$set": {"data": data}}, upsert=True
+    )

+ 1 - 0
wbb/utils/filter_groups.py

@@ -33,3 +33,4 @@ blacklist_filters_group = 9
 pipes_group = 10
 taglog_group = 11
 chat_watcher_group = 12
+trust_group = 13