This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
moderator-bot/handlers/groups/simple_admin.py

377 lines
11 KiB
Python
Raw Normal View History

2022-11-06 03:48:10 -05:00
from load import bot, dp, types
2022-08-07 04:45:35 -04:00
from aiogram.types.chat_permissions import ChatPermissions
import config
2023-01-22 05:27:20 -05:00
from database import Restriction
2022-11-06 03:48:10 -05:00
from database import MemberRoles
2022-08-07 04:45:35 -04:00
2022-12-01 05:54:32 -05:00
from utils import get_command_args, get_argument, parse_timedelta_from_message
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["ban", "sban"],
commands_prefix="!",
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
)
2022-08-07 04:45:35 -04:00
async def ban_user(message: types.Message):
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
2022-11-28 14:27:41 -05:00
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"Usage: !ban (@username|id) reason=None\n"
"Reply to a message or use with a username")
2022-08-07 04:45:35 -04:00
)
return
2023-01-22 05:27:20 -05:00
status = await bot.kick_chat_member(
chat_id=message.chat.id,
user_id=to_user.user_id,
until_date=None
)
2022-08-07 04:45:35 -04:00
2022-11-28 14:27:41 -05:00
if status and (not command.is_silent):
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has banned "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-11-06 03:48:10 -05:00
Restriction.create(
from_user=from_user,
to_user=to_user,
2022-12-01 05:54:32 -05:00
text=message.text,
message_id=message.message_id
2022-11-06 03:48:10 -05:00
)
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["unban", "sunban"],
commands_prefix="!",
2023-01-22 05:27:20 -05:00
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
2022-12-01 05:54:32 -05:00
)
2022-08-07 04:45:35 -04:00
async def unban_user(message: types.Message):
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"Usage: !unban (@username|id) reason=None\n"
"Reply to a message or use with username/id")
2022-08-07 04:45:35 -04:00
)
return
2022-11-06 03:48:10 -05:00
status = await bot.unban_chat_member(chat_id=message.chat.id, user_id=to_user.user_id)
2022-08-07 04:45:35 -04:00
2022-11-28 14:27:41 -05:00
if status and (not command.is_silent):
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has unbanned "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-12-01 05:54:32 -05:00
Restriction.create(
from_user=from_user,
to_user=to_user,
text=message.text,
message_id=message.message_id
2022-11-06 03:48:10 -05:00
)
2023-01-22 05:27:20 -05:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["info"],
commands_prefix="!",
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
)
2022-11-06 03:48:10 -05:00
async def info_user(message: types.Message):
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
2023-01-22 05:27:20 -05:00
to_user = command.to_user
2022-11-06 03:48:10 -05:00
if (not to_user):
await message.answer((
"Usage: !info (@username|id)\n"
"Reply to a message or use with username/id")
)
return
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
await message.answer((
f"[{to_user.first_name}](tg://user?id={to_user.user_id}) ({to_user.role})\n"
f"Warns: {to_user.warns}/{config.limit_of_warns}"),
parse_mode="Markdown"
)
2022-08-16 06:28:08 -04:00
2023-01-22 05:27:20 -05:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["kick", "skick"],
commands_prefix="!",
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
)
async def kick_user(message: types.Message):
command = await get_command_args(message)
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"Usage: !kick (@username|id) reason=None\n"
"Reply to a message or use with a username/id")
2022-08-07 04:45:35 -04:00
)
return
2023-01-22 05:27:20 -05:00
status1 = await bot.kick_chat_member(
chat_id=message.chat.id,
user_id=to_user.user_id,
until_date=None
)
2022-11-06 03:48:10 -05:00
2023-01-22 05:27:20 -05:00
status2 = await bot.unban_chat_member(
chat_id=message.chat.id,
user_id=to_user.user_id
)
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
if (not status1 and status2) and (not command.is_silent):
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has kicked "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-11-06 03:48:10 -05:00
Restriction.create(
from_user=from_user,
to_user=to_user,
2022-12-01 05:54:32 -05:00
text=message.text,
message_id=message.message_id
2022-11-06 03:48:10 -05:00
)
2022-08-16 06:28:08 -04:00
2022-08-07 04:45:35 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["mute", "smute"],
commands_prefix="!",
available_roles=[MemberRoles.ADMIN]
)
2023-01-22 05:27:20 -05:00
async def mute_user(message: types.Message):
command = await get_command_args(message)
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
duration = parse_timedelta_from_message(message)
2022-12-01 05:54:32 -05:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"Usage:!mute (@username|id) (duration)\n"
"Reply to a message or use with a username/id")
2022-08-07 04:45:35 -04:00
)
return
permissions = ChatPermissions(can_send_messages=False)
status = await bot.restrict_chat_member(
chat_id=message.chat.id,
2022-11-06 03:48:10 -05:00
user_id=to_user.user_id,
until_date=duration,
2022-08-07 04:45:35 -04:00
permissions=permissions
)
2022-11-06 03:48:10 -05:00
2022-11-28 14:27:41 -05:00
if status and (not command.is_silent):
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has muted "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-11-06 03:48:10 -05:00
Restriction.create(
from_user=from_user,
to_user=to_user,
2022-12-01 05:54:32 -05:00
text=message.text,
message_id=message.message_id
2022-11-06 03:48:10 -05:00
)
2022-08-16 06:28:08 -04:00
2022-08-07 04:45:35 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
2023-01-22 05:27:20 -05:00
commands=["unmute", "sunmute"],
2022-12-01 05:54:32 -05:00
commands_prefix="!",
available_roles=[MemberRoles.ADMIN]
)
2022-08-07 04:45:35 -04:00
async def umute_user(message: types.Message):
# Get information
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-08-16 06:28:08 -04:00
"Usage:!unmute (@username|id) reason=None.\n"
2022-08-07 04:45:35 -04:00
"Reply to a message or use with a username/id.")
)
2023-01-22 05:27:20 -05:00
return
2022-08-07 04:45:35 -04:00
# Get chat permissions
2022-08-16 06:28:08 -04:00
group_permissions = config.group_permissions
2022-08-07 04:45:35 -04:00
# Set permissions
permissions = ChatPermissions(
2023-01-22 05:27:20 -05:00
can_send_messages=group_permissions["can_send_messages"],
can_send_media_messages=group_permissions["can_send_media_messages"],
can_send_polls=group_permissions["can_send_polls"],
can_send_other_messages=group_permissions["can_send_other_messages"],
can_add_web_page_previews=group_permissions["can_add_web_page_previews"],
can_change_info=group_permissions["can_change_info"],
can_invite_users=group_permissions["can_invite_users"],
can_pin_messages=group_permissions["can_pin_messages"]
2022-08-07 04:45:35 -04:00
)
# Restrict user and save
status = await bot.restrict_chat_member(
chat_id=message.chat.id,
2022-11-06 03:48:10 -05:00
user_id=to_user.user_id,
2022-08-07 04:45:35 -04:00
permissions=permissions
)
2022-11-28 14:27:41 -05:00
if status and (not command.is_silent):
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has unmuted "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-08-07 04:45:35 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["pin"],
commands_prefix="!",
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
)
async def pin_message(message: types.Message):
2022-08-07 04:45:35 -04:00
await bot.pin_chat_message(message.chat.id, message.reply_to_message.message_id)
2022-12-01 05:54:32 -05:00
@dp.message_handler(
2023-01-22 05:27:20 -05:00
commands=["readonly", "ro"],
2022-12-01 05:54:32 -05:00
commands_prefix="!",
available_roles=[MemberRoles.ADMIN]
)
async def readonly_mode(message: types.Message):
2022-08-16 06:28:08 -04:00
group_permissions = config.group_permissions
2022-11-27 14:38:22 -05:00
status = config.group_permissions['can_send_messages']
2022-11-28 14:27:41 -05:00
2022-11-27 14:38:22 -05:00
if (status):
2023-01-22 05:27:20 -05:00
await message.answer("🔕 Readonly mode enabled!")
chat_permissions = ChatPermissions(
can_send_messages=not status
2022-08-07 04:45:35 -04:00
)
else:
2022-11-27 14:38:22 -05:00
await message.answer("🔔 Readonly mode disabled!")
2022-08-07 04:45:35 -04:00
chat_permissions = ChatPermissions(
2023-01-22 05:27:20 -05:00
can_send_messages=group_permissions['can_send_messages'],
can_send_media_messages=group_permissions["can_send_media_messages"],
can_send_other_messages=group_permissions['can_send_other_messages'],
can_send_polls=group_permissions['can_send_polls'],
can_invite_users=group_permissions['can_invite_users'],
can_change_info=group_permissions['can_change_info'],
can_add_web_page_previews=group_permissions['can_add_web_page_previews'],
can_pin_messages=group_permissions['can_pin_messages']
2022-08-07 04:45:35 -04:00
)
2022-11-27 14:38:22 -05:00
config.group_permissions["can_send_messages"] = not status
2023-01-22 05:27:20 -05:00
await bot.set_chat_permissions(
chat_id=message.chat.id,
permissions=chat_permissions
)
2022-08-16 06:28:08 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
2023-01-22 05:27:20 -05:00
commands=["warn", "w"],
2022-12-01 05:54:32 -05:00
commands_prefix="!",
available_roles=[MemberRoles.HELPER, MemberRoles.ADMIN]
)
2022-08-07 04:45:35 -04:00
async def warn_user(message: types.Message):
# Get information
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
if (not to_user) or (not from_user):
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"Usage: !warn (@username|id) reason=None\n"
"Reply to a message or use with username/id")
2022-08-07 04:45:35 -04:00
)
return
2022-11-06 03:48:10 -05:00
to_user.warns += 1
to_user.save()
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) has warned "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")
2022-11-06 03:48:10 -05:00
if (to_user.warns == config.limit_of_warns):
2023-01-22 05:27:20 -05:00
await message.answer((
f"[{from_user.first_name}](tg://user?id={from_user.user_id}) "
f"has banned {config.limit_of_warns}/{config.limit_of_warns} ⚠️ "
), parse_mode="Markdown")
await bot.kick_chat_member(
chat_id=message.chat.id,
user_id=to_user.user_id,
until_date=None
)
2022-11-06 03:48:10 -05:00
Restriction.create(
from_user=from_user,
2022-12-01 05:54:32 -05:00
to_user=to_user,
text=message.text,
message_id=message.message_id
2022-11-06 03:48:10 -05:00
)
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["reload"],
commands_prefix="!"
)
async def reload(message: types.Message):
2023-01-04 15:14:32 -05:00
from utils import reload_users_data
await reload_users_data()
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
await message.answer("Reloaded!")
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
2022-12-01 05:54:32 -05:00
@dp.message_handler(
commands=["setrole"],
commands_prefix="!",
available_roles=[MemberRoles.ADMIN]
)
2023-01-22 05:27:20 -05:00
async def set_role(message: types.Message):
2022-12-01 05:54:32 -05:00
command = await get_command_args(message)
new_role = get_argument(command.arguments)
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
to_user = command.to_user
from_user = command.from_user
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if (not to_user) or (not from_user) or (not new_role):
2022-08-16 06:28:08 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
"!setrole (@username|id) role(owner,admin,helper,member).\n"
2022-08-16 06:28:08 -04:00
"Reply to a message or use with username."
))
2022-08-07 04:45:35 -04:00
return
2022-12-01 05:54:32 -05:00
2022-08-16 06:28:08 -04:00
if not (new_role in [member.value for member in MemberRoles]):
2022-11-06 03:48:10 -05:00
await message.answer(f"Role {new_role} not exists")
2022-08-07 04:45:35 -04:00
return
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
if (from_user.user_id == to_user.user_id):
await message.answer("❌ You can't set role yourself")
2022-08-07 04:45:35 -04:00
return
2022-11-06 03:48:10 -05:00
to_user.role = new_role
to_user.save()
2022-12-01 05:54:32 -05:00
2023-01-22 05:27:20 -05:00
await message.answer((
f"{new_role.capitalize()} role set for "
f"[{to_user.first_name}](tg://user?id={to_user.user_id})"
), parse_mode="Markdown")