Compare commits

...

2 Commits

3 changed files with 48 additions and 30 deletions

View File

@@ -11,6 +11,17 @@ import asyncio
import re import re
import sqlite3 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 # 24
class TwentyFourSubmission: class TwentyFourSubmission:
def __init__(self, user: str, ast: tf.Node | None, result: int | float): def __init__(self, user: str, ast: tf.Node | None, result: int | float):
@@ -21,9 +32,9 @@ class TwentyFourSubmission:
class TwentyFourPlayer: class TwentyFourPlayer:
def __init__(self, name: str): def __init__(self, name: str):
global usersdb
self.username: str = name self.username: str = name
with sqlite3.connect("users.db") as conn: cur = usersdb.cursor()
cur = conn.cursor()
cur.execute( cur.execute(
'SELECT * FROM users WHERE username = ?', 'SELECT * FROM users WHERE username = ?',
(name,) (name,)
@@ -39,8 +50,8 @@ class TwentyFourPlayer:
self.rating = row[1] self.rating = row[1]
def update_user(self): def update_user(self):
with sqlite3.connect("users.db") as conn: global usersdb
cur = conn.cursor() cur = usersdb.cursor()
cur.execute( cur.execute(
'UPDATE users SET rating = ? WHERE username = ?', 'UPDATE users SET rating = ? WHERE username = ?',
(self.rating, self.username) (self.rating, self.username)
@@ -82,6 +93,9 @@ game: TwentyFourGame = TwentyFourGame(0)
# Setup # Setup
load_dotenv() load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN') TOKEN = os.getenv('DISCORD_TOKEN')
ADMINS = os.getenv('BOT_ADMINS').split()
setupUsersDb()
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
@@ -98,6 +112,7 @@ async def on_ready():
@client.event @client.event
async def on_message(message): async def on_message(message):
global game global game
global cardsdb
# Prevent infinite loops (even though it's fun) # Prevent infinite loops (even though it's fun)
if message.author == client.user: if message.author == client.user:
@@ -113,8 +128,7 @@ async def on_message(message):
cards = list(set(cards)) cards = list(set(cards))
if len(cards) > 0: if len(cards) > 0:
embed = discord.Embed() embed = discord.Embed()
with sqlite3.connect("cards.db") as conn: cur = cardsdb.cursor()
cur = conn.cursor()
for card in cards: for card in cards:
cur.execute("SELECT * FROM cards WHERE LOWER(name)=LOWER(?)", (card,)) cur.execute("SELECT * FROM cards WHERE LOWER(name)=LOWER(?)", (card,))
@@ -129,7 +143,7 @@ async def on_message(message):
# Check for 24++ game # Check for 24++ game
if content == "!start": if content == "!start":
if username == "citadel_941": if username in ADMINS:
game = TwentyFourGame(channel.id) game = TwentyFourGame(channel.id)
asyncio.create_task(run_game(client, channel)) asyncio.create_task(run_game(client, channel))
await channel.send("Starting game...") 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") await channel.send(f"@{username}: You will be removed after this round")
return return
elif content == "!stop": elif content == "!stop":
if username == "citadel_941": if username in ADMINS:
game.stopping = True game.stopping = True
await channel.send("Stopping after this round") await channel.send("Stopping after this round")
else: else:
@@ -211,3 +225,7 @@ async def run_game(client, channel):
if __name__ == "__main__": if __name__ == "__main__":
client.run(TOKEN) client.run(TOKEN)
usersdb.close()
cardsdb.close()