async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict, config: Config, lang: str): """ Handle callback button taps on report message in admin group :param call: callback coming from Telegram :param callback_data: callback data parsed by aiogram :param config: bot config :param lang: preferred bot language """ option = callback_data.get("option", "del") user_id = callback_data.get("user_id") message_ids = callback_data.get("message_ids") for msg_id in message_ids.split(","): with suppress(MessageToDeleteNotFound): await call.bot.delete_message(config.group.main, msg_id) if option == "del": await call.message.edit_text(call.message.html_text + get_string(lang, "action_deleted")) elif option == "ban": await call.bot.kick_chat_member(config.group.main, user_id) await call.message.edit_text(call.message.html_text + get_string(lang, "action_deleted_banned")) await call.answer()
async def cmd_nomedia(message: types.Message): """ Handle /nomedia command in main group :param message: Telegram message starting with /nomedia """ lang = message.bot.get("config").lang nomedia_to = await message.chat.get_member(message.reply_to_message.from_user.id) if nomedia_to.is_chat_admin(): await message.reply(get_string(lang, "error_restrict_admin")) return user = await message.chat.get_member(message.from_user.id) if not user.is_chat_admin() or user.can_restrict_members is False: return nomedia_period = get_restriction_period(message.text) nomedia_end_date = message.date + timedelta(seconds=nomedia_period) await message.chat.restrict( user_id=message.reply_to_message.from_user.id, permissions=types.ChatPermissions(can_send_messages=True), until_date=nomedia_end_date ) if nomedia_period == 0: await message.reply(get_string(lang, "nomedia_forever")) else: await message.reply( get_string(lang, "nomedia_temporary").format(time=nomedia_end_date.strftime("%d.%m.%Y %H:%M")) )
async def cmd_ro(message: types.Message, lang: str): """ Handle /ro command in main group :param message: Telegram message starting with /ro :param lang: preferred bot language """ readonly_to = await message.chat.get_member( message.reply_to_message.from_user.id) if readonly_to.is_chat_admin(): await message.reply(get_string(lang, "error_restrict_admin")) return user = await message.chat.get_member(message.from_user.id) if not user.is_chat_admin() or user.can_restrict_members is False: return ro_period = get_restriction_period(message.text) ro_end_date = message.date + timedelta(seconds=ro_period) await message.chat.restrict(user_id=message.reply_to_message.from_user.id, permissions=types.ChatPermissions(), until_date=ro_end_date) if ro_period == 0: await message.reply(get_string(lang, "readonly_forever")) else: await message.reply( get_string(lang, "readonly_temporary").format( time=ro_end_date.strftime("%d.%m.%Y %H:%M")))
async def callbacks_on_report_msg(call: types.CallbackQuery, callback_data: dict): """ Handle callback button taps on report message in admin group :param call: callback coming from Telegram :param callback_data: callback data parsed by aiogram """ config = call.bot.get("config") option = callback_data.get("option", "del") user_id = callback_data.get("user_id") message_ids = callback_data.get("message_ids") delete_ok = True for msg_id in message_ids.split(","): try: await call.bot.delete_message(config.group.main, msg_id) except (MessageToDeleteNotFound, MessageCantBeDeleted): delete_ok = False if option == "del": await call.message.edit_text(call.message.html_text + get_string(config.lang, "action_deleted")) elif option == "ban": await call.bot.kick_chat_member(config.group.main, user_id) await call.message.edit_text( call.message.html_text + get_string(config.lang, "action_deleted_banned")) if delete_ok: await call.answer() else: await call.answer(show_alert=True, text=get_string(config.lang, "action_deleted_partially"))
async def calling_all_units(message: types.Message, config: Config, lang: str): """ Notifying all admins about something's going on in main group :param message: Telegram message starting with @admin :param config: bot config :param lang: preferred bot language """ msg_url = get_message_url(message.chat.id, message.message_id) text = get_string(lang, "need_admins_attention").format(msg_url=msg_url) await message.bot.send_message(config.group.reports, text)
async def calling_all_units(message: types.Message): """ Notifying all admins about something's going on in main group :param message: Telegram message starting with @admin """ config = message.bot.get("config") msg_url = get_message_url(message.chat.id, message.message_id) text = get_string(config.lang, "need_admins_attention").format(msg_url=msg_url) await message.bot.send_message(config.group.reports, text)
async def cmd_report(message: types.Message, config: Config, lang: str): """ Handle /report command in main group :param message: Telegram message starting with /report :param config: bot config :param lang: preferred bot language """ reported = await message.chat.get_member( message.reply_to_message.from_user.id) if reported.is_chat_admin(): await message.reply(get_string(lang, "error_report_admin")) return available_options = { get_string(lang, "action_del_msg"): "del", get_string(lang, "action_del_and_ban"): "ban" } parts = message.text.split(maxsplit=1) report_msg_template = get_string(lang, "report_message") if len(parts) == 2: report_msg_template += get_string( lang, "report_note").format(note=quote_html(parts[1])) msg = await message.reply(get_string(lang, "report_sent")) kb = types.InlineKeyboardMarkup() for button_text, option in available_options.items(): kb.add( types.InlineKeyboardButton( text=button_text, callback_data=report_msg_cb.new( option=option, user_id=message.reply_to_message.from_user.id, # Collect all IDs (initial message, report message, report confirmation) to delete afterwards message_ids= f"{message.reply_to_message.message_id},{message.message_id},{msg.message_id}" ))) await message.reply_to_message.forward(config.group.reports) await message.bot.send_message( config.group.reports, report_msg_template.format( time=message.reply_to_message.date.strftime( get_string(lang, "report_date_format")), msg_url=get_message_url(message.chat.id, message.reply_to_message.message_id), ), reply_markup=kb)
async def error_no_reply(message: types.Message, lang: str): await message.reply(get_string(lang, "error_no_reply"))
async def error_no_reply(message: types.Message): lang = message.bot.get("config").lang await message.reply(get_string(lang, "error_no_reply"))