magic-wand_4/bot_io_classes.py

130 lines
4.0 KiB
Python

# Bot I/O classes v1.03
# 15/11/2021
# https://t.me/ssleg © 2021
from datetime import datetime
# TODO Сделать нормальную инициализацию модуля
class TgBotUsers:
"""класс хранения пользователей бота"""
__slots__ = ['__users', '__timestamps', '__con', '__cursor']
def __init__(self, con, cursor):
self.__users = {}
self.__timestamps = []
self.__con = con
self.__cursor = cursor
self.__cursor.execute('select * from contacts')
for row in cursor.fetchall():
user_id = row[0]
f_name = row[1]
l_name = row[2]
user_acc = row[3]
index = len(self.__timestamps)
self.__users[user_id] = (index, f_name, l_name, user_acc)
self.__timestamps.append(datetime(year=2020, month=1, day=1))
def is_bot_user(self, user_id):
tmp = self.__users.get(user_id)
if tmp is not None:
return True
else:
return False
def new_user_store(self, userinfo):
user_id = userinfo.id
f_name = userinfo.first_name
l_name = userinfo.last_name
user_acc = userinfo.username
entry = (user_id, f_name, l_name, user_acc)
self.__cursor.execute('''insert into contacts
(user_id, first_name, last_name, account_name)
values (?,?,?,?)''', entry)
self.__con.commit()
timestamp = datetime.now()
index = len(self.__timestamps)
self.__users[user_id] = (index, f_name, l_name, user_acc)
self.__timestamps.append(timestamp)
def update_user_mess_timestamp(self, user_id):
timestamp = datetime.now()
user = self.__users.get(user_id)
if user is not None:
index = user[0]
self.__timestamps[index] = timestamp
def get_user_mess_timestamp(self, user_id):
user = self.__users.get(user_id)
if user is not None:
index = user[0]
return self.__timestamps[index]
else:
return datetime(year=2020, month=1, day=1)
def get_users_list(self):
excluded_users = []
now = datetime.now()
self.__cursor.execute('select user_id, ban_to_date from ban_list')
for row in self.__cursor.fetchall():
banned_to = datetime.strptime(row[1], '%Y-%m-%d %H:%M:%S')
delta = now - banned_to
if delta.total_seconds() < 0:
excluded_users.append(row[0])
user_list = []
for key in self.__users:
if key not in excluded_users:
user_list.append(key)
return user_list
class OutMessagesQueue:
"""класс очереди исходящих сообщений"""
__slots__ = ['__queue', '__position_i']
def __init__(self):
self.__queue = []
self.__position_i = 0
def queue_empty(self):
if len(self.__queue) == 0:
return True
return False
def is_user_in_queue(self, user_id):
i = 0
found_flag = False
while i < len(self.__queue):
if self.__queue[i][0] == user_id:
found_flag = True
break
i += 1
return found_flag, i
def add_message(self, user_id, message_text, file_name, buttons, contact_add):
found_flag, i = self.is_user_in_queue(user_id)
if not found_flag:
self.__queue.append([user_id])
self.__queue[i].append((user_id, message_text, file_name, buttons, contact_add))
def get_next_message(self):
element = self.__queue[self.__position_i][1]
return element
def set_sending_result(self, flag):
if flag:
user_queue = self.__queue[self.__position_i]
user_queue.pop(1)
if len(user_queue) == 1:
self.__queue.pop(self.__position_i)
else:
self.__position_i += 1
if self.__position_i >= len(self.__queue):
self.__position_i = 0