Compare commits
2 Commits
d3bf40a7a7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| dbd8934369 | |||
| 81aee4432f |
Binary file not shown.
34
src/main.py
34
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,9 +32,9 @@ 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,)
|
||||
@@ -39,8 +50,8 @@ class TwentyFourPlayer:
|
||||
self.rating = row[1]
|
||||
|
||||
def update_user(self):
|
||||
with sqlite3.connect("users.db") as conn:
|
||||
cur = conn.cursor()
|
||||
global usersdb
|
||||
cur = usersdb.cursor()
|
||||
cur.execute(
|
||||
'UPDATE users SET rating = ? WHERE username = ?',
|
||||
(self.rating, self.username)
|
||||
@@ -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,8 +128,7 @@ 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()
|
||||
cur = cardsdb.cursor()
|
||||
|
||||
for card in cards:
|
||||
cur.execute("SELECT * FROM cards WHERE LOWER(name)=LOWER(?)", (card,))
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user