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
import utils
2022-11-06 03:48:10 -05:00
from database import Member , Restriction
from database import MemberRoles
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
from utils import getCommandArgs , getArgument , checkArg , parse_duration , delete_substring_from_string
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
# Filters
# is_admin=True - Check admin permission, if user is admin, continue.
# replied=True - If message is answer, continue.
# accessed_roles - list roles.
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " ban " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . HELPER , MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def ban_user ( message : types . Message ) :
command = await getCommandArgs ( message )
reason = getArgument ( command . arguments )
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
# If can't descibe user data
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
# Ban user and save (bool)
2022-11-06 03:48:10 -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
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has banned [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) " , parse_mode = " Markdown " )
2022-08-07 04:45:35 -04:00
# Open restrict
2022-11-06 03:48:10 -05:00
Restriction . create (
from_user = from_user ,
to_user = to_user ,
action = " Ban user " ,
reason = reason ,
)
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " unban " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . HELPER , MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def unban_user ( message : types . Message ) :
command = await getCommandArgs ( 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
# If can't descibe user data
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
# Unban user and set status (bool)
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
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has unbanned [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) " , parse_mode = " Markdown " )
Member . create (
user_id = to_user . user_id ,
first_name = to_user . first_name ,
username = to_user . username
)
@dp.message_handler ( commands = [ " info " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . HELPER , MemberRoles . ADMIN ] )
async def info_user ( message : types . Message ) :
command = await getCommandArgs ( message )
to_user = command . to_user
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
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " kick " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . HELPER , MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def kick_user ( message : types . Message ) :
command = await getCommandArgs ( message )
arguments = command . arguments
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
reason = getArgument ( arguments )
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
2022-11-06 03:48:10 -05:00
status1 = await bot . kick_chat_member ( chat_id = message . chat . id , user_id = to_user . user_id , until_date = None )
status2 = await bot . unban_chat_member ( chat_id = message . chat . id , user_id = to_user . user_id )
if ( not status1 and status2 ) :
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has kicked [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) " , parse_mode = " Markdown " )
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
Restriction . create (
from_user = from_user ,
to_user = to_user ,
action = " Kick user " ,
reason = reason ,
)
2022-08-16 06:28:08 -04:00
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " mute " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def mute_user ( message : types . Message ) :
command = await getCommandArgs ( message )
arguments = command . arguments
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:!mute (@username|id) (duration) \n "
" Reply to a message or use with a username/id " )
2022-08-07 04:45:35 -04:00
)
return
2022-11-06 03:48:10 -05:00
duration_string = parse_duration ( arguments )
duration = None
reason = None
if ( duration_string ) :
duration = utils . parse_timedelta ( duration_string )
if ( not duration ) :
await message . answer ( f " Error: \" { duration } \" — неверный формат времени. Examles: 3ч, 5м, 4h30s. " )
return
reason = delete_substring_from_string ( " " . join ( arguments ) , duration_string )
if ( not duration_string ) :
duration_string = " forever "
if ( arguments ) :
reason = " " . join ( arguments )
2022-08-07 04:45:35 -04:00
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-08-07 04:45:35 -04:00
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has muted [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) for { duration_string } " , parse_mode = " Markdown " )
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
Restriction . create (
from_user = from_user ,
to_user = to_user ,
action = " Mute user " ,
reason = reason ,
)
2022-08-16 06:28:08 -04:00
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " unmute " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def umute_user ( message : types . Message ) :
# Get information
command = await getCommandArgs ( 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
# If can't
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. " )
)
return
# 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 (
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 " ]
)
# 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
)
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has unmuted [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) " , parse_mode = " Markdown " )
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " pin " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . HELPER , MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def pin_message ( message : types . Message ) :
await bot . pin_chat_message ( message . chat . id , message . reply_to_message . message_id )
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " readonly " , " ro " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def readonly_mode ( message : types . Message ) :
check = checkArg ( message . text )
2022-11-06 03:48:10 -05:00
if ( not check ) :
await message . answer ( " Usage:!ro on,enable,start/off,disable,off \n " )
2022-08-07 04:45:35 -04:00
return
# 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
if ( check ) :
chat_permissions = ChatPermissions (
can_send_messages = not check
)
else :
chat_permissions = ChatPermissions (
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 ' ]
)
status = await bot . set_chat_permissions ( chat_id = message . chat . id , permissions = chat_permissions )
if ( status ) :
await message . answer ( f " readonly - { check } " )
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " media " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN , MemberRoles . HELPER ] )
2022-08-07 04:45:35 -04:00
async def media_content ( message : types . Message ) :
check = checkArg ( message . text )
2022-11-06 03:48:10 -05:00
if ( not check ) :
await message . answer ( " Usage: !media on,enable,start/off,disable,off " )
2022-08-07 04:45:35 -04:00
return
# 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
chat_permissions = ChatPermissions (
can_send_messages = group_permissions [ ' can_send_messages ' ] ,
can_send_media_messages = check ,
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 ' ]
)
# Set chat pemissions and save results
status = await bot . set_chat_permissions ( chat_id = message . chat . id , permissions = chat_permissions )
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " media - { check } " )
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " stickers " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN , MemberRoles . HELPER ] )
2022-08-16 06:28:08 -04:00
async def send_stickers ( message : types . Message ) :
2022-08-07 04:45:35 -04:00
# Get arguments
check = checkArg ( message . text )
2022-11-06 03:48:10 -05:00
if ( not check ) :
await message . answer ( " Usage: !stickers on,enable,start/off,disable,off " )
2022-08-07 04:45:35 -04:00
return
# 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.
chat_permissions = ChatPermissions (
can_send_messages = group_permissions [ ' can_send_messages ' ] ,
can_send_media_messages = group_permissions [ ' can_send_media_messages ' ] ,
can_send_other_messages = check ,
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 ' ]
)
# Start and save to satus (bool)
status = await bot . set_chat_permissions ( chat_id = message . chat . id , permissions = chat_permissions )
if status :
2022-11-06 03:48:10 -05:00
await message . answer ( f " stickes - { check } " )
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " warn " , " w " ] , 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
command = await getCommandArgs ( message )
reason = getArgument ( command . arguments )
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
2022-11-06 03:48:10 -05:00
await message . answer ( f " [ { from_user . first_name } ](tg://user?id= { from_user . user_id } ) has warned [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) ( { to_user . warns } / { config . limit_of_warns } ) " , parse_mode = " Markdown " )
2022-08-07 04:45:35 -04:00
2022-11-06 03:48:10 -05:00
if ( to_user . warns == config . limit_of_warns ) :
await message . answer ( f " [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) has been banned! " , parse_mode = " Markdown " )
await bot . kick_chat_member ( chat_id = message . chat . id , user_id = to_user . user_id , until_date = None )
Restriction . create (
to_user = to_user ,
from_user = from_user ,
action = " Warn user " ,
reason = reason ,
)
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " reload " ] , commands_prefix = " ! " )
2022-08-07 04:45:35 -04:00
async def reload ( message : types . Message ) :
2022-11-06 03:48:10 -05:00
from load import tgc
if ( not Member . search ( Member . role , " owner " ) ) :
Member . create (
user_id = message . from_user . id ,
first_name = message . from_user . first_name ,
username = message . from_user . username ,
role = " owner " ,
)
# TODO: do this every 1 hours
members = await tgc . members_list ( config . group_id )
for member in members :
user = Member . search ( Member . user_id , member [ " id " ] )
if ( not user ) :
Member . create (
user_id = member [ " id " ] ,
first_name = member [ " first_name " ] ,
username = member [ " username " ] ,
)
else :
user . first_name = member [ " first_name " ]
user . username = member [ " username " ]
user . save ( )
2022-08-07 04:45:35 -04:00
group = await bot . get_chat ( message . chat . id )
group_permissions = dict ( group [ " permissions " ] )
2022-08-16 06:28:08 -04:00
2022-08-07 04:45:35 -04:00
for permission in group_permissions . keys ( ) :
2022-08-16 06:28:08 -04:00
config . group_permissions [ permission ] = group_permissions [ permission ]
2022-08-13 08:25:52 -04:00
2022-11-06 03:48:10 -05:00
await message . answer ( " Reloaded! " )
2022-08-07 04:45:35 -04:00
2022-08-16 06:28:08 -04:00
2022-11-06 03:48:10 -05:00
@dp.message_handler ( commands = [ " setrole " ] , commands_prefix = " ! " , available_roles = [ MemberRoles . ADMIN ] )
2022-08-07 04:45:35 -04:00
async def set_role ( message : types . Message ) :
command = await getCommandArgs ( message )
new_role = getArgument ( 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-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 ( )
await message . answer ( f " { new_role . capitalize ( ) } role set for [ { to_user . first_name } ](tg://user?id= { to_user . user_id } ) " ,
parse_mode = " Markdown " )