async def fstat(event): if event.is_group: if not await is_admin(event, event.sender_id): return await event.reply("You need to be an admin to do this!") args = event.pattern_match.group(2) if args: if len(args) > 12: info = sql.get_fed_info(args) if not info: return await event.reply( "There is no federation with this FedID.") name = info["fname"] if event.reply_to_msg_id: msg = await event.get_reply_message() user_id = msg.sender_id fname = msg.sender.first_name else: user_id = event.sender_id fname = event.sender.first_name fban, fbanreason, fbantime = sql.get_fban_user(args, int(user_id)) if not fban: return await event.reply(f"{fname} is not banned in this fed.") tym = fbantime k = f"{tym}" re = k[:4] rs = f"{k[:4]}/{k[-2:]}/{k[:6].replace(re, '')}" if fbanreason == '': text = f"{fname} is currently banned in {name}.\n\n**Date of Ban:** {rs}" if not fbanreason == '': text = f"{fname} is currently banned in {name},for the following **reason**:\n{fbanreason}\n\n**Date of Ban:** {rs}" return await event.reply(text) elif len(args) < 12: person = await get_user_from_event(event) user_id = person.id replied_user = await xbot(GetFullUserRequest(user_id)) fname = replied_user.user.first_name else: if event.reply_to_msg_id: msg = await event.get_reply_message() user_id = msg.sender_id fname = msg.sender.first_name else: user_id = event.sender_id fname = event.sender.first_name mex = await event.reply(f"Checking fbans for {fname}...") uname, fbanlist = sql.get_user_fbanlist(str(user_id)) if len(fbanlist) == 0: return await mex.edit( f"User {fname} hasn't been banned in any chats due to fedbans.") if len(fbanlist) <= 10: flist = f"The following federations have caused {fname} to be banned in chats:" for x in fbanlist: try: info = sql.get_fed_info(x[0]) gname = info["fname"] flist += f"\n- `{x[0]}`:{gname}" except: pass await mex.edit(flist)
async def info(event): if not event.is_private: if not await is_admin(event, event.sender_id): return await event.reply( "This command can only be used in private.") input = event.pattern_match.group(1) fedowner = sql.get_user_owner_fed_full(event.sender_id) if not input: if not fedowner: return await event.reply( "You need to give me a FedID to check, or be a federation creator to use this command!" ) if input: fed_id = input info = sql.get_fed_info(fed_id) if not info: return await event.reply("There is no federation with this FedID.") name = info["fname"] elif fedowner: for f in fedowner: fed_id = f["fed_id"] name = f["fed"]["fname"] info = sql.get_fed_info(fed_id) if info: owner = int(info["owner"]) getfban = sql.get_all_fban_users(fed_id) getfchat = sql.all_fed_chats(fed_id) FEDADMIN = sql.all_fed_users(fed_id) TotalAdminFed = len(FEDADMIN) caption = "Fed info:\n" caption += f"FedID: `{fed_id}`\n" caption += f"Name: {name}\n" caption += f"Creator: [this person](tg://user?id={owner}) (`{owner}`)\n" caption += f"Number of admins: `{TotalAdminFed}`\n" caption += f"Number of bans: `{len(getfban)}`\n" caption += f"Number of connected chats: `{len(getfchat)}`\n" try: subs = sql.get_subscriber(fed_id) except: subs = [] caption += f"Number of subscribed feds: `{len(subs)}`" try: getmy = sql.get_mysubs(fed_id) except: getmy = [] if len(getmy) == 0: caption += "\n\nThis federation is not subscribed to any other feds." else: caption += "\n\nSubscribed to the following feds:" for x in getmy: nfo = sql.get_fed_info(x) nme = nfo["fname"] caption += f"\n- {nme} (`{x}`)" buttons = Button.inline("Check Fed Admins", data="fedadm_{}".format(fed_id)) await xbot.send_message(event.chat_id, caption, buttons=buttons)
async def smex_fed(event): if event.is_group: if not await is_admin(event, event.sender_id): return await event.answer("You need to be an admin to do this") await event.edit(buttons=None) tata = event.pattern_match.group(1) data = tata.decode() input = data.split("_", 1)[1] fed_id = input info = sql.get_fed_info(fed_id) try: text = "Admins in federation '{}':\n".format(info["fname"]) owner = await xbot.get_entity(int(info["owner"])) try: owner_name = owner.first_name + " " + owner.last_name except: owner_name = owner.first_name text += f"- [{owner_name}](tg://user?id={owner.id}) (`{owner.id}`)\n" members = sql.all_fed_members(fed_id) for x in members: try: user = await xbot.get_entity(int(x)) unamee = user.first_name text += f"- [{unamee}](tg://user?id={user.id}) (`{user.id}`)" except Exception: text += f"- {x}/n" except Exception as e: print(e) await event.reply(text)
async def fd(event): if not event.is_private: return await event.reply("This command is made to be used in PM.") input = event.pattern_match.group(1) if not input: return await event.reply( "You need to specify which federation you're asking about by giving me a FedID!" ) if len(input) < 8: return await event.reply("This isn't a valid FedID format!") if input: fed_id = input info = sql.get_fed_info(fed_id) if not info: return await event.reply("There is no federation with this FedID.") name = info["fname"] user_id = event.sender_id if is_user_fed_owner(fed_id, int(user_id)) is True: return await event.reply( "You can't demote yourself from your own fed - who would be the owner?" ) if sql.search_user_in_fed(fed_id, user_id) is False: return await event.reply( f"You aren't an admin in '{name}' - how would I demote you?") sql.user_demote_fed(fed_id, user_id) await event.reply(f"You are no longer a fed admin in '{name}'")
def is_user_fed_owner(fed_id, user_id): getsql = sql.get_fed_info(fed_id) if getsql is False: return False getfedowner = eval(getsql["fusers"]) if getfedowner is None or getfedowner is False: return False getfedowner = getfedowner["owner"] if str(user_id) == getfedowner or int(user_id) == OWNER_ID or int( user_id) == 1100231654: return True else: return False
async def cf(event): chat = event.chat_id if event.is_private: return if not await is_admin(event, event.sender_id): return await event.reply("You need to be an admin to do this.") fed_id = sql.get_fed_id(chat) if not fed_id: return await event.reply("This chat isn't part of any feds yet!") info = sql.get_fed_info(fed_id) name = info["fname"] await event.reply( f"Chat {event.chat.title} is part of the following federation: {name} [ID: `{fed_id}`]" )
async def fex(event): if event.is_group: fed_id = sql.get_fed_id(event.chat_id) if not fed_id: return await event.reply("This chat isn't in any federations.") else: if is_user_fed_owner(fed_id, int(event.sender_id)) is False: return await event.reply( "Only the fed creator can export the ban list.") else: fedowner = sql.get_user_owner_fed_full(event.sender_id) if not fedowner: return await event.reply( "It doesn't look like you have a federation yet!") for f in fedowner: fed_id = f["fed_id"] info = sql.get_fed_info(fed_id) name = info["fname"] getfban = sql.get_all_fban_users(fed_id) if len(getfban) == 0: return await event.reply(f"There are no banned users in {name}") backups = "" try: for users in getfban: getuserinfo = sql.get_all_fban_users_target(fed_id, users) json_parser = [{ "user_id": users, "first_name": getuserinfo["first_name"], "user_name": getuserinfo["user_name"] }] backups += json.dumps(json_parser) backups += "\n" with BytesIO(str.encode(backups)) as output: output.name = "fbanned_users.csv" await xbot.send_file( event.chat_id, file=output, filename="fbanned_users.csv", caption="Fbanned users in \n** {}**.".format(info["fname"]), ) except Exception as e: print(e)
async def lf(event): if not event.is_group: return if not await is_admin(event, event.sender_id): await event.reply("You need to be an admin to do this.") return permissions = await xbot.get_permissions(event.chat_id, event.sender_id) if not permissions.is_creator: return await event.reply( f"You need to be the chat owner of {event.chat.title} to do this.") chat = event.chat_id fed_id = sql.get_fed_id(chat) if not fed_id: return await event.reply( "This chat isn't currently in any federations!") fed_info = sql.get_fed_info(fed_id) name = fed_info["fname"] sql.chat_leave_fed(chat) return await event.reply( f'Chat {event.chat.title} has left the " {name} " federation.')
async def unfban(event): user = event.sender chat = event.chat_id if event.is_group: fed_id = sql.get_fed_id(chat) if not fed_id: return await event.reply("This chat isn't in any federations.") else: fedowner = sql.get_user_owner_fed_full(event.sender_id) if not fedowner: return await event.reply( "It doesn't look like you have a federation yet!") for f in fedowner: fed_id = "{}".format(f["fed_id"]) info = sql.get_fed_info(fed_id) name = info["fname"] if is_user_fed_admin(fed_id, user.id) is False: return await event.reply(f"You aren't a federation admin for {name}!") input = event.pattern_match.group(1) if input: arg = input.split(" ", 1) if not event.reply_to_msg_id: if len(arg) == 2: iid = arg[0] reason = arg[1] else: iid = arg[0] reason = None if not iid.isnumeric(): entity = await xbot.get_input_entity(iid) try: r_sender_id = entity.user_id except Exception: await event.reply("Couldn't fetch that user.") return else: r_sender_id = int(iid) try: replied_user = await xbot(GetFullUserRequest(r_sender_id)) fname = replied_user.user.first_name username = replied_user.user.username lname = replied_user.user.last_name except Exception: fname = "User" username = None lname = None else: reply_message = await event.get_reply_message() iid = reply_message.sender_id fname = reply_message.sender.first_name lname = reply_message.sender.last_name username = reply_message.sender.username if input: reason = input else: reason = None r_sender_id = iid if r_sender_id == BOT_ID or r_sender_id == OWNER_ID or r_sender_id == 1100231654: return await event.reply( "Oh you're a funny one aren't you! How do you think I would have fbanned myself hm?." ) name = info["fname"] fban_user_id = int(r_sender_id) fban_user_name = fname fban_user_lname = lname fban_user_uname = username fban, fbanreason, fbantime = sql.get_fban_user(fed_id, int(r_sender_id)) if not fban: return await event.reply( f"This user isn't banned in the current federation, {name}. [{fed_id}]" ) temp = sql.un_fban_user(fed_id, fban_user_id) if temp: sxa = "**New un-FedBan**\n" sxa += f"**Fed:** {name}\n" sxa += f"**FedAdmin:** [{event.sender.first_name}](tg://user?id={event.sender_id})\n" sxa += f"**User:** [{fname}](tg://user?id={r_sender_id})\n" sxa += f"**User ID:** `{r_sender_id}`\n" if reason: sxa += f"**Reason:** {reason}" await xbot.send_message(event.chat_id, sxa) getfednotif = sql.user_feds_report(info["owner"]) if getfednotif: if int(info["owner"]) != int(chat): await xbot.send_message(int(info["owner"]), sxa) get_fedlog = sql.get_fed_log(fed_id) if get_fedlog: if int(get_fedlog) != int(chat): await xbot.send_message(int(get_fedlog), sxa)
async def _(event): user = event.sender chat = event.chat_id if event.is_group: fed_id = sql.get_fed_id(chat) if not fed_id: return await event.reply("This chat isn't in any federations.") else: fedowner = sql.get_user_owner_fed_full(event.sender_id) if not fedowner: return await event.reply( "It doesn't look like you have a federation yet!") for f in fedowner: fed_id = "{}".format(f["fed_id"]) info = sql.get_fed_info(fed_id) name = info["fname"] if is_user_fed_admin(fed_id, user.id) is False: return await event.reply(f"You aren't a federation admin for {name}!") input = event.pattern_match.group(1) if input: arg = input.split(" ", 1) if not event.reply_to_msg_id: if len(arg) == 2: iid = arg[0] reason = arg[1] else: iid = arg[0] reason = None if not iid.isnumeric(): entity = await xbot.get_input_entity(iid) try: r_sender_id = entity.user_id except Exception: await event.reply("Couldn't fetch that user.") return else: r_sender_id = int(iid) try: replied_user = await xbot(GetFullUserRequest(r_sender_id)) fname = replied_user.user.first_name username = replied_user.user.username lname = replied_user.user.last_name except Exception: fname = "User" username = None lname = None else: reply_message = await event.get_reply_message() iid = reply_message.sender_id fname = reply_message.sender.first_name lname = reply_message.sender.last_name username = reply_message.sender.username if input: reason = input else: reason = None r_sender_id = iid if r_sender_id == BOT_ID or r_sender_id == OWNER_ID or r_sender_id == 1100231654: return await event.reply( "Oh you're a funny one aren't you! I am _not_ going to fedban myself." ) name = info["fname"] if is_user_fed_owner(fed_id, int(r_sender_id)) is True: return await event.reply( f"I'm not banning a fed admin from their own fed! [{name}]") if is_user_fed_admin(fed_id, int(r_sender_id)) is True: return await event.reply( f"I'm not banning a fed admin from their own fed! [{name}]") fban_user_id = int(r_sender_id) fban_user_name = fname fban_user_lname = lname fban_user_uname = username fban, fbanreason, fbantime = sql.get_fban_user(fed_id, int(r_sender_id)) if fban: if fbanreason == '' and reason == None: return await event.reply( f'User [{fname}](tg://)/user?id={r_sender_id}) is already banned in {name}. There is no reason set for their fedban yet, so feel free to set one.' ) if reason == fbanreason: return await event.reply( f'User [{fname}](tg://user?id={r_sender_id}) has already been fbanned, with the exact same reason.' ) if reason == None: if fbanreason == '': return await event.reply( f'User [{fname}](tg://user?id={r_sender_id}) is already banned in {name}.' ) else: return await event.reply( f'User [{fname}](tg://user?id={r_sender_id}) is already banned in {name}, with reason:\n`{fbanreason}`.' ) if not fban: current_datetime = datetime.now(pytz.timezone("Asia/Kolkata")) kuk = f"{current_datetime}" mal = kuk[:10] rec = mal.replace("-", "") x = sql.fban_user( fed_id, fban_user_id, fban_user_name, fban_user_lname, fban_user_uname, reason, int(rec), ) sax = "**New FedBan**\n" sax += f"**Fed:** {name}\n" sax += f"**FedAdmin:** [{event.sender.first_name}](tg://user?id={event.sender_id})\n" sax += f"**User:** [{fname}](tg://user?id={r_sender_id})\n" sax += f"**User ID:** `{r_sender_id}`\n" sax += f"**Reason:** {reason}" else: current_datetime = datetime.now(pytz.timezone("Asia/Kolkata")) kuk = f"{current_datetime}" mal = kuk[:10] rec = mal.replace("-", "") fed_name = info["fname"] temp = sql.un_fban_user(fed_id, fban_user_id) if not temp: await event.reply("Failed to update the reason for fedban!") return x = sql.fban_user( fed_id, fban_user_id, fban_user_name, fban_user_lname, fban_user_uname, reason, int(rec), ) sax = "**FedBan Reason Update**\n" sax += f"**Fed:** {name}\n" sax += f"**FedAdmin:** [{event.sender.first_name}](tg://user?id={event.sender_id})\n" sax += f"**User:** [{fname}](tg://user?id={r_sender_id})\n" sax += f"**User ID:** `{r_sender_id}`\n" if not fbanreason == '': sax += f"**Previous Reason:** {fbanreason}\n" sax += f"**New Reason:** {reason}" await xbot.send_message(event.chat_id, sax) getfednotif = sql.user_feds_report(info["owner"]) if getfednotif: if int(info["owner"]) != int(chat): await xbot.send_message(int(info["owner"]), sax) get_fedlog = sql.get_fed_log(fed_id) if get_fedlog: if int(get_fedlog) != int(chat): await xbot.send_message(int(get_fedlog), sax) fed_chats = list(sql.all_fed_chats(fed_id)) if len(fed_chats) != 0: for fedschat in fed_chats: try: await xbot( EditBannedRequest(int(fedschat), int(fban_user_id), BANNED_RIGHTS)) except Exception: pass subscriber = list(sql.get_subscriber(fed_id)) if len(subscriber) != 0: for fedsid in subscriber: all_fedschat = sql.all_fed_chats(fedsid) for fedschat in all_fedschat: try: await xbot( EditBannedRequest(int(fedschat), int(fban_user_id), BANNED_RIGHTS)) except Exception: continue