PagerMaid-Pyro/pagermaid/modules/sudo.py

132 lines
5.9 KiB
Python
Raw Normal View History

2022-05-23 12:40:30 +00:00
from pagermaid.single_utils import sqlite
from pagermaid.listener import listener
from pagermaid.group_manager import add_permission_for_group, Permission, remove_permission_for_group, \
add_user_to_group, remove_user_from_group, add_permission_for_user, remove_permission_for_user, \
permissions
from pagermaid.utils import lang, Message, edit_delete, _status_sudo
from pagermaid.single_utils import get_sudo_list
def from_msg_get_sudo_id(message: Message) -> int:
2022-06-20 13:55:14 +00:00
if reply := message.reply_to_message:
2022-05-23 12:40:30 +00:00
return reply.from_user.id if reply.from_user else reply.sender_chat.id
else:
return message.chat.id
2022-06-20 13:55:14 +00:00
@listener(is_plugin=False, command="sudo",
2022-05-23 12:40:30 +00:00
need_admin=True,
parameters="{on|off|add|remove|gaddp|gaddu|gdelp|gdelu|glist|uaddp|udelp|list}",
description=lang('sudo_des'))
2022-06-20 13:55:14 +00:00
async def sudo_change(message: Message):
2022-05-23 12:40:30 +00:00
""" To enable or disable sudo of your userbot. """
input_str = message.arguments
sudo = get_sudo_list()
if input_str == "on":
if _status_sudo():
return await edit_delete(message, lang('sudo_has_enabled'))
sqlite["sudo_enable"] = True
text = f"__{lang('sudo_enable')}__\n"
if len(sudo) != 0:
return await message.edit(
text,
)
text += f"**{lang('sudo_no_sudo')}**"
return await message.edit(
text,
)
elif input_str == "off":
if _status_sudo():
del sqlite["sudo_enable"]
text = f"__{lang('sudo_disable')}__\n"
if len(sudo) != 0:
return await message.edit(
text,
)
text += f"**{lang('sudo_no_sudo')}**"
return await message.edit(
text,
)
await edit_delete(message, lang('sudo_has_disabled'))
elif input_str == "add":
from_id = from_msg_get_sudo_id(message)
if from_id in sudo:
return await edit_delete(message, f"__{lang('sudo_add')}__")
sudo.append(from_id)
sqlite["sudo_list"] = sudo
add_user_to_group(str(from_id), "default") # 添加到默认组
if from_id > 0:
await message.edit(f"__{lang('sudo_add')}__")
else:
await message.edit(f"__{lang('sudo_add_chat')}__")
elif input_str == "remove":
from_id = from_msg_get_sudo_id(message)
if from_id not in sudo:
return await edit_delete(message, f"__{lang('sudo_no')}__")
sudo.remove(from_id)
sqlite["sudo_list"] = sudo
if from_id > 0:
await message.edit(f"__{lang('sudo_remove')}__")
else:
await message.edit(f"__{lang('sudo_remove_chat')}__")
elif input_str == "list":
if len(sudo) == 0:
return await edit_delete(message, f"__{lang('sudo_no_one')}__")
text = f"**{lang('sudo_list')}**\n\n"
for i in sudo:
try:
if i > 0:
2022-06-20 13:55:14 +00:00
user = await message.bot.get_users(i)
2022-05-23 12:40:30 +00:00
text += f"{user.mention()} - {' '.join(permissions.get_roles_for_user(str(i)))}\n"
else:
2022-06-20 13:55:14 +00:00
chat = await message.bot.get_chat(i)
2022-05-23 12:40:30 +00:00
text += f"{chat.title} - {' '.join(permissions.get_roles_for_user(str(i)))}\n"
for j in permissions.get_permissions_for_user(str(i)):
text += f"{'-' if j[2] == 'ejection' else ''}{j[1]}\n"
2022-06-20 13:55:14 +00:00
except Exception:
2022-05-23 12:40:30 +00:00
text += f"• `{i}` - {' '.join(permissions.get_roles_for_user(str(i)))}\n"
await message.edit(text)
elif len(message.parameter) > 0:
if len(message.parameter) == 2:
from_id = from_msg_get_sudo_id(message)
if message.parameter[0] == "glist":
2022-06-20 13:55:14 +00:00
if not (
data := permissions.get_permissions_for_user(
str(message.parameter[1])
)
):
2022-05-23 12:40:30 +00:00
return await edit_delete(message, f"__{lang('sudo_group_list')}__")
2022-06-20 13:55:14 +00:00
text = f"**{message.parameter[1]} {lang('sudo_group_list')}**\n\n"
for i in data:
text += f" • `{'-' if i[2] == 'ejection' else ''}{i[1]}`\n"
return await message.edit(text)
2022-05-23 12:40:30 +00:00
if from_id not in sudo:
return await edit_delete(message, f"__{lang('sudo_no')}__")
elif message.parameter[0] == "gaddu":
add_user_to_group(str(from_id), message.parameter[1])
return await message.edit(lang("sudo_group_add_user"))
elif message.parameter[0] == "gdelu":
remove_user_from_group(str(from_id), message.parameter[1])
return await message.edit(lang("sudo_group_del_user"))
elif message.parameter[0] == "uaddp":
add_permission_for_user(str(from_id), Permission(message.parameter[1]))
return await message.edit(lang("sudo_user_add_per"))
elif message.parameter[0] == "udelp":
remove_permission_for_user(str(from_id), Permission(message.parameter[1]))
return await message.edit(lang("sudo_user_del_per"))
else:
return await edit_delete(message, lang('arg_error'))
if len(message.parameter) == 3:
if message.parameter[0] == "gaddp":
add_permission_for_group(message.parameter[1], Permission(message.parameter[2]))
return await message.edit(lang("sudo_group_add_per"))
elif message.parameter[0] == "gdelp":
remove_permission_for_group(message.parameter[1], Permission(message.parameter[2]))
return await message.edit(lang("sudo_group_del_per"))
else:
return await edit_delete(message, lang('arg_error'))
else:
await edit_delete(message, lang('arg_error'))
else:
await edit_delete(message, lang('arg_error'))