Compare commits

...

3 Commits

Author SHA1 Message Date
supopur 9ed2c050b7 Removed sql stuff completly 2 years ago
supopur 9c38cf4923 Nasty punctuation marks 2 years ago
supopur 6d6c97fe40 Removed broken not needed module (readline) 2 years ago

@ -1,4 +1,4 @@
import discord, os, sys, toml, time, datetime, logging, threading, asyncio, readline, importlib, sqlite3 import discord, os, sys, toml, time, datetime, logging, threading, asyncio, importlib, sqlite3
from discord.ext import commands from discord.ext import commands
@ -54,72 +54,6 @@ bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
logger.debug("Loaded the client.") logger.debug("Loaded the client.")
class DatabaseAPI:
def __init__(self, db_file):
self.db_file = db_file
self.connection = None
self.cursor = None
def __enter__(self):
try:
self.connection = sqlite3.connect(self.db_file)
self.cursor = self.connection.cursor()
except sqlite3.Error as e:
raise e
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.cursor is not None:
self.cursor.close()
if self.connection is not None:
self.connection.close()
def create_table(self, table_name, columns):
columns_str = ", ".join(columns)
query = f"CREATE TABLE IF NOT EXISTS {table_name} ({columns_str})"
self.cursor.execute(query)
self.connection.commit()
def insert_values(self, table_name, values):
placeholders = ",".join(["?" for _ in range(len(values))])
query = f"INSERT INTO {table_name} VALUES ({placeholders})"
self.cursor.execute(query, values)
self.connection.commit()
def get_table(self, table_name):
query = f"SELECT * FROM {table_name}"
self.cursor.execute(query)
return self.cursor.fetchall()
def modify_value(self, table_name, column_name, new_value):
query = f"UPDATE {table_name} SET {column_name} = ?"
self.cursor.execute(query, (new_value,))
self.connection.commit()
def del_column(self, table_name, column_name):
query = f"ALTER TABLE {table_name} DROP COLUMN {column_name}"
self.cursor.execute(query)
self.connection.commit()
def plugin_register(self, plugin_name, guild_id=None):
general_table_query = f"CREATE TABLE IF NOT EXISTS {plugin_name}_general (key TEXT PRIMARY KEY, value TEXT)"
self.cursor.execute(general_table_query)
self.connection.commit()
if guild_id is not None:
guild_table_query = f"CREATE TABLE IF NOT EXISTS {plugin_name}_server_{guild_id} (key TEXT PRIMARY KEY, value TEXT)"
self.cursor.execute(guild_table_query)
self.connection.commit()
def get_all_tables(self):
query = "SELECT name FROM sqlite_master WHERE type='table'"
self.cursor.execute(query)
tables = self.cursor.fetchall()
table_names = [table[0] for table in tables]
return table_names
#Api used mainly for plugins so getting uptime, loading, unloading, reloading plugins, getting status of plugins, get plugin info, stopping the bot, getting bot name and tag, getting cpm/commands per minute, get if there is web plugin, if there is web plugin register api, get if plugin supports web and so on.. #Api used mainly for plugins so getting uptime, loading, unloading, reloading plugins, getting status of plugins, get plugin info, stopping the bot, getting bot name and tag, getting cpm/commands per minute, get if there is web plugin, if there is web plugin register api, get if plugin supports web and so on..
class API: class API:
@ -129,7 +63,7 @@ class API:
#Dumps all the toml data from self.config and stores it inside toml.config #Dumps all the toml data from self.config and stores it inside toml.config
def save_config(self): def save_config(self):
try: try:
with open("config.toml", w) as f: with open("config.toml", "w") as f:
self.log("inf", "Saving the configuration file...") self.log("inf", "Saving the configuration file...")
toml.dump(self.config, f) toml.dump(self.config, f)
self.log("inf", "Config was saved to config.toml") self.log("inf", "Config was saved to config.toml")
@ -158,7 +92,7 @@ class API:
self.logger = log self.logger = log
self.token = token self.token = token
self.guild_ids = config["bot"]["guild_ids"] self.guild_ids = config["bot"]["guild_ids"]
self.db_api = dbapi
#This is the most basic interface CLI wich is basically a comand line that appears when you launch the app it will have commands like stop, help, commands, cogs, load, unload and so on.. #This is the most basic interface CLI wich is basically a comand line that appears when you launch the app it will have commands like stop, help, commands, cogs, load, unload and so on..
@ -280,9 +214,7 @@ async def stop(ctx):
if __name__ == "__main__": if __name__ == "__main__":
dbapi = DatabaseAPI("storage.db") api = API(config, bot, log, token)
dbapi.__enter__()
api = API(config, bot, log, token, dbapi)
cli = CLI(api) cli = CLI(api)
bot.api = api bot.api = api
@ -383,19 +315,6 @@ if __name__ == "__main__":
cli.register_command("unload", cli_unload, "Unloads a cog takes a cog name as a arg example: unload cogs.example") cli.register_command("unload", cli_unload, "Unloads a cog takes a cog name as a arg example: unload cogs.example")
#Get a table
def cli_table_get(self, table: str):
print(f"Getting {table} data...")
try:
table_data = self.api.db_api.get_table(table)
except Exception as e:
print(f"Table {table} doesn't exist or it couldn't be reached. {e}")
return 1
print(table_data)
cli.register_command("table_get", cli_table_get, "Gets a table by name from the storage.db file.")
for x in config["bot"]["cogs"]: for x in config["bot"]["cogs"]:
log("inf", f"Loading {x}...") log("inf", f"Loading {x}...")

@ -2,4 +2,3 @@ py-cord
pytoml pytoml
flask flask
quart quart
readline

Loading…
Cancel
Save