async def warnlimit(event): K = await is_user_admin(event.chat_id, event.from_id) if K is False: await event.reply(get_string("warns", "user_no_admeme", event.chat_id)) return status, chat_id, chat_title = await get_conn_chat( event.from_id, event.chat_id, admin=True, only_in_groups=True) if status is False: await event.reply(chat_id) return arg = event.pattern_match.group(2) old = mongodb.warnlimit.find_one({'chat_id': chat_id}) if not arg: if old: num = old['num'] else: num = 3 await event.reply(get_string("warns", "warn_limit", event.chat_id).format( chat_name=chat_title, num=num)) else: if old: mongodb.warnlimit.delete_one({'_id': old['_id']}) num = int(arg) mongodb.warnlimit.insert_one({ 'chat_id': chat_id, 'num': num }) await event.reply(get_string("warns", "warn_limit_upd", event.chat_id).format(num))
async def resetwarns(event): K = await is_user_admin(event.chat_id, event.from_id) if K is False: await event.reply(get_string("warns", "user_no_admeme", event.chat_id)) return status, chat_id, chat_title = await get_conn_chat( event.from_id, event.chat_id, admin=True, only_in_groups=True) if status is False: await event.reply(chat_id) return user = await get_user(event) user_id = int(user['user_id']) admin = event.from_id admin_str = await user_link(admin) user_str = await user_link(user_id) chack = mongodb.warns.find({'group_id': chat_id, 'user_id': user_id}) if chack: mongodb.warns.delete_many({'group_id': chat_id, 'user_id': user_id}) text = get_string("warns", "purged_warns", event.chat_id) await event.reply(text.format(admin=admin_str, user=user_str)) else: text = get_string("warns", "usr_no_wrn", event.chat_id) await event.reply(text.format(user=user_str))
async def user_warns(event): status, chat_id, chat_title = await get_conn_chat( event.from_id, event.chat_id, admin=True, only_in_groups=True) if status is False: await event.reply(chat_id) return user, reason = await get_user_and_text(event) if not user: return user_id = int(user['user_id']) if user_id in WHITELISTED: await event.reply( "There are no warnings for this user!" ) return warns = mongodb.warns.find({ 'user_id': user_id, 'group_id': chat_id }) user_str = await user_link(user_id) text = get_string("warns", "warn_list_head", event.chat_id).format( user=user_str, chat_name=chat_title) H = 0 for warn in warns: H += 1 rsn = warn['reason'] if rsn == 'None': rsn = "No reason" text += "{}: `{}`\n".format(H, rsn) if H == 0: await event.reply(get_string("warns", "user_hasnt_warned", event.chat_id).format( user=user_str, chat_name=chat_title)) return await event.reply(text)
async def unban_user(event, user_id, chat_id): unbanned_rights = ChatBannedRights( until_date=None, view_messages=False, send_messages=False, send_media=False, send_stickers=False, send_gifs=False, send_games=False, send_inline=False, embed_links=False, ) bot_id = await bot.get_me() bot_id = bot_id.id if user_id == bot_id: await event.reply( get_string("bans", "bot_cant_be_unbanned", event.chat_id)) return False if await is_user_admin(chat_id, user_id) is True: await event.reply(get_string("bans", "user_admin_unban", event.chat_id)) return False try: await event.client(EditBannedRequest(chat_id, user_id, unbanned_rights)) except Exception as err: print(str(err)) return False return True
async def wrapped_1(message, status, chat_id, chat_title, *args, **kwargs): chat_fed = None arg = message.text.split(' ', 2) if not len(arg) < 2: arg = arg[1] # Check if arg is a FedID F = 0 for symbol in arg: if symbol == "-": F += 1 if F == 4: chat_fed = mongodb.fed_list.find_one({'fed_id': arg}) if not chat_fed: await message.reply( get_string("feds", 'fed_id_invalid', message.chat.id)) return if not chat_fed: chat_fed = mongodb.fed_groups.find_one({'chat_id': chat_id}) if not chat_fed: await message.reply( get_string("feds", 'chat_not_in_fed', message.chat.id)) return fed = mongodb.fed_list.find_one({'fed_id': chat_fed['fed_id']}) return await func(message, status, chat_id, chat_title, fed, *args, **kwargs)
async def cleanservice(event): args = event.pattern_match.group(1) chat_id = event.chat_id enable = ['yes', 'on', 'enable'] disable = ['no', 'disable'] bool = args.lower() old = mongodb.clean_service.find_one({'chat_id': chat_id}) if bool: if bool in enable: new = {'chat_id': chat_id, 'service': True} if old: mongodb.clean_service.update_one({'_id': old['_id']}, {"$set": new}, upsert=False) else: mongodb.clean_service.insert_one(new) await event.reply(get_string("greetings", "serv_yes", chat_id)) elif bool in disable: mongodb.clean_service.delete_one({'_id': old['_id']}) await event.reply(get_string("greetings", "serv_no", chat_id)) else: await event.reply(get_string("greetings", "no_args_serv", chat_id)) return else: await event.reply(get_string("greetings", "no_args_serv", chat_id)) return
async def setwelcome(event): if not event.pattern_match.group(1): return status, chat_id, chat_title = await get_conn_chat(event.from_id, event.chat_id, admin=True) chat = event.chat_id chat = mongodb.chat_list.find_one({'chat_id': int(chat)}) if status is False: await event.reply(chat_id) return note_name = event.pattern_match.group(1) note = mongodb.notes.find_one({'chat_id': chat_id, 'name': note_name}) if not note: await event.reply(get_string("greetings", "cant_find_note", chat)) return old = mongodb.welcomes.find_one({'chat_id': chat_id}) if old: mongodb.welcomes.delete_one({'_id': old['_id']}) mongodb.welcomes.insert_one({ 'chat_id': chat_id, 'enabled': True, 'note': note_name }) await event.reply( get_string("greetings", "welcome_set_to_note", chat).format(note_name))
async def kick_self(event, chat, user): K = await is_user_admin(chat, user) if K is True: await event.reply(get_string("bans", "kickme_admin", chat)) return False if str(user) in WHITELISTED: print(str(user)) await event.reply(get_string("bans", "whitelisted", chat)) return False banned_rights = ChatBannedRights(until_date=None, send_messages=True, view_messages=True) unbanned_rights = ChatBannedRights(until_date=None, view_messages=False, send_messages=False) try: await event.client(EditBannedRequest(chat, user, banned_rights)) await event.client(EditBannedRequest(chat, user, unbanned_rights)) except Exception: return False return True
async def get_conn_chat(user_id, chat_id, admin=False, only_in_groups=False): if not user_id == chat_id: chat_title = mongodb.chat_list.find_one({'chat_id': int(chat_id)})['chat_title'] return True, chat_id, chat_title user_chats = mongodb.user_list.find_one({'user_id': user_id})['chats'] group_id = mongodb.connections.find_one({'user_id': int(user_id)}) if not group_id: if only_in_groups is True: return False, get_string("connections", "usage_only_in_groups", chat_id), None return True, user_id, "Local" group_id = group_id['chat_id'] if chat_id not in user_chats: return False, get_string("connections", "not_in_chat", chat_id), None chat_title = mongodb.chat_list.find_one({'chat_id': int(group_id)})['chat_title'] if admin is True: if not await is_user_admin(group_id, user_id): return False, get_string("connections", "u_should_be_admin", event.chat_id).format(chat_title), None return True, int(group_id), chat_title
async def kick_user(event, user_id, chat_id): banned_rights = ChatBannedRights(until_date=None, send_messages=True, view_messages=True) unbanned_rights = ChatBannedRights(until_date=None, view_messages=False, send_messages=False) bot_id = await bot.get_me() bot_id = bot_id.id if user_id == bot_id: await event.reply( get_string("bans", "bot_cant_be_kicked", event.chat_id)) return False if await is_user_admin(chat_id, user_id) is True: await event.reply(get_string("bans", "user_admin_kick", event.chat_id)) return False try: await event.client(EditBannedRequest(chat_id, user_id, banned_rights)) await event.client(EditBannedRequest(chat_id, user_id, unbanned_rights)) except Exception as err: print(str(err)) return False return True
async def demote(event): # Admin right check user = await get_user(event) if user: pass else: return bot_id = (await tbot.get_me()).id if bot_id == user['user_id']: return # New rights after demotion newrights = ChatAdminRights(add_admins=None, invite_users=None, change_info=None, ban_users=None, delete_messages=None, pin_messages=None) # Edit Admin Permission try: await event.client( EditAdminRequest(event.chat_id, user['user_id'], newrights)) # If we catch BadRequestError from Telethon # Assume we don't have permission to demote except BadRequestError: await event.reply(get_string('misc', 'demote_failed', event.chat_id)) return await event.reply(get_string('misc', 'demote_success', event.chat_id))
async def tmute(event, status, chat_id, chat_title): user, data = await get_user_and_text(event) data = data.split(' ', 2) time_val = data[0] unit = time_val[-1] if any(time_val.endswith(unit) for unit in ('m', 'h', 'd')): time_num = time_val[:-1] # type: str if unit == 'm': mutetime = int(time.time() + int(time_num) * 60) unit_str = 'minutes' elif unit == 'h': mutetime = int(time.time() + int(time_num) * 60 * 60) unit_str = 'hours' elif unit == 'd': mutetime = int(time.time() + int(time_num) * 24 * 60 * 60) unit_str = 'days' else: await event.reply( get_string("bans", "time_var_incorrect", event.chat_id)) return if await mute_user(event, user['user_id'], event.chat_id, mutetime) is True: admin_str = await user_link(event.from_id) user_str = await user_link(user['user_id']) await event.reply( get_string("bans", "tmute_sucess", event.chat_id).format(admin=admin_str, user=user_str, time=time_val[:-1], unit=unit_str))
async def connect(event): user_id = event.from_id if not event.chat_id == user_id: return history = mongodb.connections.find_one({'user_id': user_id}) if not history: await event.reply( get_string("connections", "history_empty", event.chat_id)) return buttons = [] chat_title = mongodb.chat_list.find_one({'chat_id': history['btn1']}) buttons += [[ Button.inline("{}".format(chat_title['chat_title']), 'connect_{}'.format(history['btn1'])) ]] if history['btn2']: chat_title = mongodb.chat_list.find_one({'chat_id': history['btn2']}) buttons += [[ Button.inline("{}".format(chat_title['chat_title']), 'connect_{}'.format(history['btn2'])) ]] if history['btn3']: chat_title = mongodb.chat_list.find_one({'chat_id': history['btn3']}) buttons += [[ Button.inline("{}".format(chat_title['chat_title']), 'connect_{}'.format(history['btn3'])) ]] chat_title = mongodb.chat_list.find_one( {'chat_id': int(history['chat_id'])}) text = get_string("connections", "connected_chat", event.chat_id) text += chat_title['chat_title'] text += get_string("connections", "select_chat_to_connect", event.chat_id) await event.reply(text, buttons=buttons)
async def ban_user(message, user_id, chat_id, time_val, no_msg=False): real_chat_id = message.chat.id await update_admin_cache(real_chat_id) if str(user_id) in WHITELISTED: if no_msg is False: await message.reply("This user is whitelisted") return if user_id == BOT_ID: if no_msg is False: await message.reply( get_string("bans", "bot_cant_be_banned", real_chat_id)) return False if await is_user_admin(chat_id, user_id) is True: if no_msg is False: await message.reply( get_string("bans", "user_admin_ban", real_chat_id)) return False banned_rights = ChatBannedRights(until_date=time_val, view_messages=True) try: await tbot(EditBannedRequest(chat_id, user_id, banned_rights)) except ChatAdminRequiredError: raise NotEnoughRights('ban') return True
async def mute_user(message, user_id, chat_id, time_val, no_msg=False): real_chat_id = message.chat.id await update_admin_cache(real_chat_id) if str(user_id) in WHITELISTED: await message.reply("This user is whitelisted") return if user_id == BOT_ID: await message.reply( get_string("bans", "bot_cant_be_muted", real_chat_id)) return False if await is_user_admin(chat_id, user_id) is None: await message.reply(get_string("bans", "user_admin_mute", real_chat_id)) return False try: await bot.restrict_chat_member(chat_id, user_id, permissions=ChatPermissions( can_send_messages=False, until_date=time_val)) except (NotEnoughRightsToRestrict, BadRequest): await message.reply( get_string("bans", "user_cannot_be_muted", real_chat_id)) return False return True
async def wrapped_1(event, *args, **kwargs): if hasattr(event, 'from_id'): user_id = event.from_id elif 'from' in event: user_id = event.from_user.id if user_id == OWNER_ID: return await func(event, *args, **kwargs) if hasattr(event, 'chat_id'): real_chat_id = event.chat_id elif hasattr(event, 'chat'): real_chat_id = event.chat.id status, chat_id, chat_title = await get_conn_chat(user_id, real_chat_id, only_in_groups=True) group_fed = mongodb.fed_groups.find_one({'chat_id': chat_id}) if not group_fed: await event.reply( get_string("feds", 'chat_not_in_fed', real_chat_id)) return False fed = mongodb.fed_list.find_one({'fed_id': group_fed['fed_id']}) if not user_id == fed['creator']: fadmins = mongodb.fed_admins.find({ 'fed_id': fed['fed_id'], 'admin': user_id }) if not fadmins: await event.reply( get_string("feds", 'need_admin_to_fban', real_chat_id).format(name=fed['fed_name'])) return await func(event, *args, **kwargs)
async def wrapped_1(event, *args, **kwargs): user = await get_user(event) # Count of - in group(2) to see if its a fed id F = 0 text = "" for a in event.pattern_match.group(2): if a == "-": F += 1 if F == 4: # group(2) is id chat_fed = mongodb.fed_list.find_one( {'fed_id': event.pattern_match.group(2)}) if not chat_fed: await event.reply( get_string("feds", 'fed_id_invalid', event.chat_id)) return else: chat_fed = mongodb.fed_groups.find_one({'chat_id': event.chat_id}) if not chat_fed: await event.reply( get_string("feds", 'chat_not_in_fed', event.chat_id)) return text += event.pattern_match.group(2) text += " " if event.pattern_match.group(3): text += event.pattern_match.group(3) fed = mongodb.fed_list.find_one({'fed_id': chat_fed['fed_id']}) return await func(event, user, fed, text, *args, **kwargs)
async def join_fed_comm(message, strings, **kwargs): fed_id = message.get_args() chat_id = message.chat.id user = message.from_user.id peep = await tbot(GetParticipantRequest( channel=chat_id, user_id=user, )) if not peep.participant == ChannelParticipantCreator(user_id=user): await message.reply(get_string('feds', 'only_creators', chat_id)) return check = mongodb.fed_list.find_one({'fed_id': fed_id}) if check is False: # Assume Fed ID invalid await message.reply(get_string('feds', 'fed_id_invalid', chat_id)) return old = mongodb.fed_groups.find_one({'chat_id': chat_id}) if old: # Assume chat already joined this/other fed await message.reply(get_string('feds', 'joined_fed_already', chat_id)) return join_data = {'chat_id': chat_id, 'fed_id': fed_id} mongodb.fed_groups.insert_one(join_data) await message.reply( strings['join_fed_success'].format(name=check['fed_name']))
async def get_mod_help_callback(event): chat_id = event.chat_id module = re.search('mod_help_(.*)', str(event.data)).group(1)[:-1] text = get_string(module, "title", chat_id, dir="HELPS") text += '\n' lang = get_chat_lang(chat_id) buttons = [] for string in get_string(module, "text", chat_id, dir="HELPS"): if "HELPS" in LANGUAGES[lang]: text += LANGUAGES[lang]["HELPS"][module]['text'][string] else: text += LANGUAGES["en"]["HELPS"][module]['text'][string] text += '\n' if 'buttons' in LANGUAGES[lang]["HELPS"][module]: counter = 0 for btn in LANGUAGES[lang]["HELPS"][module]['buttons']: counter += 1 btn_name = LANGUAGES[lang]["HELPS"][module]['buttons'][btn] t = [Button.inline(btn_name, btn)] if counter % 2 == 0: new = buttons[-1] + t buttons = buttons[:-1] buttons.append(new) else: buttons.append(t) buttons += [[Button.inline("Back", 'get_help')]] await event.edit(text, buttons=buttons)
async def purge(event): K = await is_user_admin(event.chat_id, event.from_id) if K is False: await event.reply( get_string("msg_deleting", "no_rights_purge", event.chat_id)) return if not event.message.reply_to_msg_id: await event.reply( get_string("msg_deleting", "reply_to_msg", event.chat_id)) return msg = await event.get_reply_message() chat = await event.get_input_chat() msgs = [] msg_id = msg.id delete_to = event.message.id - 1 await event.client.delete_messages(chat, event.message.id) msgs.append(event.reply_to_msg_id) for m_id in range(int(delete_to), msg_id - 1, -1): msgs.append(m_id) if len(msgs) == 100: await event.client.delete_messages(chat, msgs) msgs = [] await event.client.delete_messages(chat, msgs) await event.reply(get_string("msg_deleting", "purge_done", event.chat_id))
async def wrapped_1(message, status, chat_id, chat_title, *args, **kwargs): user, text = await aio_get_user(message) if not user: return chat_fed = None if text: args = text.split(" ", 1) if len(args) >= 1: # Check if args[0] is a FedID F = 0 for symbol in args[0]: if symbol == "-": F += 1 if F == 4: text = args[1] chat_fed = mongodb.fed_list.find_one({'fed_id': args[0]}) if not chat_fed: await message.reply(get_string("feds", 'fed_id_invalid', message.chat.id)) return else: text = "".join(args) if not chat_fed: chat_fed = mongodb.fed_groups.find_one({'chat_id': chat_id}) if not chat_fed: await message.reply(get_string("feds", 'chat_not_in_fed', message.chat.id)) return fed = mongodb.fed_list.find_one({'fed_id': chat_fed['fed_id']}) return await func(message, status, chat_id, chat_title, user, fed, text, *args, **kwargs)
def get_start(event): text = get_string("pm_menu", "start_hi", event.chat_id) buttons = [[Button.inline(get_string("pm_menu", "btn_help", event.chat_id), 'get_help')]] buttons += [[Button.inline(get_string("pm_menu", "btn_lang", event.chat_id), 'set_lang')]] buttons += [[custom.Button.url(get_string("pm_menu", "btn_chat", event.chat_id), 'https://t.me/YanaBotGroup'), custom.Button.url(get_string("pm_menu", "btn_channel", event.chat_id), 'https://t.me/SophieNEWS')]] return text, buttons
async def disconnect(event): user_id = event.from_id old = mongodb.connections.find_one({'user_id': user_id}) if not old: await event.reply(get_string("connections", "u_wasnt_connected", event.chat_id)) return chat_title = await get_conn_chat(user_id, event.chat_id) mongodb.connections.delete_one({'_id': old['_id']}) redis.delete('connection_cache_{}'.format(user_id)) await event.reply(get_string("connections", "disconnected", event.chat_id).format(chat_title))
async def ban(event, status, chat_id, chat_title): user, reason = await get_user_and_text(event) if await ban_user(event, user['user_id'], chat_id, None) is True: admin_str = await user_link(event.from_id) user_str = await user_link(user['user_id']) text = get_string("bans", "user_banned", event.chat_id) text += get_string("bans", "reason", event.chat_id) await event.reply(text.format(user=user_str, admin=admin_str, chat_name=chat_title, reason=reason), link_preview=False)
async def get_note_callback(event): data = str(event.data) event_data = re.search(r'get_note_(.*)_(.*)', data) notename = event_data.group(2)[:-1] group_id = event_data.group(1) user_id = event.original_update.user_id try: await send_note(user_id, group_id, None, notename) await event.answer(get_string("notes", "pmed_note", event.chat_id)) except errors.rpcerrorlist.UserIsBlockedError or errors.rpcerrorlist.PeerIdInvalidError: await event.answer(get_string("notes", "user_blocked", event.chat_id), alert=True)
async def stop_filter(event, status, chat_id, chat_title): handler = event.message.text.split(" ", 2)[1] filter = mongodb.filters.find_one({'chat_id': chat_id, "handler": {'$regex': str(handler)}}) if not filter: await event.reply(get_string("filters", "cant_find_filter", event.chat_id)) return mongodb.filters.delete_one({'_id': filter['_id']}) update_handlers_cache(chat_id) text = str(get_string("filters", "filter_deleted", event.chat_id)) # text = text.format(filter=handler, chat_name=chat_title) #FIXME await event.reply(text)
async def leave_fed_comm(message, strings, status, chat_id, chat_title, **kwargs): user = message.from_user.id peep = await tbot(GetParticipantRequest( channel=chat_id, user_id=user, )) if not peep.participant == ChannelParticipantCreator(user_id=user): await message.reply(get_string('feds', 'only_creators', chat_id)) return if not (fed := await db.feds.find_one({'chats': {'$in': [chat_id]}})): await message.reply(get_string('feds', 'chat_not_in_fed', chat_id)) return
async def remove_warn(event): user_id = event.query.user_id K = await is_user_admin(event.chat_id, user_id) if K is False: await event.answer(get_string("warns", "rmv_warn_admin", event.chat_id)) return warn_id = re.search(r'remove_warn_(.*)', str(event.data)).group(1)[:-1] warn = mongodb.warns.find_one({'warn_id': warn_id}) if warn: mongodb.warns.delete_one({'_id': warn['_id']}) user_str = await user_link(user_id) textx = get_string("warns", "rmv_sfl", event.chat_id) await event.edit(textx.format(admin=user_str), link_preview=False)
async def list_filters(event, status, chat_id, chat_title): filters = mongodb.filters.find({'chat_id': chat_id}) text = get_string("filters", "filters_in", event.chat_id).format(chat_name=chat_title) H = 0 for filter in filters: H += 1 if filter['arg']: text += "- {} ({} - `{}`)\n".format( filter['handler'], filter['action'], filter['arg']) else: text += "- {} ({})\n".format(filter['handler'], filter['action']) if H == 0: text = get_string("filters", "no_filters_in", event.chat_id).format(chat_title) await event.reply(text)
async def leave_fed(event, chat_id, user): peep = await bot(GetParticipantRequest( channel=chat_id, user_id=user, )) if not peep.participant == ChannelParticipantCreator(user_id=user): await event.reply(get_string('feds', 'only_creators', chat_id)) return old = mongodb.fed_groups.delete_one({'chat_id': chat_id}).deleted_count if old < 1: # If chat not was in any federation await event.reply(get_string('feds', 'chat_not_in_fed', chat_id)) return return True