async def migrate_chat(m: Message, new_chat: int) -> None: LOGGER.info(f"Migrating from {m.chat.id} to {new_chat}...") langdb = Langs(m.chat.id) notedb = Notes() gdb = Greetings(m.chat.id) ruledb = Rules(m.chat.id) userdb = Users(m.chat.id) chatdb = Chats(m.chat.id) bldb = Blacklist(m.chat.id) approvedb = Approve(m.chat.id) reportdb = Reporting(m.chat.id) notes_settings = NotesSettings() pins_db = Pins(m.chat.id) fldb = Filters() disabl = Disabling(m.chat.id) disabl.migrate_chat(new_chat) gdb.migrate_chat(new_chat) chatdb.migrate_chat(new_chat) userdb.migrate_chat(new_chat) langdb.migrate_chat(new_chat) ruledb.migrate_chat(new_chat) bldb.migrate_chat(new_chat) notedb.migrate_chat(m.chat.id, new_chat) approvedb.migrate_chat(new_chat) reportdb.migrate_chat(new_chat) notes_settings.migrate_chat(m.chat.id, new_chat) pins_db.migrate_chat(new_chat) fldb.migrate_chat(m.chat.id, new_chat) LOGGER.info(f"Successfully migrated from {m.chat.id} to {new_chat}!")
def add_chat(self, chat_id: int): with INSERTION_LOCK: global BLACKLIST_CHATS try: Chats.remove_chat(chat_id) # Delete chat from database except KeyError: pass BLACKLIST_CHATS.append(chat_id) BLACKLIST_CHATS.sort() return self.insert_one({"_id": chat_id, "blacklist": True})
async def chat_broadcast(c: Alita, m: Message): if m.reply_to_message: msg = m.reply_to_message.text.markdown else: await m.reply_text("Reply to a message to broadcast it") return exmsg = await m.reply_text("Started broadcasting!") all_chats = (Chats.list_chats_by_id()) or {} err_str, done_broadcast = "", 0 for chat in all_chats: try: await c.send_message(chat, msg, disable_web_page_preview=True) done_broadcast += 1 await sleep(0.1) except RPCError as ef: LOGGER.error(ef) err_str += str(ef) continue await exmsg.edit_text( f"Done broadcasting ✅\nSent message to {done_broadcast} chats", ) if err_str: with BytesIO(str.encode(await remove_markdown_and_html(err_str))) as f: f.name = "error_broadcast.txt" await m.reply_document( document=f, caption="Broadcast Error", ) return
async def chats(c: Alita, m: Message): exmsg = await m.reply_text(tlang(m, "dev.chatlist.exporting")) await c.send_message( MESSAGE_DUMP, f"#CHATLIST\n\n**User:** {(await mention_markdown(m.from_user.first_name, m.from_user.id))}", ) all_chats = (Chats.list_chats_full()) or {} chatfile = tlang(m, "dev.chatlist.header") P = 1 for chat in all_chats: try: chat_info = await c.get_chat(chat["_id"]) chat_members = chat_info.members_count try: invitelink = chat_info.invite_link except KeyError: invitelink = "No Link!" chatfile += f"{P}. {chat['chat_name']} | {chat['_id']} | {chat_members} | {invitelink}\n" P += 1 except ChatAdminRequired: pass except (ChannelPrivate, ChannelInvalid): Chats.remove_chat(chat["_id"]) except PeerIdInvalid: LOGGER.warning(f"Peer not found {chat['_id']}") except FloodWait as ef: LOGGER.error("FloodWait required, Sleeping for 60s") LOGGER.error(ef) sleep(60) except RPCError as ef: LOGGER.error(ef) await m.reply_text(f"**Error:**\n{ef}") with BytesIO(str.encode(await remove_markdown_and_html(chatfile))) as f: f.name = "chatlist.txt" await m.reply_document( document=f, caption=(tlang(m, "dev.chatlist.chats_in_db")), ) await exmsg.delete() return
async def get_private_rules(_, m: Message, help_option: str): chat_id = int(help_option.split("_")[1]) rules = Rules(chat_id).get_rules() chat_title = Chats.get_chat_info(chat_id)["chat_name"] if not rules: await m.reply_text( "The Admins of that group have not setup any rules, that dosen't mean you break the decorum of the chat!", quote=True, ) return "" await m.reply_text( f"""The rules for <b>{escape(chat_title)} are</b>:\n {rules} """, quote=True, disable_web_page_preview=True, ) return ""
from alita.database.chats_db import Chats from alita.database.filters_db import Filters from alita.database.notes_db import Notes, NotesSettings from alita.database.rules_db import Rules from alita.database.users_db import Users from alita.database.warns_db import Warns, WarnSettings from alita.utils.custom_filters import command, dev_filter # initialise bldb = Blacklist() gbandb = GBan() notesdb = Notes() rulesdb = Rules() userdb = Users() appdb = Approve() chatdb = Chats() fldb = Filters() pinsdb = Pins() notesettings_db = NotesSettings() warns_db = Warns() warns_settings_db = WarnSettings() @Alita.on_message(command("stats", DEV_PREFIX_HANDLER) & dev_filter) async def get_stats(_, m: Message): replymsg = await m.reply_text("<b><i>Fetching Stats...</i></b>", quote=True) rply = ( f"<b>Users:</b> <code>{(userdb.count_users())}</code> in <code>{(chatdb.count_chats())}</code> chats\n" f"<b>Anti Channel Pin:</b> <code>{(pinsdb.count_chats('antichannelpin'))}</code> enabled chats\n" f"<b>Clean Linked:</b> <code>{(pinsdb.count_chats('cleanlinked'))}</code> enabled chats\n" f"<b>Filters:</b> <code>{(fldb.count_filters_all())}</code> in <code>{(fldb.count_filters_chats())}</code> chats\n"
) from alita import HELP_COMMANDS, LOGGER, SUPPORT_GROUP from alita.bot_class import Alita from alita.database.chats_db import Chats from alita.database.notes_db import Notes from alita.database.rules_db import Rules from alita.tr_engine import tlang from alita.utils.cmd_senders import send_cmd from alita.utils.msg_types import Types from alita.utils.string import build_keyboard, parse_button # Initialize rules_db = Rules() notes_db = Notes() chats_db = Chats() async def gen_cmds_kb(m: Message or CallbackQuery): """Generate the keyboard for languages.""" if isinstance(m, CallbackQuery): m = m.message cmds = sorted(list(HELP_COMMANDS.keys())) kb = [] while cmds: if cmds: cmd = cmds[0] a = [ InlineKeyboardButton(
async def initial_works(_, m: Message): chatdb = Chats(m.chat.id) try: if m.migrate_to_chat_id or m.migrate_from_chat_id: new_chat = m.migrate_to_chat_id or m.chat.id try: await migrate_chat(m, new_chat) except RPCError as ef: LOGGER.error(ef) return elif m.reply_to_message and not m.forward_from: chatdb.update_chat( m.chat.title, m.reply_to_message.from_user.id, ) Users(m.reply_to_message.from_user.id).update_user( ( f"{m.reply_to_message.from_user.first_name} {m.reply_to_message.from_user.last_name}" if m.reply_to_message.from_user.last_name else m.reply_to_message.from_user.first_name ), m.reply_to_message.from_user.username, ) elif m.forward_from and not m.reply_to_message: chatdb.update_chat( m.chat.title, m.forward_from.id, ) Users(m.forward_from.id).update_user( ( f"{m.forward_from.first_name} {m.forward_from.last_name}" if m.forward_from.last_name else m.forward_from.first_name ), m.forward_from.username, ) elif m.reply_to_message: chatdb.update_chat( m.chat.title, m.reply_to_message.forward_from.id, ) Users(m.forward_from.id).update_user( ( f"{m.reply_to_message.forward_from.first_name} {m.reply_to_message.forward_from.last_name}" if m.reply_to_message.forward_from.last_name else m.reply_to_message.forward_from.first_name ), m.forward_from.username, ) else: chatdb.update_chat(m.chat.title, m.from_user.id) Users(m.from_user.id).update_user( ( f"{m.from_user.first_name} {m.from_user.last_name}" if m.from_user.last_name else m.from_user.first_name ), m.from_user.username, ) except AttributeError: pass # Skip attribute errors! return
async def get_private_note(c: Alita, m: Message, help_option: str): """Get the note in pm of user, with parsing enabled.""" from alita import BOT_USERNAME help_lst = help_option.split("_") if len(help_lst) == 2: chat_id = int(help_lst[1]) all_notes = notes_db.get_all_notes(chat_id) chat_title = Chats.get_chat_info(chat_id)["chat_name"] rply = f"Notes in {chat_title}:\n\n" note_list = [ f"- [{note[0]}](https://t.me/{BOT_USERNAME}?start=note_{chat_id}_{note[1]})" for note in all_notes ] rply = "\n".join(note_list) rply += "You can retrieve these notes by tapping on the notename." await m.reply_text(rply, disable_web_page_preview=True, quote=True) return if len(help_lst) != 3: return note_hash = help_option.split("_")[2] getnotes = notes_db.get_note_by_hash(note_hash) if not getnotes: await m.reply_text("Note does not exist", quote=True) return msgtype = getnotes["msgtype"] if not msgtype: await m.reply_text( "<b>Error:</b> Cannot find a type for this note!!", quote=True, ) return try: # support for random notes texts splitter = "%%%" note_reply = getnotes["note_value"].split(splitter) note_reply = choice(note_reply) except KeyError: note_reply = "" parse_words = [ "first", "last", "fullname", "username", "id", "chatname", "mention", ] text = await escape_mentions_using_curly_brackets(m, note_reply, parse_words) if msgtype == Types.TEXT: teks, button = await parse_button(text) button = await build_keyboard(button) button = ikb(button) if button else None if button: try: await m.reply_text( teks, reply_markup=button, disable_web_page_preview=True, quote=True, ) return except RPCError as ef: await m.reply_text( "An error has occured! Cannot parse note.", quote=True, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await m.reply_text(teks, quote=True, disable_web_page_preview=True) return elif msgtype in ( Types.STICKER, Types.VIDEO_NOTE, Types.CONTACT, Types.ANIMATED_STICKER, ): await (await send_cmd(c, msgtype))(m.chat.id, getnotes["fileid"]) else: if getnotes["note_value"]: teks, button = await parse_button(getnotes["note_value"]) button = await build_keyboard(button) button = ikb(button) if button else None else: teks = "" button = None if button: try: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=teks, reply_markup=button, ) return except RPCError as ef: await m.reply_text( teks, quote=True, reply_markup=button, disable_web_page_preview=True, ) LOGGER.error(ef) LOGGER.error(format_exc()) return else: await (await send_cmd(c, msgtype))( m.chat.id, getnotes["fileid"], caption=teks, ) LOGGER.info( f"{m.from_user.id} fetched privatenote {note_hash} (type - {getnotes}) in {m.chat.id}", ) return