Skip to content

Instantly share code, notes, and snippets.

@Kyle2142
Last active July 2, 2021 11:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kyle2142/bfc3928394ca2244cf4460cbd9c3e3f2 to your computer and use it in GitHub Desktop.
Save Kyle2142/bfc3928394ca2244cf4460cbd9c3e3f2 to your computer and use it in GitHub Desktop.
Some helper scripts that might be of use, for multi-userbot, multi-group administration
# bans user from all chats in common with each client
def banFromCommon(clients, user):
from telethon.tl.functions.messages import GetCommonChatsRequest
from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import Channel, ChannelBannedRights
rights = ChannelBannedRights(view_messages=True, until_date=0)
for client in clients:
count = 1
oldcount = 0
while (count != oldcount):
# condition handles cases where banning was not possible,
# checks whether banning didn't change common chat number
result = client(GetCommonChatsRequest(user, 0, 100))
count = len(result.chats)
for chat in result.chats:
if not isinstance(chat, Channel) or not chat.megagroup: continue # skip non-supergroups
try:
client(EditBannedRequest(chat, user, rights))
except Exception as e:
print(chat.title, e)
oldcount = count
# Deletes all messages containing bad_stuff (string) from all chats in all clients.
# NB: must have appropriate permissions to delete from supergroups
# `clients` must be iterable of TelegramClient objects.
def deleteAll(clients, bad_stuff):
from itertools import groupby
from telethon.tl.functions.messages import SearchGlobalRequest
from telethon.tl.types import InputPeerEmpty
for client in clients:
messages = client(SearchGlobalRequest(bad_stuff, 0, InputPeerEmpty(), 0, 100)).messages
# next instruction sorts by channel ID to prepare for groupby (requires sorted list)
messages = sorted(messages, key=lambda x: x.to_id.channel_id)
for channel, message_iterator in groupby(messages, lambda x: x.to_id.channel_id):
msgs = list(message_iterator)
try:
client.delete_messages(channel, msgs) # this function accepts list of Message objects as 2nd param
except Exception as e:
print(channel, e)
# Attempts to encounter userID in all clients.
# msg_lim is optional, but will greatly affect worst-case time to complete.
def encounterAll(clients, userID, msg_lim=10):
for client in clients:
try:
client.get_input_entity(userID)
except ValueError: # no peer was found in internal DB
found = False
for dialog in client.iter_dialogs(None):
for msg in client.iter_messages(dialog.input_entity, limit=msg_lim):
if msg.from_id == userID:
found = True
break
if found: break
# Finds any message from spammerID (within msg_lim), reports it and deletes all messages from that user
# NB: Does not ban the user. Edit script if required
def reportAndDeleteAll(clients, spammerID, msg_lim=100):
from telethon.tl.functions.channels import DeleteUserHistoryRequest, ReportSpamRequest
from telethon.tl.types import Channel
for client in clients:
for dialog in client.iter_dialogs(None):
if not isinstance(dialog.entity, Channel) or not dialog.entity.megagroup: continue
for msg in client.iter_messages(dialog.input_entity, limit=msg_lim):
if msg.from_id == spammerID:
try:
client(ReportSpamRequest(dialog.input_entity, spammerID, [msg.id]))
client(DeleteUserHistoryRequest(dialog.input_entity, spammerID))
# you could call EditBannedRequest here
except Exception as e: # continuing with script is more important than handling specific errors
print(dialog.name, e)
break

All of these were created to work with multiple client situations, but you can easily pass [client] to work on one.

Note that in most cases, you will need to tweak the instructions (e.g. by adding exception handling) if the clients you run it on are not admins, or other things that happen in "less-than-perfect" situations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment