Disable checks for admins
This commit is contained in:
parent
eaf9d33b8d
commit
352b312afb
11
bot.py
11
bot.py
@ -17,7 +17,7 @@ from model import User, Message, MessageHide, UserBan, session
|
|||||||
from time import strftime
|
from time import strftime
|
||||||
import re
|
import re
|
||||||
import unidecode
|
import unidecode
|
||||||
|
from mwt import MWT
|
||||||
|
|
||||||
class TelegramMonitorBot:
|
class TelegramMonitorBot:
|
||||||
|
|
||||||
@ -55,6 +55,11 @@ class TelegramMonitorBot:
|
|||||||
self.ban_name_feedback_message = "You have been banned from this group for having a misleading username."
|
self.ban_name_feedback_message = "You have been banned from this group for having a misleading username."
|
||||||
self.hide_feedback_message = "Your last message was deleted for having prohibited content."
|
self.hide_feedback_message = "Your last message was deleted for having prohibited content."
|
||||||
|
|
||||||
|
@MWT(timeout=60*60)
|
||||||
|
def get_admin_ids(self, bot, chat_id):
|
||||||
|
"""Returns a list of admin IDs for a given chat. Results are cached for 1 hour."""
|
||||||
|
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
|
||||||
|
|
||||||
def ban_user(self, update):
|
def ban_user(self, update):
|
||||||
""" Ban user """
|
""" Ban user """
|
||||||
kick_success = update.message.chat.kick_member(update.message.from_user.id)
|
kick_success = update.message.chat.kick_member(update.message.from_user.id)
|
||||||
@ -209,10 +214,12 @@ class TelegramMonitorBot:
|
|||||||
update.message.text.encode('utf-8'))
|
update.message.text.encode('utf-8'))
|
||||||
)
|
)
|
||||||
|
|
||||||
if not update.message.from_user.id in self.safe_user_ids:
|
if update.message.from_user.id not in self.get_admin_ids(bot, update.message.chat_id):
|
||||||
# Security checks
|
# Security checks
|
||||||
self.security_check_username(bot, update)
|
self.security_check_username(bot, update)
|
||||||
self.security_check_message(bot, update)
|
self.security_check_message(bot, update)
|
||||||
|
else:
|
||||||
|
print("Skipping checks. User is admin: {}".format(user.id))
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error: {}".format(e))
|
print("Error: {}".format(e))
|
||||||
|
38
mwt.py
Normal file
38
mwt.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
class MWT(object):
|
||||||
|
"""Memoize With Timeout"""
|
||||||
|
_caches = {}
|
||||||
|
_timeouts = {}
|
||||||
|
|
||||||
|
def __init__(self,timeout=2):
|
||||||
|
self.timeout = timeout
|
||||||
|
|
||||||
|
def collect(self):
|
||||||
|
"""Clear cache of results which have timed out"""
|
||||||
|
for func in self._caches:
|
||||||
|
cache = {}
|
||||||
|
for key in self._caches[func]:
|
||||||
|
if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
|
||||||
|
cache[key] = self._caches[func][key]
|
||||||
|
self._caches[func] = cache
|
||||||
|
|
||||||
|
def __call__(self, f):
|
||||||
|
self.cache = self._caches[f] = {}
|
||||||
|
self._timeouts[f] = self.timeout
|
||||||
|
|
||||||
|
def func(*args, **kwargs):
|
||||||
|
kw = sorted(kwargs.items())
|
||||||
|
key = (args, tuple(kw))
|
||||||
|
try:
|
||||||
|
v = self.cache[key]
|
||||||
|
print("cache")
|
||||||
|
if (time.time() - v[1]) > self.timeout:
|
||||||
|
raise KeyError
|
||||||
|
except KeyError:
|
||||||
|
print("new")
|
||||||
|
v = self.cache[key] = f(*args,**kwargs),time.time()
|
||||||
|
return v[0]
|
||||||
|
func.func_name = f.__name__
|
||||||
|
|
||||||
|
return func
|
Loading…
Reference in New Issue
Block a user