Compare commits

...

2 Commits

3 changed files with 48 additions and 30 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
.env .env
*.db *.db
__pycache__ __pycache__

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,30 +32,30 @@ 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(
'SELECT * FROM users WHERE username = ?',
(name,)
)
row = cur.fetchone()
if row is None:
# Create user
cur.execute( cur.execute(
'SELECT * FROM users WHERE username = ?', 'INSERT INTO users (username, rating) VALUES (?, ?)',
(name,) (name, 1500.0)
) )
row = cur.fetchone() row = [name, 1500.0]
if row is None:
# Create user
cur.execute(
'INSERT INTO users (username, rating) VALUES (?, ?)',
(name, 1500.0)
)
row = [name, 1500.0]
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)
) )
class TwentyFourGame: class TwentyFourGame:
def __init__(self, channel, running=False): def __init__(self, channel, running=False):
@@ -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,15 +128,14 @@ 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,)) row = cur.fetchone()
row = cur.fetchone()
if row:
if row: embed.add_field(name=row[1], value=f"Description: {row[2]}\nType: {row[3]}\nDLC: {row[4]}", inline=False)
embed.add_field(name=row[1], value=f"Description: {row[2]}\nType: {row[3]}\nDLC: {row[4]}", inline=False)
if len(embed.fields) > 0: if len(embed.fields) > 0:
await channel.send(embed=embed) await channel.send(embed=embed)
@@ -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()