"""User(s) AFK database.""" from kaga.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
"""Global bans utils.""" from kaga.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: user = GBAN_USER.find_one({'_id': user_id}) if not user: return None old_reason = user["reason"] GBAN_USER.update_one({'_id': user_id}, {"$set": { 'name': name, 'reason': reason }}, upsert=True) return old_reason
import time import requests from telegram import ParseMode, error from telegram.ext import CommandHandler from kaga import LASTFM_API_KEY, dispatcher from kaga.modules.no_sql import get_collection from kaga.modules.disable import DisableAbleCommandHandler from kaga.modules.helper_funcs.alternate import typing_action LASTFM_USER = get_collection("LAST_FM") @typing_action def set_user(update, context): msg = update.effective_message args = context.args if args: user = update.effective_user.id username = "******".join(args) if LASTFM_USER.find_one({'_id': user}): LASTFM_USER.find_one_and_update({'_id': user}, {"$set": { 'username': username }}) del_msg = msg.reply_text( f"Nama pengguna diperbarui menjadi {username}!") else: LASTFM_USER.insert_one({'_id': user, 'username': username}) del_msg = msg.reply_text(
"""User database utils.""" from kaga import dispatcher from kaga.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": {
"""Channel log database.""" from kaga.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: return LOG_DATA.count_documents({})
import html from typing import Optional from telegram import MAX_MESSAGE_LENGTH, Message, ParseMode, User from telegram.utils.helpers import escape_markdown from kaga import DEV_USERS, dispatcher from kaga.modules.disable import DisableAbleCommandHandler from kaga.modules.no_sql import get_collection from kaga.modules.helper_funcs.alternate import typing_action from kaga.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})["info"] if info: update.effective_message.reply_text(
MessageHandler, ) from telegram.utils.helpers import mention_html from kaga import LOGGER, dispatcher from kaga.modules.helper_funcs.alternate import typing_action from kaga.modules.helper_funcs.chat_status import ( user_admin, user_not_admin, ) from kaga.modules.log_channel import loggable from kaga.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)},
"""Chat blacklist database.""" from kaga.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:
"""Group disabled commands database.""" from kaga.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:
InlineKeyboardMarkup, Message, ParseMode, User, ) from telegram.error import BadRequest from telegram.ext import CommandHandler, Filters from telegram.utils.helpers import escape_markdown from kaga import dispatcher from kaga.modules.no_sql import get_collection from kaga.modules.helper_funcs.alternate import typing_action from kaga.modules.helper_funcs.chat_status import user_admin from kaga.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: