#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..
classDatabaseAPI:
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()
exceptsqlite3.Errorase:
raisee
returnself
def__exit__(self,exc_type,exc_val,exc_tb):
ifself.cursorisnotNone:
self.cursor.close()
ifself.connectionisnotNone:
self.connection.close()
defcreate_table(self,table_name,columns):
columns_str=", ".join(columns)
query=f"CREATE TABLE IF NOT EXISTS {table_name} ({columns_str})"
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()
ifguild_idisnotNone:
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()
defget_all_tables(self):
query="SELECT name FROM sqlite_master WHERE type='table'"
self.cursor.execute(query)
tables=self.cursor.fetchall()
table_names=[table[0]fortableintables]
returntable_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..
classAPI:
#Returns config.tomls content in a array.
@ -86,13 +151,15 @@ class API:
def__init__(self,config,bot,log,token):
def__init__(self,config,bot,log,token, dbapi):
self.start_time=time.time()
self.config=config
self.bot=bot
self.logger=log
self.token=token
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..
classCLI:
@ -213,7 +280,10 @@ async def stop(ctx):
if__name__=="__main__":
api=API(config,bot,log,token)
dbapi=DatabaseAPI("storage.db")
dbapi.__enter__()
api=API(config,bot,log,token,dbapi)
cli=CLI(api)
bot.api=api
@ -313,19 +383,18 @@ if __name__ == "__main__":
cli.register_command("unload",cli_unload,"Unloads a cog takes a cog name as a arg example: unload cogs.example")
#start Starts the bot itself.
defcli_start(self):
print("Starting the bot...")
#Get a table
defcli_table_get(self,table:str):
print(f"Getting {table} data...")
try:
self.api.bot.run(self.api.token)
table_data=self.api.db_api.get_table(table)
exceptExceptionase:
print(e)
print(f"Table {table} doesn't exist or it couldn't be reached. {e}")
return1
else:
print("Bot started.")
return0
#cli.register_command("start", cli_start, "Starts the bot.")
print(table_data)
cli.register_command("table_get",cli_table_get,"Gets a table by name from the storage.db file.")