@ -84,8 +84,8 @@ def get_admins(message):
if i . status == " creator " or i . can_restrict_members == True :
true_admins . append ( i . user . id )
return true_admins
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
return None
# Fix for anon admins, all anon (not premium) users == admins
@ -96,35 +96,30 @@ def is_anon(message):
else :
return False
# Return id from db/chat of user
def get_target ( message ) :
# try:
if True :
try :
global users
if len ( message . text . split ( ) ) > 1 and message . text . split ( ) [ 1 ] [ 0 ] == " @ " :
username = message . text . split ( ) [ 1 ] [ 1 : ]
read_users ( )
if sha ( username ) in users :
return users [ sha ( username ) ]
else :
print ( 1 )
return None
elif len ( message . text . split ( ) ) > 1 and message . text . split ( ) [ 2 ] [ 0 ] == " @ " :
username = message . text . split ( ) [ 2 ] [ 1 : ]
spl = message . text . split ( )
if len ( spl ) > 1 and ( spl [ 1 ] [ 0 ] == ' @ ' or spl [ 2 ] [ 0 ] == ' @ ' ) :
for i in spl :
if i [ 0 ] == ' @ ' :
username = i [ 1 : ]
break
read_users ( )
if sha ( username ) in users :
return users [ sha ( username ) ]
else :
print ( 2 )
return None
else :
target = message . reply_to_message . from_user . id
if target not in get_admins ( message ) :
return target
else :
print ( 3 )
return None
# except:
# return None
except :
return None
def get_name ( message ) :
try :
@ -138,8 +133,26 @@ def get_name(message):
# Reply to message
else :
return telebot . util . user_link ( message . reply_to_message . from_user )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
# Get time for '/mute'
# [time, time_in_sec, format]
def get_time ( message ) :
formats = { ' s ' : [ 1 , ' секунд(ы) ' ] , ' m ' : [ 60 , ' минут(ы) ' ] , ' h ' : [ 3600 , ' час(а ) ' ] , ' d ' : [ 86400 , ' день/дня ' ] }
text = message . text . split ( ) [ 1 : ] ; time = None
# Find format in text
for i in text :
if time :
break
for f in list ( formats . keys ( ) ) :
print ( i , ' ' , f )
if f in i :
time = [ i [ : - 1 ] , int ( i [ : - 1 ] ) * formats [ i [ - 1 ] ] [ 0 ] , formats [ i [ - 1 ] ] [ 1 ] ]
break
return time
def have_rights ( message , set_la = False ) :
global la ; read_la ( )
@ -220,22 +233,22 @@ def mute(message):
try :
if have_rights ( message ) :
target = get_target ( message )
print ( target )
time = get_time ( message )
if target :
if len ( message . text . split ( ) ) == 1 :
bot . restrict_chat_member ( message . chat . id , target , until_date = message . date )
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } был заглушен. """ , parse_mode = ' HTML ' )
elif len ( message . text . split ( ) ) == 2 and message . text . split ( ) [ 1 ] [ 0 ] == " @ " :
bot . restrict_chat_member ( message . chat . id , target , until_date = message . date )
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } был заглушен. """ )
if time :
bot . restrict_chat_member ( message . chat . id , target , until_date = message . date + time [ 1 ] )
answer = f " Пользователь { get_name ( message ) } был заглушён на { time [ 0 ] } { time [ 2 ] } . "
else :
time = int ( message . text . split ( ) [ 1 ] ) * 60
bot . restrict_chat_member ( message . chat . id , target , until_date = message . date + time )
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } был заглушён на { time / 60 } минут(ы). """ , parse_mode = ' HTML ' )
bot . restrict_chat_member ( message . chat . id , target , until_date = message . date )
answer = f " Пользователь { get_name ( message ) } был заглушен. "
try :
bot . reply_to ( message , answer , parse_mode = ' HTML ' )
except :
bot . reply_to ( message , answer )
else :
catch_error ( message , " no_user " )
except :
catch_error ( message )
catch_error ( message , ' None ' , ' no_user ' )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' unmute ' ] )
@ -248,11 +261,9 @@ def unmute(message):
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } снова имеет дар речи.
""" , parse_mode= ' HTML ' )
else :
catch_error ( message , " no_user " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
catch_error ( message , None , " no_user " )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' kick ' ] )
@ -267,11 +278,9 @@ def kick(message):
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } был исключён.
""" , parse_mode= ' HTML ' )
else :
catch_error ( message , " no_user " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
catch_error ( message , None , " no_user " )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' ban ' ] )
@ -284,11 +293,9 @@ def ban(message):
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } исключён и заблокирован.
""" , parse_mode= ' HTML ' )
else :
catch_error ( message , " no_user " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
catch_error ( message , None , " no_user " )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' unban ' ] )
def unban ( message ) :
@ -300,11 +307,9 @@ def unban(message):
bot . reply_to ( message , f """ Пользователь { get_name ( message ) } разблокирован.
""" , parse_mode= ' HTML ' )
else :
catch_error ( message , " no_user " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
catch_error ( message , None , " no_user " )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' setwelcome ' ] )
def setwelcome ( message ) :
@ -316,10 +321,8 @@ def setwelcome(message):
bot . reply_to ( message , f """ Установлено новое приветственное сообщение:
\n { db [ str ( message . chat . id ) ] } """ , parse_mode= ' HTML ' )
write_db ( )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' welcome ' ] )
def welcome ( message ) :
@ -328,8 +331,8 @@ def welcome(message):
read_db ( )
bot . reply_to ( message , f """ Приветственное сообщение:
\n { db [ str ( message . chat . id ) ] } """ , parse_mode= ' HTML ' )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' lock ' ] )
def lock ( message ) :
@ -339,8 +342,8 @@ def lock(message):
bot . reply_to ( message , " Чат был заблокирован 🔒 " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' unlock ' ] )
def unlock ( message ) :
@ -348,20 +351,16 @@ def unlock(message):
if have_rights ( message ) :
bot . set_chat_permissions ( message . chat . id , telebot . types . ChatPermissions ( can_send_messages = True , can_send_other_messages = True , can_send_polls = True ) )
bot . reply_to ( message , " Чат был разблокирован 🔓 " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' id ' ] )
def getid ( message ) :
try :
if have_rights ( message ) :
bot . reply_to ( message , " ID: " + telebot . formatting . hcode ( str ( get_target ( message ) ) ) , parse_mode = " HTML " )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' del ' ] )
@ -370,11 +369,10 @@ def delete(message):
if have_rights ( message ) :
bot . delete_message ( message . chat . id , message . reply_to_message . id )
bot . delete_message ( message . chat . id , message . id )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
### EXPEREMENTAL
@bot.message_handler ( commands = [ ' secret ' ] )
def secret ( message ) :
@ -382,10 +380,8 @@ def secret(message):
if have_rights ( message ) :
bot . send_message ( message . from_user . id , telebot . util . user_link ( message . reply_to_message . from_user ) )
bot . delete_message ( message . chat . id , message . id )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' html ' ] )
def html ( message ) :
@ -393,8 +389,10 @@ def html(message):
text = ' ' . join ( message . text . split ( ) [ 1 : ] )
bot . send_message ( message . chat . id , text , parse_mode = ' HTML ' )
bot . delete_message ( message . chat . id , message . id )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
###
@bot.message_handler ( commands = [ ' chatid ' ] )
def chatid ( message ) :
@ -403,8 +401,8 @@ def chatid(message):
bot . reply_to ( message , " Айди чата: " + telebot . formatting . hcode ( str ( message . chat . id ) ) , parse_mode = ' HTML ' )
else :
bot . reply_to ( message , " Увы, но у вас нету прав. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
############ LOW-ADMIN ##############
@ -417,8 +415,8 @@ def have_la(id):
la [ id ] = [ ]
write_la ( )
return True
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' la-list ' ] )
def la_list ( message ) :
@ -430,8 +428,8 @@ def la_list(message):
for i in la [ str ( message . chat . id ) ] :
s = s + ' \n @ ' + i
bot . reply_to ( message , s , parse_mode = ' HTML ' )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' la-add ' ] )
def la_add ( message ) :
@ -443,8 +441,8 @@ def la_add(message):
la [ message . chat . id ] . append ( nick )
write_la ( )
bot . reply_to ( message , f " Пользователь @ { nick } успешно добавлен в список администраторов. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' la-del ' ] )
def la_del ( message , set_la = True ) :
@ -458,8 +456,8 @@ def la_del(message, set_la=True):
del la [ message . chat . id ]
write_la ( )
bot . reply_to ( message , f " Пользователь @ { nick } был исключён из списка администраторов. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
#######################JOIN REQUEST #############
@ -470,8 +468,8 @@ def join_request(message: telebot.types.ChatJoinRequest):
try :
bot . send_message ( message . chat . id , f """ Поступила заявка на вступление от { telebot . util . user_link ( message . from_user ) }
Принять : { telebot . formatting . hcode ( f " /accept { message . from_user . id } " ) } """ , parse_mode= " HTML " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
@bot.message_handler ( commands = [ ' accept ' ] )
def accept_request ( message ) :
@ -480,18 +478,18 @@ def accept_request(message):
if len ( message . text . split ( ) ) == 2 :
bot . approve_chat_join_request ( message . chat . id , message . text . split ( ) [ 1 ] )
bot . reply_to ( message , " Заявка принята. " )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
######################SUPPORT########
@bot.message_handler ( commands = [ ' support ' ] )
def support ( message ) :
try :
bot . reply_to ( message , f """ Связь с аднимистратором в @just_anonchat_bot :
Адрес - { telebot . formatting . hcode ( " c :justuser" ) } """ , parse_mode= " HTML " )
except :
catch_error ( message )
bot . reply_to ( message , f """ Связь с аднимистратором в @just_anonchat_bot
Адрес - { telebot . formatting . hcode ( " :justuser" ) } """ , parse_mode= " HTML " )
except Exception as e :
catch_error ( message , e )
#####################WELCOME#####
@ -501,8 +499,8 @@ def handler_new_member(message):
global db
read_db ( )
bot . reply_to ( message , db [ str ( message . chat . id ) ] , parse_mode = ' HTML ' )
except :
catch_error ( message )
except Exception as e :
catch_error ( message , e )
##############ANALYTIC########
@ -528,17 +526,29 @@ global log_stream
log_stream = StringIO ( )
logging . basicConfig ( stream = log_stream )
def catch_error ( message , err_type = None ) :
# Known errors
known_errs = {
" A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: not enough rights to restrict/unrestrict chat member " : " Увы, но у бота не хватает прав для этого. "
}
# message error err_type ('no_user', ...)
def catch_error ( message , e , err_type = None ) :
if not err_type :
global log_stream
global log_stream , known_errs
e = str ( e )
logging . error ( traceback . format_exc ( ) ) # Log error
err = log_stream . getvalue ( ) # Error to variable
# Check error in known_errs
print ( e )
if e in known_errs :
bot . send_message ( message . chat . id , known_errs [ e ] )
else :
logging . error ( traceback . format_exc ( ) ) # Log error
err = log_stream . getvalue ( ) # Error to variable
bot . send_message ( message . chat . id , " Critical error (свяжитись через /support ) : \n \n " + telebot . formatting . hcode ( err ) , parse_mode = ' HTML ' )
bot . send_message ( message . chat . id , " Critical error (свяжитись через /support ) : \n \n " + telebot . formatting . hcode ( err ) , parse_mode = ' HTML ' )
log_stream . truncate ( 0 ) # Clear
log_stream . seek ( 0 ) # Clear
log_stream . truncate ( 0 ) # Clear
log_stream . seek ( 0 ) # Clear
elif err_type == " no_user " :
bot . send_message ( message . chat . id , " Н е указан пользователь." )
@ -552,6 +562,6 @@ while True:
exit ( )
except :
pass
'''
bot . polling ( allowed_updates = telebot . util . update_types )
'''
# '''
bot . polling ( )
# '''