Migrating to multi computer development

dev
supopur 2 years ago
parent 17c727d225
commit 697fa4f6ff

@ -1,4 +1,4 @@
import discord, os, sys, toml, time, datetime, logging, threading, asyncio, readline, importlib import discord, os, sys, toml, time, datetime, logging, threading, asyncio, readline, importlib, sqlite3
from discord.ext import commands from discord.ext import commands
@ -43,7 +43,7 @@ except:
if token == "": if token == "":
log("err", 'Error no token is present type: export MEETOO_TOKEN="[Your token here]" into your terminall NOW!') log("err", 'Error no token is present type: export MEETOO_TOKEN="[Your token here]" into your terminall NOW!')
log("dbg", f"Token is: {token}")
start_time = time.time() start_time = time.time()
@ -54,8 +54,73 @@ bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
logger.debug("Loaded the client.") logger.debug("Loaded the client.")
#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 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..
class API: class API:
#Returns config.tomls content in a array. #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.start_time = time.time()
self.config = config self.config = config
self.bot = bot self.bot = bot
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..
class CLI: class CLI:
@ -213,7 +280,10 @@ async def stop(ctx):
if __name__ == "__main__": 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) cli = CLI(api)
bot.api = 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") 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. #Get a table
def cli_start(self): def cli_table_get(self, table: str):
print("Starting the bot...") print(f"Getting {table} data...")
try: try:
self.api.bot.run(self.api.token) table_data = self.api.db_api.get_table(table)
except Exception as e: except Exception as e:
print(e) print(f"Table {table} doesn't exist or it couldn't be reached. {e}")
return 1 return 1
else:
print("Bot started.")
return 0
#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.")
for x in config["bot"]["cogs"]: for x in config["bot"]["cogs"]:

Loading…
Cancel
Save