diff --git a/.gitignore b/.gitignore index 784853b..5e1d427 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .env *.db -__pycache__ \ No newline at end of file +__pycache__ diff --git a/src/__pycache__/twentyfour.cpython-314.pyc b/src/__pycache__/twentyfour.cpython-314.pyc deleted file mode 100644 index 6d5afe2..0000000 Binary files a/src/__pycache__/twentyfour.cpython-314.pyc and /dev/null differ diff --git a/src/main.py b/src/main.py index 6d14899..1352f82 100644 --- a/src/main.py +++ b/src/main.py @@ -11,6 +11,17 @@ import asyncio import re import sqlite3 +usersdb = sqlite3.connect("users.db") +cardsdb = sqlite3.connect("cards.db") + +def setupUsersDb(): + cur = usersdb.cursor() + cur.execute("DROP TABLE IF EXISTS users") + cur.execute("""CREATE TABLE IF NOT EXISTS users ( + username STRING PRIMARY KEY, + rating DECIMAL + )""") + # 24 class TwentyFourSubmission: def __init__(self, user: str, ast: tf.Node | None, result: int | float): @@ -21,30 +32,30 @@ class TwentyFourSubmission: class TwentyFourPlayer: def __init__(self, name: str): + global usersdb self.username: str = name - with sqlite3.connect("users.db") as conn: - cur = conn.cursor() + cur = usersdb.cursor() + cur.execute( + 'SELECT * FROM users WHERE username = ?', + (name,) + ) + row = cur.fetchone() + if row is None: + # Create user cur.execute( - 'SELECT * FROM users WHERE username = ?', - (name,) + 'INSERT INTO users (username, rating) VALUES (?, ?)', + (name, 1500.0) ) - row = cur.fetchone() - if row is None: - # Create user - cur.execute( - 'INSERT INTO users (username, rating) VALUES (?, ?)', - (name, 1500.0) - ) - row = [name, 1500.0] + row = [name, 1500.0] self.rating = row[1] def update_user(self): - with sqlite3.connect("users.db") as conn: - cur = conn.cursor() - cur.execute( - 'UPDATE users SET rating = ? WHERE username = ?', - (self.rating, self.username) - ) + global usersdb + cur = usersdb.cursor() + cur.execute( + 'UPDATE users SET rating = ? WHERE username = ?', + (self.rating, self.username) + ) class TwentyFourGame: def __init__(self, channel, running=False): @@ -82,6 +93,9 @@ game: TwentyFourGame = TwentyFourGame(0) # Setup load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') +ADMINS = os.getenv('BOT_ADMINS').split() + +setupUsersDb() intents = discord.Intents.default() intents.message_content = True @@ -98,6 +112,7 @@ async def on_ready(): @client.event async def on_message(message): global game + global cardsdb # Prevent infinite loops (even though it's fun) if message.author == client.user: @@ -113,15 +128,14 @@ async def on_message(message): cards = list(set(cards)) if len(cards) > 0: embed = discord.Embed() - with sqlite3.connect("cards.db") as conn: - cur = conn.cursor() - - for card in cards: - cur.execute("SELECT * FROM cards WHERE LOWER(name)=LOWER(?)", (card,)) - row = cur.fetchone() - - if row: - embed.add_field(name=row[1], value=f"Description: {row[2]}\nType: {row[3]}\nDLC: {row[4]}", inline=False) + cur = cardsdb.cursor() + + for card in cards: + cur.execute("SELECT * FROM cards WHERE LOWER(name)=LOWER(?)", (card,)) + row = cur.fetchone() + + if row: + embed.add_field(name=row[1], value=f"Description: {row[2]}\nType: {row[3]}\nDLC: {row[4]}", inline=False) if len(embed.fields) > 0: await channel.send(embed=embed) @@ -129,7 +143,7 @@ async def on_message(message): # Check for 24++ game if content == "!start": - if username == "citadel_941": + if username in ADMINS: game = TwentyFourGame(channel.id) asyncio.create_task(run_game(client, channel)) await channel.send("Starting game...") @@ -145,7 +159,7 @@ async def on_message(message): await channel.send(f"@{username}: You will be removed after this round") return elif content == "!stop": - if username == "citadel_941": + if username in ADMINS: game.stopping = True await channel.send("Stopping after this round") else: @@ -211,3 +225,7 @@ async def run_game(client, channel): if __name__ == "__main__": client.run(TOKEN) + + +usersdb.close() +cardsdb.close()