async def unmute_usr(c: Alita, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) if len(m.text.split()) == 1 and not m.reply_to_message: await m.reply_text("Whom should I unmute?\nSpecify a user first.") return if from_user.can_restrict_members or from_user.status == "creator": user_id, user_first_name = extract_user(m) try: await m.chat.restrict_member(user_id, unmute_permissions) await m.reply_text(f"<b>Muted</b> {mention_html(user_first_name,user_id)}") except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}") LOGGER.error(ef) return await m.reply_text("You don't have permissions to restrict users.") return
async def kick_usr(c: Alita, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) if len(m.text.split()) == 1 and not m.reply_to_message: await m.reply_text("Whom should I kick?\nSpecify a user first.") return if from_user.can_restrict_members or from_user.status == "creator": user_id, user_first_name = extract_user(m) try: await c.kick_chat_member(m.chat.id, user_id, int(time.time() + 45)) await m.reply_text(f"Banned {mention_html(user_first_name, user_id)}") except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}") LOGGER.error(ef) return
async def view_locks(c: Alita, m: Message): _ = GetLang(m).strs v_perm = "" vmsg = "" vmedia = "" vstickers = "" vanimations = "" vgames = "" vinlinebots = "" vwebprev = "" vpolls = "" vinfo = "" vinvite = "" vpin = "" chkmsg = await m.reply_text(_("locks.check_perm_msg")) v_perm = await c.get_chat(m.chat.id) async def convert_to_emoji(val: bool): if val is True: return "â " return "â" vmsg = await convert_to_emoji(v_perm.permissions.can_send_messages) vmedia = await convert_to_emoji(v_perm.permissions.can_send_media_messages) vstickers = await convert_to_emoji(v_perm.permissions.can_send_stickers) vanimations = await convert_to_emoji(v_perm.permissions.can_send_animations ) vgames = await convert_to_emoji(v_perm.permissions.can_send_games) vinlinebots = await convert_to_emoji(v_perm.permissions.can_use_inline_bots ) vwebprev = await convert_to_emoji( v_perm.permissions.can_add_web_page_previews) vpolls = await convert_to_emoji(v_perm.permissions.can_send_polls) vinfo = await convert_to_emoji(v_perm.permissions.can_change_info) vinvite = await convert_to_emoji(v_perm.permissions.can_invite_users) vpin = await convert_to_emoji(v_perm.permissions.can_pin_messages) if v_perm is not None: try: permission_view_str = _("locks.view_perm").format( vmsg=vmsg, vmedia=vmedia, vstickers=vstickers, vanimations=vanimations, vgames=vgames, vinlinebots=vinlinebots, vwebprev=vwebprev, vpolls=vpolls, vinfo=vinfo, vinvite=vinvite, vpin=vpin, ) await chkmsg.edit_text(permission_view_str) except Exception as e_f: await chkmsg.edit_text(_("general.something_wrong")) await m.reply_text(e_f) return
async def start(c: Alita, m: Message): me = await c.get_users("self") _ = GetLang(m).strs if m.chat.type == "private": if errors.UserIsBlocked: LOGGER.warning(f"Bot blocked by {m.from_user.id}") pass keyboard = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton("đ " + _("start.commands_btn"), callback_data="commands") ] + [ InlineKeyboardButton("âšī¸ " + _("start.infos_btn"), callback_data="infos") ], [ InlineKeyboardButton("đ " + _("start.language_btn"), callback_data="chlang") ] + [ InlineKeyboardButton( "â " + _("start.add_chat_btn"), url=f"https://t.me/{me.username}?startgroup=new", ) ], ]) await m.reply_text(_("start.private"), reply_markup=keyboard, reply_to_message_id=m.message_id) else: await m.reply_text(_("start.group"), reply_to_message_id=m.message_id) return
async def gen_start_kb(m, me): _ = GetLang(m).strs keyboard = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton( f"đ {_('start.commands_btn')}", callback_data="commands", ), ] + [ InlineKeyboardButton( f"âšī¸ {_('start.infos_btn')}", callback_data="infos", ), ], [ InlineKeyboardButton( f"đ {_('start.language_btn')}", callback_data="chlang", ), ] + [ InlineKeyboardButton( f"â {_('start.add_chat_btn')}", url=f"https://t.me/{me.username}?startgroup=new", ), ], [ InlineKeyboardButton( "đī¸ Source Code", url="https://github.com/Divkix/Alita_Robot", ), ], ], ) return keyboard
async def adminlist(c: Alita, m: Message): _ = GetLang(m).strs try: me_id = int(get_key("BOT_ID")) # Get Bot ID from Redis! adminlist = get_key("ADMINDICT")[str( m.chat.id)] # Load ADMINDICT from string adminstr = _("admin.adminlist").format(chat_title=m.chat.title) for i in adminlist: usr = await c.get_users(i) if i == me_id: adminstr += f"- {mention_html(usr.first_name, i)} (Me)\n" else: usr = await c.get_users(i) adminstr += f"- {mention_html(usr.first_name, i)} (`{i}`)\n" await m.reply_text(adminstr) except Exception as ef: if str(ef) == str(m.chat.id): await m.reply_text(_("admin.useadmincache")) else: await m.reply_text( _("admin.somerror").format(SUPPORT_GROUP=SUPPORT_GROUP, ef=ef)) LOGGER.error(ef) return
async def commands_pvt(c: Alita, m: Message): _ = GetLang(m).strs me = await c.get_users("self") if m.chat.type != "private": priv8kb = InlineKeyboardMarkup([ [ InlineKeyboardButton( text="Help", url=f"t.me/{me.username}?start=help", ), ], ], ) await m.reply_text( "Contact me in PM to get the list of possible commands.", reply_markup=priv8kb, reply_to_message_id=m.message_id, ) return keyboard = InlineKeyboardMarkup(inline_keyboard=[ *(await gen_cmds_kb()), (await back_kb(m.message)), ], ) await m.reply_text(_("general.commands_available"), reply_markup=keyboard) return
async def unmute_usr(c: Alita, m: Message): _ = GetLang(m).strs if not (await admin_check(c, m)): return from_user = await m.chat.get_member(m.from_user.id) if from_user.can_restrict_members or from_user.status == "creator": user_id, user_first_name = await extract_user(m) try: await m.chat.restrict_member(user_id, unmute_permissions) await m.reply_text( f"<b>Unmuted</b> {(await mention_html(user_first_name,user_id))}", ) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}" ) LOGGER.error(ef) return await m.reply_text("You don't have permissions to restrict users.") return
async def reload_admins(c: Alita, m: Message): _ = GetLang(m).strs replymsg = await m.reply_text("Refreshing admin list...") if not (await admin_check(c, m)): return ADMINDICT = await get_key("ADMINDICT") # Load ADMINDICT from string try: adminlist = [] async for i in m.chat.iter_members(filter="administrators"): if not i.user.is_deleted: continue adminlist.append(( i.user.id, f"@{i.user.username}" if i.user.username else i.user.first_name, ), ) ADMINDICT[str(m.chat.id)] = adminlist await set_key("ADMINDICT", ADMINDICT) await replymsg.edit_text(_("admin.reloadedadmins")) LOGGER.info(f"Reloaded admins for {m.chat.title}({m.chat.id})") except Exception as ef: await m.reply_text(_("admin.useadmincache")) LOGGER.error(ef) return
async def get_invitelink(c: Alita, m: Message): _ = GetLang(m).strs if not (await admin_check(c, m)): return from_user = await m.chat.get_member(m.from_user.id) # If user does not have permission to invite other users, return if from_user.can_invite_users or from_user.status == "creator": try: link = await c.export_chat_invite_link(m.chat.id) await m.reply_text(_("admin.invitelink").format(link=link)) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except errors.ChatAdminInviteRequired: await m.reply_text(_("admin.noinviteperm")) except errors.RightForbidden: await m.reply_text("I don't have enough rights to view invitelink." ) except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}" ) LOGGER.error(ef) return await m.reply_text(_("admin.nouserinviteperm")) return
async def pin_message(c: Alita, m: Message): _ = GetLang(m).strs if not (await admin_check(c, m)): return pin_args = m.text.split(None, 1) if m.reply_to_message: try: disable_notification = True if len(pin_args) >= 2 and pin_args[1] in ["alert", "notify", "loud"]: disable_notification = False await c.pin_chat_message( m.chat.id, m.reply_to_message.message_id, disable_notification=disable_notification, ) await m.reply_text(_("admin.pinnedmsg")) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except errors.RightForbidden: await m.reply_text("I don't have enough rights to pin messages.") except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}") LOGGER.error(ef) else: await m.reply_text(_("admin.nopinmsg")) return
async def get_rules(c: Alita, m: Message): _ = GetLang(m).strs chat_id = m.chat.id rules = db.get_rules(chat_id) if not rules: await m.reply_text(_("rules.no_rules"), reply_to_message_id=m.message_id) return try: await c.send_message( m.from_user.id, _("rules.get_rules").format(chat=m.chat.title, rules=rules), ) except errors.UserIsBlocked: me = await c.get_me() pm_kb = InlineKeyboardMarkup([[ InlineKeyboardButton("PM", url=f"https://t.me/{me.username}?start") ]]) await m.reply_text(_("rules.pm_me"), reply_to_message_id=m.message_id, reply_markup=pm_kb) return await m.reply_text(_("rules.sent_pm_rules"), reply_to_message_id=m.message_id) return
async def rm_blacklist(c: Alita, m: Message): res = await admin_check(c, m) if not res: return _ = GetLang(m).strs chat_bl = db.get_chat_blacklist(m.chat.id) if not isinstance(chat_bl, bool): pass else: if len(m.text.split()) >= 2: bl_word = m.text.split(None, 1)[1] if bl_word in chat_bl: db.rm_from_blacklist(m.chat.id, bl_word.lower()) await m.reply_text( _("blacklist.rm_blacklist").format(bl_word=bl_word)) return await m.reply_text( _("blacklist.no_bl_found").format(bl_word=bl_word)) else: await m.reply_text( _("general.check_help"), reply_to_message_id=m.message_id, ) return
async def start_back(c: Alita, m: CallbackQuery): me = await c.get_users("self") _ = GetLang(m).strs keyboard = InlineKeyboardMarkup( inline_keyboard=[ [ InlineKeyboardButton( "đ " + _("start.commands_btn"), callback_data="commands" ) ] + [ InlineKeyboardButton( "âšī¸ " + _("start.infos_btn"), callback_data="infos" ) ], [ InlineKeyboardButton( "đ " + _("start.language_btn"), callback_data="chlang" ) ] + [ InlineKeyboardButton( "â " + _("start.add_chat_btn"), url=f"https://t.me/{me.username}?startgroup=new", ) ], ] ) await m.message.edit_text(_("start.private"), reply_markup=keyboard) await m.answer()
async def commands_pvt(c: Alita, m: Message): me = await c.get_users("self") _ = GetLang(m).strs if m.chat.type != "private": priv8kb = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Help", url="t.me/{}?start=help".format(me.username) ) ] ] ) await m.reply_text( "Contact me in PM to get the list of possible commands.", reply_markup=priv8kb, reply_to_message_id=m.message_id, ) return keyboard = InlineKeyboardMarkup( inline_keyboard=[ *gen_cmds_kb(), [ InlineKeyboardButton( "ÂĢ " + _("general.back_btn"), callback_data="start_back" ) ], ] ) await m.reply_text(_("general.commands_available"), reply_markup=keyboard) return
async def set_lang_callback(c: Alita, m: CallbackQuery): _ = GetLang(m).strs if m.message.chat.type == "private": keyboard = InlineKeyboardMarkup( inline_keyboard=[ [ InlineKeyboardButton( "ÂĢ " + _("general.back_btn"), callback_data="start_back" ) ] ] ) else: keyboard = InlineKeyboardMarkup( inline_keyboard=[ [InlineKeyboardButton("â " + _("close_btn"), callback_data="close")] ] ) db.set_lang(m.message.chat.id, m.message.chat.type, m.data.split(".")[1]) await m.message.edit_text( "đ " + _("langs.changed").format(lang_code=m.data.split(".")[1]), reply_markup=keyboard, ) await m.answer() return
async def clear_rules(c: Alita, m: Message): res = await admin_check(c, m) if not res: return _ = GetLang(m).strs rules = db.get_rules(m.chat.id) if not rules: await m.reply_text(_("rules.no_rules"), reply_to_message_id=m.message_id) return await m.reply_text( "Are you sure you want to clear rules?", reply_markup=InlineKeyboardMarkup([ [ InlineKeyboardButton("â ī¸ Confirm", callback_data="clear.rules"), InlineKeyboardButton("â Cancel", callback_data="close"), ], ], ), ) return
async def demote_usr(c: Alita, m: Message): _ = GetLang(m).strs if not (await admin_check(c, m)): return from_user = await m.chat.get_member(m.from_user.id) # If user does not have permission to demote other users, return if from_user.can_promote_members or from_user.status == "creator": user_id, user_first_name = await extract_user(m) try: await m.chat.promote_member( user_id=user_id, can_change_info=False, can_delete_messages=False, can_restrict_members=False, can_invite_users=False, can_pin_messages=False, ) await m.reply_text( _("admin.demoted").format( demoter=(await mention_html(m.from_user.first_name, m.from_user.id)), demoted=(await mention_html(user_first_name, user_id)), chat_title=m.chat.title, ), ) # ----- Add admin to redis cache! ----- ADMINDICT = await get_key("ADMINDICT" ) # Load ADMINDICT from string adminlist = [] async for i in m.chat.iter_members(filter="administrators"): if not i.user.is_deleted: continue adminlist.append([ i.user.id, f"@{i.user.username}" if i.user.username else i.user.first_name, ], ) ADMINDICT[str(m.chat.id)] = adminlist await set_key("ADMINDICT", ADMINDICT) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except errors.RightForbidden: await m.reply_text( "I don't have enough rights to demote this user.") except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}" ) LOGGER.error(ef) return await m.reply_text(_("admin.nodemoteperm")) return
async def public_ip(c: Alita, m: Message): _ = GetLang(m).strs ip = requests.get("https://api.ipify.org").text await c.send_message( MESSAGE_DUMP, f"#IP\n\n**User:** {mention_markdown(m.from_user.first_name, m.from_user.id)}", ) await m.reply_text(_("dev.bot_ip").format(ip=ip)) return
async def public_ip(c: Alita, m: Message): _ = GetLang(m).strs ip = (await AioHttp.get_text("https://api.ipify.org"))[0] await c.send_message( MESSAGE_DUMP, f"#IP\n\n**User:** {(await mention_markdown(m.from_user.first_name, m.from_user.id))}", ) await m.reply_text(_("dev.bot_ip").format(ip=ip), quote=True) return
async def start_back(c: Alita, m: CallbackQuery): me = await c.get_users("self") _ = GetLang(m).strs await m.message.edit_text( _("start.private"), reply_markup=(await gen_start_kb(m, me)), ) await m.answer() return
async def demote_usr(c: Alita, m: Message): _ = GetLang(m).strs res = await admin_check(c, m) if not res: return from_user = await m.chat.get_member(m.from_user.id) if len(m.text.split()) == 1 and not m.reply_to_message: await m.reply_text("Whom should I demote?\nSpecify a user first.") return # If user does not have permission to demote other users, return if from_user.can_promote_members or from_user.status == "creator": user_id, user_first_name = extract_user(m) try: await m.chat.promote_member( user_id=user_id, can_change_info=False, can_delete_messages=False, can_restrict_members=False, can_invite_users=False, can_pin_messages=False, ) await m.reply_text( _("admin.demoted").format( demoter=mention_html(m.from_user.first_name, m.from_user.id), demoted=mention_html(user_first_name, user_id), chat_title=m.chat.title, ) ) # ----- Add admin to redis cache! ----- ADMINDICT = get_key("ADMINDICT") # Load ADMINDICT from string adminlist = [] async for i in m.chat.iter_members(filter="administrators"): adminlist.append(i.user.id) ADMINDICT[str(m.chat.id)] = adminlist set_key("ADMINDICT", ADMINDICT) except errors.ChatAdminRequired: await m.reply_text(_("admin.notadmin")) except errors.RightForbidden: await m.reply_text("I don't have enough rights to demote this user.") except Exception as ef: await m.reply_text(f"<code>{ef}</code>\nReport to @{SUPPORT_GROUP}") LOGGER.error(ef) return await m.reply_text(_("admin.nodemoteperm")) return
async def adminlist_show(_: Alita, m: Message): _ = GetLang(m).strs replymsg = await m.reply_text("Getting admins...") try: me_id = int(await get_key("BOT_ID")) # Get Bot ID from Redis! try: adminlist = (await get_key("ADMINDICT"))[str( m.chat.id)] # Load ADMINDICT from string note = "These are cached values!" except Exception: adminlist = [] async for i in m.chat.iter_members(filter="administrators", ): adminlist.append(( i.user.id, f"@{i.user.username}" if i.user.username else (i.user.first_name or "ItsADeletdAccount"), ), ) adminlist = sorted(adminlist, key=lambda x: x[1]) note = "These are up-to-date values!" ADMINDICT = await get_key("ADMINDICT") ADMINDICT[str(m.chat.id)] = adminlist await set_key("ADMINDICT", ADMINDICT) adminstr = _("admin.adminlist").format(chat_title=m.chat.title) for i in adminlist: try: usr = await m.chat.get_member(i[0]) mention = (i[1] if i[1].startswith("@") else (await mention_html(i[1], i[0]))) if i[0] == me_id: adminstr += f"- @{(await get_key('BOT_USERNAME'))} (â)\n" elif usr.user.is_bot: adminstr += f"- {mention} (đ¤)\n" elif usr.status == "owner": adminstr += f"- {mention} (đ)\n" else: adminstr += f"- {mention}\n" except errors.PeerIdInvalid: pass await replymsg.edit_text(f"{adminstr}\n\n<i>Note: {note}</i>") except Exception as ef: if str(ef) == str(m.chat.id): await m.reply_text(_("admin.useadmincache")) else: ef = str(ef) + f"{adminlist}\n" await m.reply_text( _("admin.somerror").format(SUPPORT_GROUP=SUPPORT_GROUP, ef=ef), ) LOGGER.error(ef) return
async def evaluate_code(c: Alita, m: Message): _ = GetLang(m).strs if len(m.text.split()) == 1: await m.reply_text(_("dev.execute_cmd_err")) return sm = await m.reply_text("`Processing...`") cmd = m.text.split(" ", maxsplit=1)[1] reply_to_id = m.message_id if m.reply_to_message: reply_to_id = m.reply_to_message.message_id old_stderr = sys.stderr old_stdout = sys.stdout redirected_output = sys.stdout = io.StringIO() redirected_error = sys.stderr = io.StringIO() stdout, stderr, exc = None, None, None try: await aexec(cmd, c, m) except Exception: exc = traceback.format_exc() stdout = redirected_output.getvalue() stderr = redirected_error.getvalue() sys.stdout = old_stdout sys.stderr = old_stderr evaluation = "" if exc: evaluation = exc elif stderr: evaluation = stderr elif stdout: evaluation = stdout else: evaluation = "Success" final_output = f"<b>EVAL</b>: <code>{cmd}</code>\n\n<b>OUTPUT</b>:\n<code>{evaluation.strip()}</code> \n" if len(final_output) > 4000: with open("eval.text", "w+", encoding="utf8") as out_file: out_file.write(str(final_output)) await m.reply_document( document="eval.text", caption=cmd, disable_notification=True, reply_to_message_id=reply_to_id, ) os.remove("eval.text") await sm.delete() else: await sm.edit(final_output) return
async def back_kb(m): _ = GetLang(m).strs keyboard = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton( f"ÂĢ {_('general.back_btn')}", callback_data="commands", ), ], ], ) return keyboard
async def send_log(c: Alita, m: Message): _ = GetLang(m).strs rply = await m.reply_text("Sending logs...!") await c.send_message( m.chat.id, f"#LOGS\n\n**User:** {(await mention_markdown(m.from_user.first_name, m.from_user.id))}", ) # Send logs await m.reply_document(document=LOGFILE, quote=True) await rply.delete() return
async def get_module_info(c: Alita, m: CallbackQuery): _ = GetLang(m).strs module = m.data.split(".")[1] keyboard = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton("ÂĢ " + _("general.back_btn"), callback_data="commands") ]]) await m.message.edit_text(HELP_COMMANDS[module], parse_mode="markdown", reply_markup=keyboard) await m.answer() return
async def chlang_callback(c: Alita, m: CallbackQuery): _ = GetLang(m).strs keyboard = InlineKeyboardMarkup(inline_keyboard=[ *gen_langs_kb(), [ InlineKeyboardButton("ÂĢ " + _("general.back_btn"), callback_data="start_back") ], ]) await m.message.edit_text(_("lang.changelang"), reply_markup=keyboard) await m.answer() return
async def set_lang(c: Alita, m: Message): if not (await admin_check(c, m)): return _ = GetLang(m).strs keyboard = InlineKeyboardMarkup(inline_keyboard=[*(await gen_langs_kb())]) if len(m.text.split()) >= 2: await m.reply_text(_("langs.correct_usage")) return await m.reply_text(_("lang.changelang"), reply_markup=keyboard) return
async def commands_menu(c: Alita, m: CallbackQuery): _ = GetLang(m).strs keyboard = InlineKeyboardMarkup(inline_keyboard=[ *gen_cmds_kb(), [ InlineKeyboardButton("ÂĢ " + _("general.back_btn"), callback_data="start_back") ], ]) await m.message.edit_text(_("general.commands_available"), reply_markup=keyboard) await m.answer() return