70 lines
2.9 KiB
Python
70 lines
2.9 KiB
Python
|
import sqlite3
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
class ALLusers:
|
|||
|
|
|||
|
def __init__(self, database):
|
|||
|
"""Подключаемся к БД и сохраняем курсор соединения"""
|
|||
|
self.connection = sqlite3.connect(database)
|
|||
|
self.cursor = self.connection.cursor()
|
|||
|
|
|||
|
|
|||
|
def search_player(self, user_id):
|
|||
|
"""Проверяем, есть ли уже юзер в базе"""
|
|||
|
with self.connection:
|
|||
|
result = self.cursor.execute('SELECT * FROM `allusers` WHERE `user_id` = ?', (user_id,)).fetchall()
|
|||
|
return bool(len(result))
|
|||
|
|
|||
|
|
|||
|
def add_player(self, user_id, online = True):
|
|||
|
"""Добавляем нового подписчика"""
|
|||
|
with self.connection:
|
|||
|
return self.cursor.execute("INSERT INTO `allusers` (`user_id`, 'online') VALUES(?,?)", (user_id,online))
|
|||
|
|
|||
|
|
|||
|
def online_spam(self, online = True):
|
|||
|
"""Получаем всех активных подписчиков бота с подпиской"""
|
|||
|
with self.connection:
|
|||
|
return self.cursor.execute("SELECT * FROM `allusers` WHERE `online` = ?", (online,)).fetchall()
|
|||
|
|
|||
|
|
|||
|
def update_online(self, user_id, online):
|
|||
|
"""Обновляем статус подписки пользователя"""
|
|||
|
with self.connection:
|
|||
|
return self.cursor.execute("UPDATE `allusers` SET `online` = ? WHERE `user_id` = ?", (online, user_id))
|
|||
|
|
|||
|
|
|||
|
def update_russia(self, user_id, russia):
|
|||
|
"""Обновляем статус рф или иностранец"""
|
|||
|
with self.connection:
|
|||
|
return self.cursor.execute("UPDATE `allusers` SET `russia` = ? WHERE `user_id` = ?", (russia, user_id))
|
|||
|
|
|||
|
|
|||
|
def check_russia(self, user_id) -> str:
|
|||
|
"""Получаем рф или иностранец"""
|
|||
|
with self.connection:
|
|||
|
result = self.cursor.execute('SELECT `russia` FROM `allusers` WHERE `user_id` = ?', (user_id,)).fetchall()
|
|||
|
temp_answer = result[0]
|
|||
|
return (temp_answer[0])
|
|||
|
|
|||
|
|
|||
|
def log_info(self):
|
|||
|
"""Подсчет пользователей"""
|
|||
|
with self.connection:
|
|||
|
result = self.cursor.execute("SELECT COUNT(DISTINCT id) FROM `allusers`").fetchall()
|
|||
|
result2 = self.cursor.execute("SELECT COUNT(DISTINCT id) FROM `allusers` WHERE `online` = 1").fetchall()
|
|||
|
result4 = self.cursor.execute("SELECT COUNT(DISTINCT id) FROM `allusers` WHERE `online` = 0").fetchall()
|
|||
|
how_id_users = result[0]
|
|||
|
how_online_users = result2[0]
|
|||
|
how_deactivate_users = result4[0]
|
|||
|
return (f"Всего пользователей зарегистрировано: {how_id_users[0]}\n"
|
|||
|
f"Активных аккунтов: {how_online_users[0]}\n"
|
|||
|
f"Деактивировано аккаунтов: {how_deactivate_users[0]}\n")
|
|||
|
|
|||
|
|
|||
|
def close(self):
|
|||
|
"""Закрываем соединение с БД"""
|
|||
|
self.connection.close()
|