Untitled
unknown
plain_text
a year ago
1.9 kB
10
Indexable
import discord
from discord.ext import commands
from .DataBase import Database # Ensure the correct import path
from mysql.connector import Error
class NewGuild(commands.Cog):
def __init__(self, bot, db: Database):
self.bot = bot
self.db = db
@commands.Cog.listener()
async def on_guild_join(self, guild):
await self.bot.wait_until_ready() # Ensure the bot is ready before accessing DB
self.create_guild_db(guild.id)
def create_guild_db(self, guild_id):
connection = None
cursor = None
try:
create_table_query = f"""
CREATE TABLE IF NOT EXISTS G_{guild_id} (
User VARCHAR(255) NOT NULL UNIQUE PRIMARY KEY,
Warns BIGINT NULL,
Xp BIGINT NULL,
Level BIGINT NULL,
Admin_Power BIGINT NULL
);
"""
# Get the database connection
connection = self.db.get_connection()
if connection is None:
raise ConnectionError("Failed to obtain database connection.")
cursor = connection.cursor()
cursor.execute(create_table_query)
print(f"Table 'G_{guild_id}' created successfully or already exists.")
except ConnectionError as e:
print(f"Connection Error: {e}")
except Error as e:
print(f"Database Error: {e}")
except Exception as e:
print(f"Unexpected Error: {e}")
finally:
if cursor:
cursor.close()
if connection:
connection.close()
async def setup(bot):
db = Database(host='host', user='user', password='pass', database='DB')
await bot.add_cog(NewGuild(bot, db))Editor is loading...
Leave a Comment