diff --git a/database/blacklist_db.py b/database/blacklist_db.py new file mode 100644 index 00000000..b1e019ca --- /dev/null +++ b/database/blacklist_db.py @@ -0,0 +1,32 @@ +from database import dbname + +blacklist_filtersdb = db["blacklistFilters"] + +async def get_blacklisted_words(chat_id: int) -> List[str]: + _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id}) + if not _filters: + return [] + return _filters["filters"] + +async def save_blacklist_filter(chat_id: int, word: str): + word = word.lower().strip() + _filters = await get_blacklisted_words(chat_id) + _filters.append(word) + await blacklist_filtersdb.update_one( + {"chat_id": chat_id}, + {"$set": {"filters": _filters}}, + upsert=True, + ) + +async def delete_blacklist_filter(chat_id: int, word: str) -> bool: + filtersd = await get_blacklisted_words(chat_id) + word = word.lower().strip() + if word in filtersd: + filtersd.remove(word) + await blacklist_filtersdb.update_one( + {"chat_id": chat_id}, + {"$set": {"filters": filtersd}}, + upsert=True, + ) + return True + return False \ No newline at end of file diff --git a/misskaty/plugins/blacklist_chat.py b/misskaty/plugins/blacklist_chat.py new file mode 100644 index 00000000..2a5cfedd --- /dev/null +++ b/misskaty/plugins/blacklist_chat.py @@ -0,0 +1,121 @@ +""" +MIT License + +Copyright (c) 2023 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. +""" +import re +from time import time + +from pyrogram import filters +from pyrogram.types import ChatPermissions + +from misskaty import app +from misskaty.vars import SUDO +from misskaty.core.decorator.permissions import adminsOnly +from wbb.modules.admin import list_admins +from misskaty.core.decorator.permissions import list_admins +from database.blacklist_db import ( + delete_blacklist_filter, + get_blacklisted_words, + save_blacklist_filter, +) + + +__MODULE__ = "Blacklist" +__HELP__ = """ +/blacklisted - Get All The Blacklisted Words In The Chat. +/blacklist [WORD|SENTENCE] - Blacklist A Word Or A Sentence. +/whitelist [WORD|SENTENCE] - Whitelist A Word Or A Sentence. +""" + + +@app.on_message(filters.command("blacklist") & ~filters.private) +@adminsOnly("can_restrict_members") +async def save_filters(_, message): + if len(message.command) < 2: + return await message.reply_text("Usage:\n/blacklist [WORD|SENTENCE]") + word = message.text.split(None, 1)[1].strip() + if not word: + return await message.reply_text("**Usage**\n__/blacklist [WORD|SENTENCE]__") + chat_id = message.chat.id + await save_blacklist_filter(chat_id, word) + await message.reply_text(f"__**Blacklisted {word}.**__") + + +@app.on_message(filters.command("blacklisted") & ~filters.private) +@capture_err +async def get_filterss(_, message): + data = await get_blacklisted_words(message.chat.id) + if not data: + await message.reply_text("**No blacklisted words in this chat.**") + else: + msg = f"List of blacklisted words in {message.chat.title} :\n" + for word in data: + msg += f"**-** `{word}`\n" + await message.reply_text(msg) + + +@app.on_message(filters.command("whitelist") & ~filters.private) +@adminsOnly("can_restrict_members") +async def del_filter(_, message): + if len(message.command) < 2: + return await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]") + word = message.text.split(None, 1)[1].strip() + if not word: + return await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]") + chat_id = message.chat.id + deleted = await delete_blacklist_filter(chat_id, word) + if deleted: + return await message.reply_text(f"**Whitelisted {word}.**") + await message.reply_text("**No such blacklist filter.**") + + +@app.on_message(filters.text & ~filters.private, group=8) +@capture_err +async def blacklist_filters_re(_, message): + text = message.text.lower().strip() + if not text: + return + chat_id = message.chat.id + user = message.from_user + if not user: + return + if user.id in SUDO: + return + list_of_filters = await get_blacklisted_words(chat_id) + for word in list_of_filters: + pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])" + if re.search(pattern, text, flags=re.IGNORECASE): + if user.id in await list_admins(chat_id): + return + try: + await message.chat.restrict_member( + user.id, + ChatPermissions(), + until_date=int(time() + 3600), + ) + except Exception: + return + return await app.send_message( + chat_id, + f"Muted {user.mention} [`{user.id}`] for 1 hour " + + f"due to a blacklist match on {word}.", + ) \ No newline at end of file diff --git a/misskaty/plugins/misc_tools.py b/misskaty/plugins/misc_tools.py index 7eb84d2b..f866c2fd 100644 --- a/misskaty/plugins/misc_tools.py +++ b/misskaty/plugins/misc_tools.py @@ -61,6 +61,8 @@ async def kbbi_search(_, ctx: Client): if len(ctx.command) == 1: return await ctx.reply_msg("Please add keyword to search definition in kbbi") r = (await http.get(f"https://yasirapi.eu.org/kbbi?kata={ctx.input}")).json() + if nomsg := r.get("detail"): + return await ctx.reply_msg(nomsg) kbbi_btn = InlineKeyboardMarkup( [[InlineKeyboardButton(text="Open in Web", url=r.get('link'))]] ) diff --git a/update.py b/update.py index 6d8022df..a02b9627 100644 --- a/update.py +++ b/update.py @@ -7,6 +7,10 @@ import dotenv import requests from git import Repo +if ospath.exists("MissKatyLogs.txt"): + with open("MissKatyLogs.txt", "r+") as f: + f.truncate(0) + basicConfig( level=INFO, format="[%(asctime)s - %(levelname)s] - %(name)s.%(funcName)s - %(message)s",