Created
February 4, 2019 01:49
-
-
Save dogtopus/e159cb54cff21e28878eb4dc0b0b5a91 to your computer and use it in GitHub Desktop.
tql, wsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import os | |
import pyrogram | |
import sys | |
from collections import OrderedDict | |
def make_bot(session, persistent_path): | |
app = pyrogram.Client(session) | |
if os.path.isfile(persistent_path): | |
with open(persistent_path, 'r') as f: | |
persistent = json.load(f) | |
elif os.path.isdir(persistent_path): | |
raise IsADirectoryError(persistent_path) | |
else: | |
persistent = {'priv_ids': [], 'perms': {}} | |
priv_ids = persistent['priv_ids'] | |
perm_store = persistent['perms'] | |
def private_guard(f): | |
def wrapper(ctx, msg): | |
if msg.chat.type in ('private', 'channel'): | |
msg.reply('ERROR: This is a private chat') | |
else: | |
f(ctx, msg) | |
return wrapper | |
@app.on_message(pyrogram.Filters.command('start')) | |
def handle_start(ctx, msg): | |
msg.reply('Add me to a group and add admin permission, then use ' | |
'/tql to save admin permission and /restore to load.') | |
@app.on_message(pyrogram.Filters.command('tql')) | |
@private_guard | |
def handle_save(ctx, msg): | |
# check for permission | |
chat_id = msg.chat.id | |
bot_info = ctx.get_chat_member(chat_id, 'me') | |
if bot_info.status not in ('creator', 'administrator') or not bot_info.can_promote_members: | |
msg.reply('ERROR: Insufficient permission') | |
return | |
user_info = ctx.get_chat_member(chat_id, msg.from_user.id) | |
if user_info.status == 'creator': | |
msg.reply('ERROR: shidalao, wsl') | |
elif user_info.status == 'administrator': | |
permissions = OrderedDict(( | |
('i', user_info.can_change_info), | |
('d', user_info.can_delete_messages), | |
('r', user_info.can_restrict_members), | |
('a', user_info.can_invite_users), | |
('s', user_info.can_pin_messages), | |
('p', user_info.can_promote_members), | |
)) | |
flags = ''.join(f if p else '-' for f, p in permissions.items()) | |
perm_store[f'{msg.from_user.id}@{chat_id}'] = permissions | |
with open(persistent_path, 'w') as f: | |
json.dump(persistent, f) | |
msg.reply(f'ID: {msg.from_user.id}\nChat: {chat_id}\nPerm: {flags}\ntql') | |
else: | |
msg.reply('ERROR: Not an admin') | |
@app.on_message(pyrogram.Filters.command('restore')) | |
@private_guard | |
def handle_load(ctx, msg): | |
# check for permission | |
chat_id = msg.chat.id | |
bot_info = ctx.get_chat_member(chat_id, 'me') | |
if bot_info.status not in ('creator', 'administrator') or not bot_info.can_promote_members: | |
msg.reply('ERROR: Insufficient permission') | |
return | |
user_info = ctx.get_chat_member(chat_id, msg.from_user.id) | |
key = f'{msg.from_user.id}@{chat_id}' | |
if key not in perm_store: | |
msg.reply('ERROR: User not registered') | |
elif user_info.status in ('creator', 'administrator'): | |
msg.reply('ERROR: shidalao, wsl') | |
else: | |
perm = perm_store[key] | |
ctx.promote_chat_member(chat_id, msg.from_user.id, | |
can_change_info=perm['i'], | |
can_delete_messages=perm['d'], | |
can_restrict_members=perm['r'], | |
can_invite_users=perm['a'], | |
can_pin_messages=perm['s'], | |
can_promote_members=perm['p']) | |
msg.reply('OK') | |
@app.on_message(pyrogram.Filters.command('wsl')) | |
@private_guard | |
def handle_self_exit(ctx, msg): | |
chat_id = msg.chat.id | |
if msg.from_user.id in priv_ids: | |
msg.reply('wsl, tql') | |
ctx.leave_chat(chat_id) | |
return app | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print(f'Usage: {sys.argv[0]} token persistent-store') | |
sys.exit(1) | |
make_bot(sys.argv[1], sys.argv[2]).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment