PagerMaid-Pyro/pagermaid/modules/sudo.py

155 lines
6.3 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
2023-03-12 03:56:01 +00:00
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,
)
2022-09-01 08:35:19 +00:00
from pagermaid.enums import Client, Message
from pagermaid.utils import lang, edit_delete, _status_sudo
2022-05-23 12:40:30 +00:00
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
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
command="sudo",
need_admin=True,
parameters="{on|off|add|remove|gaddp|gaddu|gdelp|gdelu|glist|uaddp|udelp|list}",
description=lang("sudo_des"),
)
2022-09-01 08:35:19 +00:00
async def sudo_change(client: Client, message: Message):
2023-03-12 03:56:01 +00:00
"""To enable or disable sudo of your userbot."""
2022-05-23 12:40:30 +00:00
input_str = message.arguments
sudo = get_sudo_list()
if input_str == "on":
if _status_sudo():
2023-03-12 03:56:01 +00:00
return await edit_delete(message, lang("sudo_has_enabled"))
2022-05-23 12:40:30 +00:00
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,
)
2023-03-12 03:56:01 +00:00
await edit_delete(message, lang("sudo_has_disabled"))
2022-05-23 12:40:30 +00:00
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"
2023-05-22 14:57:23 +00:00
for i in sudo.copy():
2022-05-23 12:40:30 +00:00
try:
if i > 0:
2022-09-01 08:35:19 +00:00
user = await client.get_users(i)
2023-05-22 14:57:23 +00:00
if user.is_deleted:
sudo.remove(i)
sqlite["sudo_list"] = sudo
continue
2022-05-23 12:40:30 +00:00
text += f"{user.mention()} - {' '.join(permissions.get_roles_for_user(str(i)))}\n"
else:
2022-09-01 08:35:19 +00:00
chat = await client.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:
2023-03-12 03:56:01 +00:00
text += (
f"• `{i}` - {' '.join(permissions.get_roles_for_user(str(i)))}\n"
)
2022-05-23 12:40:30 +00:00
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":
2023-03-12 03:56:01 +00:00
remove_permission_for_user(
str(from_id), Permission(message.parameter[1])
)
2022-05-23 12:40:30 +00:00
return await message.edit(lang("sudo_user_del_per"))
else:
2023-03-12 03:56:01 +00:00
return await edit_delete(message, lang("arg_error"))
2022-05-23 12:40:30 +00:00
if len(message.parameter) == 3:
if message.parameter[0] == "gaddp":
2023-03-12 03:56:01 +00:00
add_permission_for_group(
message.parameter[1], Permission(message.parameter[2])
)
2022-05-23 12:40:30 +00:00
return await message.edit(lang("sudo_group_add_per"))
elif message.parameter[0] == "gdelp":
2023-03-12 03:56:01 +00:00
remove_permission_for_group(
message.parameter[1], Permission(message.parameter[2])
)
2022-05-23 12:40:30 +00:00
return await message.edit(lang("sudo_group_del_per"))
else:
2023-03-12 03:56:01 +00:00
return await edit_delete(message, lang("arg_error"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await edit_delete(message, lang("arg_error"))
2022-05-23 12:40:30 +00:00
else:
2023-03-12 03:56:01 +00:00
await edit_delete(message, lang("arg_error"))