async def _append_msg(variable: dict, chat: int, event: int) -> None: if chat in variable: variable[chat]['mentions'].append(event) else: notif = await client( functions.account.GetNotifySettingsRequest(peer=chat)) notif = types.InputPeerNotifySettings(**vars(notif)) await _update_notif_settings(chat) async for dialog in client.iter_dialogs(): if chat == dialog.entity.id: title = getattr(dialog, 'title', dialog.name) unread_count = dialog.unread_count last_msg = dialog.message.id break x = 1 messages = [] async for message in client.iter_messages(chat, max_id=last_msg): if x >= unread_count: if not messages: messages.append(message.id) break if not message.out: x = x + 1 messages.append(message.id) variable[chat] = { 'title': title, 'unread_from': messages[-1], 'mentions': [event], 'PeerNotifySettings': notif } messages.clear()
async def _delete_sticker_messages(offset): messages = [offset] async for msg in client.iter_messages(entity="@Stickers", offset_id=offset.id, reverse=True): messages.append(msg) return await client.delete_messages('@Stickers', messages)
async def _delete_sticker_messages( message: types.Message) -> Sequence[types.messages.AffectedMessages]: messages = [message] async for msg in client.iter_messages(entity="@Stickers", offset_id=message.id, reverse=True): messages.append(msg) return await client.delete_messages('@Stickers', messages)
async def sed_substitute(event: NewMessage.Event) -> None: """ Perfom a GNU like SED substitution of the matched text. **{prefix}[line]s[ed]/(expression)/(substitution)/[flags][;]** Everything inside the brackets is optional. You can perform case conversions in the substitution as well. The semi-colon is mandatory to perform multiple subs in one go. """ matches = event.matches reply = await event.get_reply_message() try: if reply: original = reply if not original: return newStr = await sub_matches(matches, original.text) if newStr: await original.reply("**SED**:\n\n" + newStr) else: total_messages = [] # Append messages to avoid timeouts count = 0 # Don't fetch more than ten texts/captions async for msg in client.iter_messages( event.chat_id, offset_id=event.message.id ): if msg.raw_text: total_messages.append(msg) count += 1 else: continue if count >= 10: break for message in total_messages: newStr = await sub_matches(matches, message.text) if newStr: await message.reply("**SED**\n\n" + newStr) break except Exception as e: await event.answer( ( f"{event.text}" "\n\n" "Like regexbox says, f**k me.\n" "`" f"{str(type(e))}" ":` `" f"{str(e)}" "`" ), reply=True, ) raise e
async def boiltheoil(event: NewMessage.Event) -> None: """ Deep fry images and stickers! `{prefix}fry <level>` """ frycount = ( int(event.matches[0].group(2)) if event.matches[0].group(2) else random.randint(1, 3) ) if event.reply_to_msg_id: potato = await event.get_reply_message() if not await _is_fryable_event(potato): await event.answer("`Invalid message type!`") return else: potato = None async for msg in client.iter_messages( event.chat_id, offset_id=event.message.id, limit=10 ): if await _is_fryable_event(msg): potato = msg break if not potato: await event.answer( "`Couldn't find any acceptable media in the recent messages.`" ) return # download photo as byte array. await event.answer("`Firing up the deep-fryer!`") image = io.BytesIO() await potato.download_media(file=image) image = Image.open(image) # fry the image for _ in range(frycount): image = await deepfry(image) fried_io = io.BytesIO() fried_io.name = "deepfried_image.jpeg" image.save(fried_io, "JPEG") fried_io.seek(0) try: await event.answer(file=fried_io, reply_to=event.reply_to_msg_id) await event.delete() except rpcerrorlist.TimeoutError: await event.answer("`Ran out of oil to fry this pic :P`")
async def sed_substitute(event: NewMessage.Event) -> None: """Perfom a GNU like SED substitution of the matched text.""" matches = event.matches reply = await event.get_reply_message() try: if reply: original = reply if not original: return newStr = await sub_matches(matches, original.text) if newStr: await original.reply("**「sed」**\n\n" + newStr) else: total_messages = [] # Append messages to avoid timeouts count = 0 # Don't fetch more than ten texts/captions async for msg in client.iter_messages( event.chat_id, offset_id=event.message.id ): if msg.raw_text: total_messages.append(msg) count += 1 else: continue if count >= 10: break for message in total_messages: newStr = await sub_matches(matches, message.text) if newStr: await message.reply("**「sed」**\n\n" + newStr) break except Exception as e: await event.answer( ( f"{event.text}" "\n\n" "Like regexbox says, f**k me.\n" "`" f"{str(type(e))}" ":` `" f"{str(e)}" "`" ), reply=True, ) raise e
async def sed_substitute(event: NewMessage.Event) -> None: """Perfom a GNU like SED substitution of the matched text.""" if not re.match(ub_sed_pattern.format(client.prefix or '.'), event.raw_text): return matches = event.matches reply = await event.get_reply_message() try: if reply: original = reply if not original: return newStr = await sub_matches(matches, original.raw_text) if newStr: await original.reply('[SED]\n\n' + newStr) else: total_messages = [] # Append messages to avoid timeouts count = 0 # Don't fetch more than ten texts/captions async for msg in client.iter_messages(event.chat_id, offset_id=event.message.id): if msg.raw_text: total_messages.append(msg) count += 1 else: continue if count >= 10: break for message in total_messages: newStr = await sub_matches(matches, message.raw_text) if newStr: await message.reply('[SED]\n\n' + newStr) break except Exception as e: await event.answer((f"{event.raw_text}" '\n\n' 'Like regexbox says, f**k me.\n' '`' f"{str(type(e))}" ':` `' f"{str(e)}" '`'), reply=True)
async def sed_substitute(event): """SED function used to substitution texts for s command""" if not match(r"^(?:[1-9]+sed|[1-9]+s|sed)", event.raw_text, IGNORECASE): return matches = event.matches reply = await event.get_reply_message() try: if reply: original = reply if not original: return newStr = await sub_matches(matches, original.raw_text) if newStr: await original.reply('[SED]\n\n' + newStr) else: total_messages = [] # Append messages to avoid timeouts count = 0 # Don't fetch more than ten texts/captions async for msg in client.iter_messages(event.chat_id, offset_id=event.message.id): if msg.raw_text: total_messages.append(msg) count += 1 else: continue if count >= 10: break for message in total_messages: newStr = await sub_matches(matches, message.raw_text) if newStr: await message.reply('[SED]\n\n' + newStr) break except Exception as e: await event.reply((f"{event.raw_text}" '\n\n' 'Like regexbox says, f**k me.\n' '`' f"{str(type(e))}" ':` `' f"{str(e)}" '`'))
async def delme(event): entity = await event.get_input_chat() amount = event.matches[0].group(1) offset = 0 reverse = False limit = None reply_message = None if event.reply_to_msg_id: reply = await event.get_reply_message() await event.delete() if reply.sender_id == (await client.get_me()).id: reply_message = reply.id offset = reply.id reverse = True if amount: limit = int(amount) elif amount: await event.delete() offset = event.id limit = int(amount) else: await event.delete() offset = event.id limit = 1 messages = [] if reply_message: messages.append(reply_message) async for msg in client.iter_messages(entity=entity, offset_id=offset, reverse=reverse, limit=limit, from_user="******"): messages.append(msg.id) await client.delete_messages(entity, messages) toast = await event.respond( f"`Successfully deleted {len(messages)} messages!`") await sleep(2) await toast.delete()
async def mamma_mia(event: NewMessage.Event) -> None: """Deep fry images and stickers!""" frycount = int(event.matches[0].group(2)) or 1 if event.reply_to_msg_id: potato = await event.get_reply_message() if not await _is_fryable_event(potato): await event.answer("`Invalid message type!`") return else: potato = None async for msg in client.iter_messages(event.chat_id, offset_id=event.message.id, limit=10): if await _is_fryable_event(msg): potato = msg break if not potato: await event.answer( "`Couldn't find any acceptable media in the recent messages.`") return # download photo as byte array. data = potato.photo if potato.photo else potato.media.document image = io.BytesIO() await potato.download_media(file=image) image = PIL.Image.open(image) # fry the image for _ in range(frycount): image = await deepfry(image) fried_io = io.BytesIO() fried_io.name = "deepfried_image.jpeg" image.save(fried_io, "JPEG") fried_io.seek(0) try: await event.answer(file=fried_io) await event.delete() except rpcerrorlist.TimeoutError: await event.answer("`Event timed out.`")
async def purge(event): if ((event.is_channel or event.is_group) and not (event.chat.creator or event.chat.admin_rights.delete_messages)): await event.edit("`You do not have message deleting rights in here!`") return entity = await event.get_input_chat() amount = event.matches[0].group(1) reverse = False limit = None if event.reply_to_msg_id: await event.delete() offset = await event.get_reply_message() reverse = True if amount: limit = int(amount) - 1 elif amount: offset = event limit = int(amount) else: await event.edit("`Purge yourself!`") await sleep(2) await event.delete() return messages = [offset.id] async for msg in client.iter_messages(entity=entity, offset_id=offset.id, reverse=reverse, limit=limit): messages.append(msg.id) await client.delete_messages(entity, messages) toast = await event.respond( f"`Successfully deleted {len(messages)} messages!`") await sleep(2) await toast.delete()
async def kang(event: NewMessage.Event) -> None: """ Steal (AKA kang) stickers and images to your Sticker packs. `{prefix}kang [pack] [emojis]` or **{prefix}kang (short)=(title) [emojis]** `pack` and `emojis` can be used as arguments as well. """ match = event.matches[0].group(1) or "" if event.reply_to_msg_id: sticker_event = await event.get_reply_message() if not await _is_sticker_event(sticker_event): await event.answer("`Invalid message type!`") return else: sticker_event = None async for msg in client.iter_messages( event.chat_id, offset_id=event.message.id, limit=10 ): if await _is_sticker_event(msg): sticker_event = msg break if not sticker_event: await event.answer( "`Couldn't find any acceptable media in the recent messages.`" ) return new_pack = False first_msg = None new_first_msg = None args, kwargs = await client.parse_arguments(match) pack, emojis, name, is_animated = await _resolve_messages( args, kwargs, sticker_event ) prefix = client.prefix if client.prefix is not None else "." notif = await client(functions.account.GetNotifySettingsRequest(peer="Stickers")) await _update_stickers_notif(DEFAULT_MUTE) if pack or len(kwargs) == 1: if pack and pack.lower() == "auto": pack, packnick = await _get_userbot_auto_pack(is_animated) if kwargs: pack = None packnick = None for x, y in kwargs.items(): pack = str(x) packnick = str(y) if not (pack and packnick): await event.answer( "`Are you sure you're using the correct syntax?`\n" f"`{prefix}kang <packName>=<packsShortName>`\n" "`You can also choose emojis whilst making a new pack.`" ) await _update_stickers_notif(notif) return packs, first_msg = await _list_packs() is_pack = await _verify_cs_name(pack, packs) if is_pack: pack = is_pack new_pack = False else: new_pack = True attribute_emoji = None if sticker_event.sticker: for attribute in sticker_event.media.document.attributes: if isinstance(attribute, types.DocumentAttributeSticker): attribute_emoji = attribute.alt if attribute_emoji: if emojis and attribute_emoji not in emojis: emojis += attribute_emoji else: emojis = attribute_emoji else: packs, first_msg = await _list_packs() is_pack = await _verify_cs_name(pack, packs) if "_kang_pack" in pack: new_pack = True elif not is_pack: await event.answer( NO_PACK.format( pack, prefix, pack or "<pack username>", emojis or default_emoji ) ) await _delete_sticker_messages(first_msg) await _update_stickers_notif(notif) return else: pack = is_pack else: basic, animated = await _get_default_packs() packs, first_msg = await _list_packs() if is_animated: pack = await _verify_cs_name(animated, packs) if not pack: if "_kang_pack" in animated: await event.answer( "`Making a new animated kang pack, please wait!`" ) pack = animated _, packnick = await _get_userbot_auto_pack(is_animated) new_pack = True else: pack = animated or "a default animated pack" await event.answer(FALSE_DEFAULT.format(pack, prefix)) await _delete_sticker_messages(first_msg) await _update_stickers_notif(notif) return else: pack = await _verify_cs_name(basic, packs) if not pack: if "_kang_pack" in basic: await event.answer("`Making a new kang pack, please wait!`") pack = basic _, packnick = await _get_userbot_auto_pack(is_animated) new_pack = True else: pack = basic or "a default pack" await event.answer(FALSE_DEFAULT.format(pack, prefix)) await _delete_sticker_messages(first_msg) await _update_stickers_notif(notif) return await event.answer(f"`{random.choice(KANGING_STR)}`") async with client.conversation(**conversation_args) as conv: if new_pack: packtype = "/newanimated" if is_animated else "/newpack" new_first_msg = await conv.send_message(packtype) r1 = await conv.get_response() LOGGER.debug("Stickers:" + r1.text) await conv.send_message(packnick) r2 = await conv.get_response() LOGGER.debug("Stickers:" + r2.text) else: await conv.send_message("/addsticker") r1 = await conv.get_response() LOGGER.debug("Stickers:" + r1.text) await conv.send_message(pack) r2 = await conv.get_response() LOGGER.debug("Stickers:" + r2.text) if "120 stickers" in r2.text: if "_kang_pack" in pack: pack, packnick, new_pack = await _get_new_ub_pack( conv, packs, is_animated ) if new_pack: await event.answer( "`Current kang pack is full, making a new one!`" ) await conv.send_message("/cancel") r12 = await conv.get_response() LOGGER.debug("Stickers:" + r12.text) packtype = "/newanimated" if is_animated else "/newpack" await conv.send_message(packtype) r13 = await conv.get_response() LOGGER.debug("Stickers:" + r13.text) await conv.send_message(packnick) r14 = await conv.get_response() LOGGER.debug("Stickers:" + r14.text) else: await event.answer(f"`{pack} has reached it's limit!`") await _delete_sticker_messages(first_msg or new_first_msg) await _update_stickers_notif(notif) return elif ".TGS" in r2.text and not is_animated: await event.answer( "`I can't kang a normal sticker" " to an animated pack!`" ) await _delete_sticker_messages(first_msg or new_first_msg) await _update_stickers_notif(notif) return elif ".PSD" in r2.text and is_animated: await event.answer( "`I can't kang an animated sticker" "to a normal pack!`" ) await _delete_sticker_messages(first_msg or new_first_msg) await _update_stickers_notif(notif) return sticker = io.BytesIO() sticker.name = name await sticker_event.download_media(file=sticker) if ( sticker_event.sticker and sticker_event.sticker.mime_type == "application/x-tgsticker" ): sticker.seek(0) await conv.send_message(file=sticker, force_document=True) else: new_sticker = io.BytesIO() if sticker_event.sticker: resized_sticker = await _resize_image(sticker, new_sticker, False) else: resized_sticker = await _resize_image(sticker, new_sticker) if isinstance(resized_sticker, str): await event.answer(resized_sticker) await _update_stickers_notif(notif) return await conv.send_message(file=new_sticker, force_document=True) new_sticker.close() sticker.close() r3 = await conv.get_response() LOGGER.debug("Stickers:" + r3.text) await conv.send_message(emojis) r4 = await conv.get_response() LOGGER.debug("Stickers:" + r4.text) if new_pack: await conv.send_message("/publish") r5 = await conv.get_response() LOGGER.debug("Stickers:" + r5.text) if is_animated: await conv.send_message("<" + packnick + ">") r41 = await conv.get_response() LOGGER.debug("Stickers:" + r41.text) if r41.text == "Invalid pack selected.": await event.answer("`Invalid pack selected.`") await conv.send_message("/cancel") await conv.get_response() await client.send_read_acknowledge(conv.chat_id) await _update_stickers_notif(notif) return await conv.send_message("/skip") r6 = await conv.get_response() LOGGER.debug("Stickers:" + r6.text) await conv.send_message(pack) r7 = await conv.get_response() LOGGER.debug("Stickers:" + r7.text) if "Sorry" in r7.text: await conv.send_message("/cancel") r61 = await conv.get_response() LOGGER.debug("Stickers:" + r61.text) await event.answer( "`Pack's short name is unacceptable or already taken. " "Try thinking of a better short name.`" ) await _delete_sticker_messages(first_msg or new_first_msg) await _update_stickers_notif(notif) return else: await conv.send_message("/done") r5 = await conv.get_response() LOGGER.debug("Stickers:" + r5.text) pack = f"[{pack}](https://t.me/addstickers/{pack})" extra = await get_chat_link(event, sticker_event.id) await event.answer( f"`Successfully added the sticker to` {pack} `!`", self_destruct=4, log=("kang", f"Successfully kanged a sticker from {extra} to {pack}"), ) await client.send_read_acknowledge(conv.chat_id) await _delete_sticker_messages(first_msg or new_first_msg) await _update_stickers_notif(notif)
async def kang(event): """Steal stickers to your Sticker packs""" if event.reply_to_msg_id: sticker_event = await event.get_reply_message() if not await _is_sticker_event(sticker_event): await event.edit("`Invalid message type!`") return else: sticker_event = None async for msg in client.iter_messages(event.chat_id, offset_id=event.message.id, limit=10): if await _is_sticker_event(msg): sticker_event = msg break if not sticker_event: await event.edit( "`Couldn't find any acceptable media in the recent messages.`") return new_pack = False first_msg = None new_first_msg = None pack, emojis, name, is_animated = await _resolve_messages( event, sticker_event) prefix = client.prefix if client.prefix is not None else '.' if pack: if (':' in pack) or ('=' in pack): text = event.matches[0].group(1) pack, packnick, new_emojis = await _resolve_pack_name( text, is_animated) if not pack and not packnick: await event.edit( "`Are you sure you're using the correct syntax?`\n" f"`{prefix}kang <packName>=<packsShortName>`\n" "`You can also choose emojis whilst making a new pack.`") return packs, first_msg = await _list_packs() is_pack = await _verify_cs_name(pack, packs) if is_pack: pack = is_pack new_pack = False else: new_pack = True attribute_emoji = None if sticker_event.sticker: attribute_emoji = ( attribute.alt for attribute in sticker_event.media.document.attributes if isinstance(attribute, DocumentAttributeSticker)) emojis = new_emojis or attribute_emoji or default_emoji else: packs, first_msg = await _list_packs() is_pack = await _verify_cs_name(pack, packs) if "_kang_pack" in pack: new_pack = True elif not is_pack: await event.edit( NO_PACK.format(pack, prefix, pack or "<pack username>", emojis or default_emoji)) await _delete_sticker_messages(first_msg) return else: pack = is_pack else: basic, animated = await _get_default_packs() packs, first_msg = await _list_packs() if is_animated: pack = await _verify_cs_name(animated, packs) if not pack: if "_kang_pack" in animated: await event.edit("`Making a custom TG-UserBot pack!`") user = await client.get_me() tag = '@' + user.username if user.username else user.id new_pack = True pack = animated packnick = f"{tag}'s animated kang pack" else: pack = animated or "a default animated pack" await event.edit(FALSE_DEFAULT.format(pack, prefix)) await _delete_sticker_messages(first_msg) return else: pack = await _verify_cs_name(basic, packs) if not pack: if "_kang_pack" in basic: await event.edit("`Making a custom TG-UserBot pack!`") user = await client.get_me() tag = '@' + user.username if user.username else user.id new_pack = True pack = basic packnick = f"{tag}'s kang pack" else: pack = basic or "a default pack" await event.edit(FALSE_DEFAULT.format(pack, prefix)) await _delete_sticker_messages(first_msg) return await event.edit( "`Turning on the kang machine... Your sticker? My sticker!`") async with client.conversation(**conversation_args) as conv: if new_pack: packtype = "/newanimated" if is_animated else "/newpack" new_first_msg = await conv.send_message(packtype) r1 = await conv.get_response() LOGGER.debug("Stickers:" + r1.text) await client.send_read_acknowledge(conv.chat_id) await conv.send_message(packnick) r2 = await conv.get_response() LOGGER.debug("Stickers:" + r2.text) await client.send_read_acknowledge(conv.chat_id) else: await conv.send_message('/addsticker') r1 = await conv.get_response() LOGGER.debug("Stickers:" + r1.text) await client.send_read_acknowledge(conv.chat_id) await conv.send_message(pack) r2 = await conv.get_response() LOGGER.debug("Stickers:" + r2.text) await client.send_read_acknowledge(conv.chat_id) if "120 stickers" in r2.text: if "_kang_pack" in pack: await event.edit( "`Current userbot pack is full, making a new one!`") await conv.send_message('/cancel') r11 = await conv.get_response() LOGGER.debug("Stickers:" + r11.text) await client.send_read_acknowledge(conv.chat_id) pack, packnick = await _get_new_ub_pack(packs, is_animated) packtype = "/newanimated" if is_animated else "/newpack" await conv.send_message(packtype) r12 = await conv.get_response() LOGGER.debug("Stickers:" + r12.text) await client.send_read_acknowledge(conv.chat_id) await conv.send_message(packnick) r13 = await conv.get_response() LOGGER.debug("Stickers:" + r13.text) await client.send_read_acknowledge(conv.chat_id) new_pack = True else: await event.edit(f"`{pack} has reached it's limit!`") await _delete_sticker_messages(first_msg or new_first_msg) return elif ".TGS" in r2.text and not is_animated: await event.edit( "`You're trying to kang a normal sticker " "to an animated pack. Choose the correct pack!`") await _delete_sticker_messages(first_msg or new_first_msg) return elif ".PSD" in r2.text and is_animated: await event.edit("`You're trying to kang an animated sticker " "to a normal pack. Choose the correct pack!`") await _delete_sticker_messages(first_msg or new_first_msg) return sticker = BytesIO() sticker.name = name await sticker_event.download_media(file=sticker) sticker.seek(0) if sticker_event.sticker: await conv.send_message(file=sticker, force_document=True) else: new_sticker = BytesIO() resized_sticker = await _resize_image(sticker, new_sticker) new_sticker.name = name new_sticker.seek(0) await conv.send_message(file=resized_sticker, force_document=True) new_sticker.close() sticker.close() r3 = await conv.get_response() LOGGER.debug("Stickers:" + r3.text) await client.send_read_acknowledge(conv.chat_id) await conv.send_message(emojis) r4 = await conv.get_response() LOGGER.debug("Stickers:" + r4.text) await client.send_read_acknowledge(conv.chat_id) if new_pack: await conv.send_message('/publish') r5 = await conv.get_response() LOGGER.debug("Stickers:" + r5.text) await client.send_read_acknowledge(conv.chat_id) if is_animated: await conv.send_message('<' + packnick + '>') r41 = await conv.get_response() LOGGER.debug("Stickers:" + r41.text) await client.send_read_acknowledge(conv.chat_id) if r41.text == "Invalid pack selected.": await event.edit("`You tried to kang to an invalid pack.`") await conv.send_message('/cancel') await conv.get_response() await client.send_read_acknowledge(conv.chat_id) return await conv.send_message('/skip') r6 = await conv.get_response() LOGGER.debug("Stickers:" + r6.text) await client.send_read_acknowledge(conv.chat_id) await conv.send_message(pack) r7 = await conv.get_response() LOGGER.debug("Stickers:" + r7.text) await client.send_read_acknowledge(conv.chat_id) if "Sorry" in r7.text: await conv.send_message('/cancel') r61 = await conv.get_response() LOGGER.debug("Stickers:" + r61.text) await client.send_read_acknowledge(conv.chat_id) await event.edit( "`Pack's short name is unacceptable or already taken. " "Try thinking of a better short name.`") await _delete_sticker_messages(first_msg or new_first_msg) return else: await conv.send_message('/done') r5 = await conv.get_response() LOGGER.debug("Stickers:" + r5.text) await client.send_read_acknowledge(conv.chat_id) await event.edit("`Successfully added the sticker to` " f"[{pack}](https://t.me/addstickers/{pack})`!`") await _delete_sticker_messages(first_msg or new_first_msg)