chore(userbot): apply ruff check --fix and ruff format
- ruff check --fix: 210 auto-fixed errors (import sorting, trailing whitespace, unused imports, f-string fixups, deprecated annotations) - ruff format: 104 files reformatted to consistent style - 268 non-auto-fixable issues remain (S113 requests timeout, etc.)
This commit is contained in:
104 files changed
+4338
-5319
No files matched your search
+48
-48
@@ -14,16 +14,18 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import re
|
||||
import json
|
||||
import threading
|
||||
import re
|
||||
import sqlite3
|
||||
from dns import resolver
|
||||
import threading
|
||||
|
||||
import pymongo
|
||||
from dns import resolver
|
||||
|
||||
from utils import config
|
||||
|
||||
resolver.default_resolver = resolver.Resolver(configure=False)
|
||||
resolver.default_resolver.nameservers = ["1.1.1.1"]
|
||||
resolver.default_resolver.nameservers = ['1.1.1.1']
|
||||
|
||||
|
||||
class Database:
|
||||
@@ -55,26 +57,24 @@ class MongoDatabase(Database):
|
||||
|
||||
def set(self, module: str, variable: str, value):
|
||||
if not isinstance(module, str) or not isinstance(variable, str):
|
||||
raise ValueError("Module and variable must be strings")
|
||||
self._database[module].replace_one(
|
||||
{"var": variable}, {"var": variable, "val": value}, upsert=True
|
||||
)
|
||||
raise ValueError('Module and variable must be strings')
|
||||
self._database[module].replace_one({'var': variable}, {'var': variable, 'val': value}, upsert=True)
|
||||
|
||||
def get(self, module: str, variable: str, default=None):
|
||||
if not isinstance(module, str) or not isinstance(variable, str):
|
||||
raise ValueError("Module and variable must be strings")
|
||||
doc = self._database[module].find_one({"var": variable})
|
||||
return default if doc is None else doc["val"]
|
||||
raise ValueError('Module and variable must be strings')
|
||||
doc = self._database[module].find_one({'var': variable})
|
||||
return default if doc is None else doc['val']
|
||||
|
||||
def get_collection(self, module: str):
|
||||
if not isinstance(module, str):
|
||||
raise ValueError("Module must be a string")
|
||||
return {item["var"]: item["val"] for item in self._database[module].find()}
|
||||
raise ValueError('Module must be a string')
|
||||
return {item['var']: item['val'] for item in self._database[module].find()}
|
||||
|
||||
def remove(self, module: str, variable: str):
|
||||
if not isinstance(module, str) or not isinstance(variable, str):
|
||||
raise ValueError("Module and variable must be strings")
|
||||
self._database[module].delete_one({"var": variable})
|
||||
raise ValueError('Module and variable must be strings')
|
||||
self._database[module].delete_one({'var': variable})
|
||||
|
||||
def close(self):
|
||||
self._client.close()
|
||||
@@ -82,27 +82,27 @@ class MongoDatabase(Database):
|
||||
def add_chat_history(self, user_id, message):
|
||||
chat_history = self.get_chat_history(user_id, default=[])
|
||||
chat_history.append(message)
|
||||
self.set(f"core.cohere.user_{user_id}", "chat_history", chat_history)
|
||||
self.set(f'core.cohere.user_{user_id}', 'chat_history', chat_history)
|
||||
|
||||
def get_chat_history(self, user_id, default=None):
|
||||
if default is None:
|
||||
default = []
|
||||
return self.get(f"core.cohere.user_{user_id}", "chat_history", default=[])
|
||||
return self.get(f'core.cohere.user_{user_id}', 'chat_history', default=[])
|
||||
|
||||
def addaiuser(self, user_id):
|
||||
chatai_users = self.get("core.chatbot", "chatai_users", default=[])
|
||||
chatai_users = self.get('core.chatbot', 'chatai_users', default=[])
|
||||
if user_id not in chatai_users:
|
||||
chatai_users.append(user_id)
|
||||
self.set("core.chatbot", "chatai_users", chatai_users)
|
||||
self.set('core.chatbot', 'chatai_users', chatai_users)
|
||||
|
||||
def remaiuser(self, user_id):
|
||||
chatai_users = self.get("core.chatbot", "chatai_users", default=[])
|
||||
chatai_users = self.get('core.chatbot', 'chatai_users', default=[])
|
||||
if user_id in chatai_users:
|
||||
chatai_users.remove(user_id)
|
||||
self.set("core.chatbot", "chatai_users", chatai_users)
|
||||
self.set('core.chatbot', 'chatai_users', chatai_users)
|
||||
|
||||
def getaiusers(self):
|
||||
return self.get("core.chatbot", "chatai_users", default=[])
|
||||
return self.get('core.chatbot', 'chatai_users', default=[])
|
||||
|
||||
|
||||
class SqliteDatabase(Database):
|
||||
@@ -114,25 +114,25 @@ class SqliteDatabase(Database):
|
||||
|
||||
@staticmethod
|
||||
def _parse_row(row: sqlite3.Row):
|
||||
if row["type"] == "bool":
|
||||
return row["val"] == "1"
|
||||
if row["type"] == "int":
|
||||
return int(row["val"])
|
||||
if row["type"] == "str":
|
||||
return row["val"]
|
||||
return json.loads(row["val"])
|
||||
if row['type'] == 'bool':
|
||||
return row['val'] == '1'
|
||||
if row['type'] == 'int':
|
||||
return int(row['val'])
|
||||
if row['type'] == 'str':
|
||||
return row['val']
|
||||
return json.loads(row['val'])
|
||||
|
||||
def _execute(self, module: str, *args, **kwargs) -> sqlite3.Cursor:
|
||||
pattern = r"^(core|custom)"
|
||||
pattern = r'^(core|custom)'
|
||||
if not re.match(pattern, module):
|
||||
raise ValueError(f"Invalid module name format: {module}")
|
||||
raise ValueError(f'Invalid module name format: {module}')
|
||||
|
||||
self._lock.acquire()
|
||||
try:
|
||||
cursor = self._conn.cursor()
|
||||
return cursor.execute(*args, **kwargs)
|
||||
except sqlite3.OperationalError as e:
|
||||
if str(e).startswith("no such table"):
|
||||
if str(e).startswith('no such table'):
|
||||
sql = f"""
|
||||
CREATE TABLE IF NOT EXISTS '{module}' (
|
||||
var TEXT UNIQUE NOT NULL,
|
||||
@@ -165,17 +165,17 @@ class SqliteDatabase(Database):
|
||||
"""
|
||||
|
||||
if isinstance(value, bool):
|
||||
val = "1" if value else "0"
|
||||
typ = "bool"
|
||||
val = '1' if value else '0'
|
||||
typ = 'bool'
|
||||
elif isinstance(value, str):
|
||||
val = value
|
||||
typ = "str"
|
||||
typ = 'str'
|
||||
elif isinstance(value, int):
|
||||
val = str(value)
|
||||
typ = "int"
|
||||
typ = 'int'
|
||||
else:
|
||||
val = json.dumps(value)
|
||||
typ = "json"
|
||||
typ = 'json'
|
||||
|
||||
self._execute(module, sql, (variable, val, typ, val, typ, variable))
|
||||
self._conn.commit()
|
||||
@@ -188,16 +188,16 @@ class SqliteDatabase(Database):
|
||||
self._conn.commit()
|
||||
|
||||
def get_collection(self, module: str) -> dict:
|
||||
pattern = r"^(core|custom)"
|
||||
pattern = r'^(core|custom)'
|
||||
if not re.match(pattern, module):
|
||||
raise ValueError(f"Invalid module name format: {module}")
|
||||
raise ValueError(f'Invalid module name format: {module}')
|
||||
|
||||
sql = f"SELECT * FROM '{module}'"
|
||||
cur = self._execute(module, sql)
|
||||
|
||||
collection = {}
|
||||
for row in cur:
|
||||
collection[row["var"]] = self._parse_row(row)
|
||||
collection[row['var']] = self._parse_row(row)
|
||||
|
||||
return collection
|
||||
|
||||
@@ -208,30 +208,30 @@ class SqliteDatabase(Database):
|
||||
def add_chat_history(self, user_id, message):
|
||||
chat_history = self.get_chat_history(user_id, default=[])
|
||||
chat_history.append(message)
|
||||
self.set(f"core.cohere.user_{user_id}", "chat_history", chat_history)
|
||||
self.set(f'core.cohere.user_{user_id}', 'chat_history', chat_history)
|
||||
|
||||
def get_chat_history(self, user_id, default=None):
|
||||
if default is None:
|
||||
default = []
|
||||
return self.get(f"core.cohere.user_{user_id}", "chat_history", default=[])
|
||||
return self.get(f'core.cohere.user_{user_id}', 'chat_history', default=[])
|
||||
|
||||
def addaiuser(self, user_id):
|
||||
chatai_users = self.get("core.chatbot", "chatai_users", default=[])
|
||||
chatai_users = self.get('core.chatbot', 'chatai_users', default=[])
|
||||
if user_id not in chatai_users:
|
||||
chatai_users.append(user_id)
|
||||
self.set("core.chatbot", "chatai_users", chatai_users)
|
||||
self.set('core.chatbot', 'chatai_users', chatai_users)
|
||||
|
||||
def remaiuser(self, user_id):
|
||||
chatai_users = self.get("core.chatbot", "chatai_users", default=[])
|
||||
chatai_users = self.get('core.chatbot', 'chatai_users', default=[])
|
||||
if user_id in chatai_users:
|
||||
chatai_users.remove(user_id)
|
||||
self.set("core.chatbot", "chatai_users", chatai_users)
|
||||
self.set('core.chatbot', 'chatai_users', chatai_users)
|
||||
|
||||
def getaiusers(self):
|
||||
return self.get("core.chatbot", "chatai_users", default=[])
|
||||
return self.get('core.chatbot', 'chatai_users', default=[])
|
||||
|
||||
|
||||
if config.db_type in ["mongo", "mongodb"]:
|
||||
if config.db_type in ['mongo', 'mongodb']:
|
||||
db = MongoDatabase(config.db_url, config.db_name)
|
||||
else:
|
||||
db = SqliteDatabase(config.db_name)
|
||||
Reference in new issue
Block a user