Example #1
0
"""Chat blacklist database."""

from menhera.modules.no_sql import get_collection

BL = get_collection("BLACKLIST")
BL_SETTING = get_collection("BLACKLIST_SETTINGS")

CHAT_BLACKLISTS = {}
CHAT_SETTINGS_BLACKLISTS = {}


def add_to_blacklist(chat_id, trigger):
    BL.find_one_and_update({
        'chat_id': chat_id,
        'trigger': trigger
    }, {"$set": {
        'chat_id': chat_id,
        'trigger': trigger
    }},
                           upsert=True)
    global CHAT_BLACKLISTS
    if CHAT_BLACKLISTS.get(str(chat_id), set()) == set():
        CHAT_BLACKLISTS[str(chat_id)] = {trigger}
    else:
        CHAT_BLACKLISTS.get(str(chat_id), set()).add(trigger)


def rm_from_blacklist(chat_id, trigger) -> bool:
    data = BL.find_one_and_delete({'chat_id': chat_id, 'trigger': trigger})
    if data:
        if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()):
Example #2
0
"""User(s) AFK database."""

from menhera.modules.no_sql import get_collection

AFK_USERS = get_collection("AFK_USERS")
AFK_LIST = set()


def is_afk(user_id) -> bool:
    return user_id in AFK_LIST


def check_afk_status(user_id) -> dict:
    data = AFK_USERS.find_one({'_id': user_id})
    return data


def set_afk(user_id, reason: str = "") -> None:
    AFK_USERS.update_one({'_id': user_id}, {"$set": {
        'reason': reason
    }},
                         upsert=True)
    __load_afk_users()


def rm_afk(user_id) -> bool:
    data = AFK_USERS.find_one_and_delete({'_id': user_id})
    if data:
        AFK_LIST.remove(user_id)
        return True
    return False
Example #3
0
import html
from typing import Optional

from telegram import MAX_MESSAGE_LENGTH, Message, ParseMode, User
from telegram.utils.helpers import escape_markdown

from menhera import DEV_USERS, dispatcher
from menhera.modules.disable import DisableAbleCommandHandler
from menhera.modules.no_sql import get_collection
from menhera.modules.helper_funcs.alternate import typing_action
from menhera.modules.helper_funcs.extraction import extract_user

USER_INFO = get_collection("USER_INFO")
USER_BIO = get_collection("USER_BIO")


@typing_action
def about_me(update, context):
    message = update.effective_message  # type: Optional[Message]
    args = context.args
    user_id = extract_user(message, args)

    if user_id:
        user = context.bot.get_chat(user_id)
    else:
        user = message.from_user

    info = USER_INFO.find_one({'_id': user.id})

    if info:
        update.effective_message.reply_text(
Example #4
0
    InlineKeyboardMarkup,
    Message,
    ParseMode,
    User,
)
from telegram.error import BadRequest
from telegram.ext import CommandHandler, Filters
from telegram.utils.helpers import escape_markdown

from menhera import dispatcher
from menhera.modules.no_sql import get_collection
from menhera.modules.helper_funcs.alternate import typing_action
from menhera.modules.helper_funcs.chat_status import user_admin
from menhera.modules.helper_funcs.string_handling import markdown_parser

RULES_DATA = get_collection("RULES")


@typing_action
def get_rules(update, context):
    chat_id = update.effective_chat.id
    send_rules(update, chat_id)


# Do not async - not from a handler
def send_rules(update, chat_id, from_pm=False):
    bot = dispatcher.bot
    user = update.effective_user  # type: Optional[User]
    try:
        chat = bot.get_chat(chat_id)
    except BadRequest as excp:
Example #5
0
"""User database utils."""

from menhera import dispatcher
from menhera.modules.no_sql import get_collection


USERS_DB = get_collection("USERS")
CHATS_DB = get_collection("CHATS")
CHAT_MEMBERS_DB = get_collection("CHAT_MEMBERS")


def ensure_bot_in_db():
    USERS_DB.update_one(
        {'_id': dispatcher.bot.id},
        {"$set": {'username': dispatcher.bot.username}},
        upsert=True,
    )


def update_user(user_id, username, chat_id=None, chat_name=None):
    USERS_DB.update_one(
        {'_id': user_id},
        {"$set": {'username': username}},
        upsert=True)
    
    if not (chat_id or chat_name):
        return
    
    CHATS_DB.update_one(
        {'chat_id': chat_id},
        {"$set": {'chat_name': chat_name}},
Example #6
0
"""Global bans utils."""

from menhera.modules.no_sql import get_collection

GBAN_USER = get_collection("GBANS")
GBAN_SETTINGS = get_collection("GBAN_SETTINGS")
GBANNED_LIST = set()
GBANSTAT_LIST = set()


def gban_user(user_id, name, reason=None) -> None:
    GBAN_USER.insert_one({
        '_id': user_id,
        'name': name,
        'reason': reason,
    })
    __load_gbanned_userid_list()


def update_gban_reason(user_id, name, reason) -> str:
    data = GBAN_USER.find_one_and_update(
        {'_id': user_id}, {"$set": {
            'name': name,
            'reason': reason
        }},
        upsert=False)
    return data["reason"]


def ungban_user(user_id) -> None:
    GBAN_USER.delete_one({'_id': user_id})
Example #7
0
"""Channel log database."""

from menhera.modules.no_sql import get_collection


LOG_DATA = get_collection("LOG_CHANNELS")

CHANNELS = {}


def set_chat_log_channel(chat_id, log_channel):
    LOG_DATA.update_one(
        {'chat_id': chat_id},
        {"$set": {'log_channel': log_channel}},
        upsert=True
    )
    CHANNELS[str(chat_id)] = log_channel


def get_chat_log_channel(chat_id) -> int:
    return CHANNELS.get(str(chat_id))


def stop_chat_logging(chat_id) -> int:
    res = LOG_DATA.find_one_and_delete({'chat_id': chat_id})
    if str(chat_id) in CHANNELS:
        del CHANNELS[str(chat_id)]
    return res["log_channel"]


def num_logchannels() -> int:
Example #8
0
"""Group disabled commands database."""

from menhera.modules.no_sql import get_collection

DISABLED_COMMANDS = get_collection("DISABLED_COMMANDS")

DISABLED = {}


def disable_command(chat_id, disable) -> bool:
    data = DISABLED_COMMANDS.find_one({'chat_id': chat_id, 'command': disable})
    if not data:
        DISABLED.setdefault(str(chat_id), set()).add(disable)

        DISABLED_COMMANDS.insert_one({'chat_id': chat_id, 'command': disable})
        return True
    return False


def enable_command(chat_id, enable) -> bool:
    data = DISABLED_COMMANDS.find_one({'chat_id': chat_id, 'command': enable})
    if data:
        if enable in DISABLED.get(str(chat_id)):  # sanity check
            DISABLED.setdefault(str(chat_id), set()).remove(enable)

        DISABLED_COMMANDS.delete_one({'chat_id': chat_id, 'command': enable})
        return True
    return False


def is_command_disabled(chat_id, cmd) -> bool:
Example #9
0
    MessageHandler,
)
from telegram.utils.helpers import mention_html

from menhera import LOGGER, dispatcher
from menhera.modules.helper_funcs.alternate import typing_action
from menhera.modules.helper_funcs.chat_status import (
    user_admin,
    user_not_admin,
)
from menhera.modules.log_channel import loggable
from menhera.modules.no_sql import get_collection

REPORT_GROUP = 5

USER_REPORT_SETTINGS = get_collection("USER_REPORT_SETTINGS")
CHAT_REPORT_SETTINGS = get_collection("CHAT_REPORT_SETTINGS")


@user_admin
@typing_action
def report_setting(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    args = context.args

    if chat.type == chat.PRIVATE:
        if len(args) >= 1:
            if args[0] in ("yes", "on", "true"):
                USER_REPORT_SETTINGS.update_one(
                    {'user_id': int(chat.id)},