edditing code
This commit is contained in:
98
lib.py
98
lib.py
@@ -2,13 +2,12 @@ import json
|
||||
import logging
|
||||
import sqlite3
|
||||
from os import walk, listdir, path
|
||||
|
||||
DEFAULT_PREFIX = "$" # The prefix you want everyone to have if you don't define your own
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class DB:
|
||||
@staticmethod
|
||||
def prepare_db(guild: int):
|
||||
async def prepare_db(guild: int):
|
||||
try:
|
||||
connect = sqlite3.connect('user.db')
|
||||
cursor = connect.cursor()
|
||||
@@ -21,7 +20,7 @@ class DB:
|
||||
logging.error("failed to connect db", _error)
|
||||
|
||||
@staticmethod
|
||||
def work_with_db(db_func: str, data_turple: tuple):
|
||||
async def work_with_db(db_func: str, data_turple: tuple):
|
||||
"""
|
||||
Writing to db per server userinfo
|
||||
:param db_func:
|
||||
@@ -38,32 +37,81 @@ class DB:
|
||||
logging.error("failed to connect db", _error)
|
||||
|
||||
@staticmethod
|
||||
def fill_bd(name: str, userid: int, isbot: bool, nick: str, guild: int):
|
||||
async def fill_bd(name: str, userid: int, isbot: bool, nick: str, guild: int):
|
||||
sqlite_insert_with_param = (f"""INSERT OR IGNORE INTO "{guild}"
|
||||
(username, userid, nick, isbot)
|
||||
VALUES (?, ?, ?, ?)""")
|
||||
data_tuple: tuple[str, int, str, bool] = (name, userid, nick, isbot)
|
||||
DB.work_with_db(sqlite_insert_with_param, data_tuple)
|
||||
await DB.work_with_db(sqlite_insert_with_param, data_tuple)
|
||||
|
||||
@staticmethod
|
||||
def add_audio(guild: int, user: int, audio: str):
|
||||
sql_update_query = f"""UPDATE "{guild}" set track = ? where userid = ?"""
|
||||
async def add_audio(guild: int, user: int, audio: str, track: str = 'usertracks'):
|
||||
"""
|
||||
Adding audio into folder and DB
|
||||
|
||||
:param guild: Guild id
|
||||
:param user:
|
||||
:param audio:
|
||||
:param track: usertracks or defaulttracks
|
||||
"""
|
||||
# audio = f'{DB.read_db(guild, user, track)}, {audio}'
|
||||
|
||||
logging.error(f"Guild id is: {guild}\n"
|
||||
f"\t\t\t\t\tUser id is: {user}\n"
|
||||
f"\t\t\t\t\taudio is: {audio}\n"
|
||||
f"\t\t\t\t\tcolumn is {track}\n"
|
||||
f"\t\t\t\t\t---------------")
|
||||
sql_update_query = f"""UPDATE "{guild}" set {track} = ? where userid = ?"""
|
||||
data_tuple = (audio, user)
|
||||
DB.work_with_db(sql_update_query, data_tuple)
|
||||
logging.info(f'Func add_audio:\n'
|
||||
f'\t\t\t\t\tQuery: {sql_update_query}\n'
|
||||
f'\t\t\t\t\tTurple: {data_tuple}\n'
|
||||
f'\t\t\t\t\tAudio: {audio}\n'
|
||||
f'\t\t\t\t\t-----------------')
|
||||
await DB.work_with_db(sql_update_query, data_tuple)
|
||||
|
||||
@staticmethod
|
||||
def read_db(guild: int, user: int):
|
||||
async def read_db(guild: int, user: int, column: str):
|
||||
_col_dict = {'userid': 0,
|
||||
'username': 1,
|
||||
'nick': 2,
|
||||
'isbot': 3,
|
||||
'defaulttracks': 4,
|
||||
'usertracks': 5}
|
||||
try:
|
||||
sql_con = sqlite3.connect("user.db")
|
||||
cursor = sql_con.cursor()
|
||||
sql_read = f"""SELECT * FROM "{guild}" where userid = {user}"""
|
||||
cursor.execute(sql_read)
|
||||
record = cursor.fetchone()
|
||||
return record[4]
|
||||
|
||||
logging.info(f'Func read_db:\n'
|
||||
f'\t\t\t\t\tTrack: {column}\n'
|
||||
f'\t\t\t\t\tRecord: {record}\n'
|
||||
f'\t\t\t\t\t-----------------')
|
||||
|
||||
return record[_col_dict[column]]
|
||||
|
||||
except sqlite3.Error as _error:
|
||||
logging.error("Failed to read sqlite table", _error)
|
||||
|
||||
@staticmethod
|
||||
async def check_exist_audio(ctx, guild: int, user: int, column: str, audio: str):
|
||||
_list_str = await DB.read_db(guild, user, column)
|
||||
print(type(_list_str))
|
||||
if _list_str is not None:
|
||||
_list = _list_str.split(',')
|
||||
if audio in _list:
|
||||
await ctx.reply("File in list")
|
||||
logging.info(f'File {audio} in list')
|
||||
else:
|
||||
logging.info(f'File {audio} is not in list')
|
||||
else:
|
||||
_list = 'None'
|
||||
logging.info(f'check_exist_audio\n'
|
||||
f'\t\t\t\t\t{_list}'
|
||||
f'\t\t\t\t\t{audio}')
|
||||
|
||||
|
||||
class CogsPrepare:
|
||||
"""
|
||||
@@ -79,7 +127,7 @@ class CogsPrepare:
|
||||
return cogs_list
|
||||
|
||||
@staticmethod
|
||||
def cogs_dict():
|
||||
async def cogs_dict():
|
||||
cog_dict = {}
|
||||
for _cog in CogsPrepare.cog_list():
|
||||
cog_dict.update({f'{_cog}': f'{_cog}'})
|
||||
@@ -109,7 +157,7 @@ class Commands:
|
||||
await Commands.write_json(inter.guild.id, "prefix", prefix)
|
||||
|
||||
@staticmethod
|
||||
def list_files(fold: str):
|
||||
async def list_files(fold: str):
|
||||
fl = []
|
||||
for filenames in walk(fold):
|
||||
fl.extend(filenames)
|
||||
@@ -142,9 +190,9 @@ class Commands:
|
||||
|
||||
@staticmethod
|
||||
async def write_json(guild: int, param_name: str, param: str or int):
|
||||
with open('prefix.json', 'r', encoding='utf-8') as _f:
|
||||
with open('prefix.json', 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
_json = json.load(_f)
|
||||
_json = json.load(f)
|
||||
except json.decoder.JSONDecodeError:
|
||||
_json = {}
|
||||
try:
|
||||
@@ -154,15 +202,15 @@ class Commands:
|
||||
_guild = _json[f'{guild}']
|
||||
_guild.update({f'{param_name}': f'{param}'})
|
||||
|
||||
with open('prefix.json', 'w', encoding='utf-8') as _f:
|
||||
json.dump(_json, _f, indent=4)
|
||||
with open('prefix.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(_json, f, indent=4)
|
||||
|
||||
@staticmethod
|
||||
def determine_prefix(bot, msg):
|
||||
async def determine_prefix(bot, msg):
|
||||
"""
|
||||
Determite perserver bot prefix
|
||||
:param bot:
|
||||
:param msg:
|
||||
:param bot: Disnake Bot object
|
||||
:param msg: Disnake msg object
|
||||
:return: prefix
|
||||
"""
|
||||
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
|
||||
@@ -173,11 +221,9 @@ class Commands:
|
||||
except:
|
||||
_json = {}
|
||||
|
||||
if msg.guild: # If the guild exists
|
||||
try:
|
||||
parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up
|
||||
except:
|
||||
parameter = DEFAULT_PREFIX
|
||||
try:
|
||||
parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up
|
||||
except:
|
||||
parameter = '$'
|
||||
|
||||
print(parameter)
|
||||
return parameter
|
||||
|
||||
Reference in New Issue
Block a user