From 6b51801ed2542647db8a1e80c806a13579c1a058 Mon Sep 17 00:00:00 2001 From: ~Nebula~ Date: Sun, 31 Aug 2025 14:06:35 +0100 Subject: [PATCH 1/2] fix database and bot --- .gitignore | 3 +-- discord_bot.py | 61 +++++++++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 967d45f..4c49bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -token.env -myenv \ No newline at end of file +.env diff --git a/discord_bot.py b/discord_bot.py index 6637f88..e25b6a6 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -3,58 +3,47 @@ import discord from discord.ext import commands import psycopg2 -from psycopg2 import sql -from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT +conn = psycopg2.connect(os.getenv("DATABASE_URL")) -dotenv.load_dotenv() - -intents = discord.Intents.default() -intents.message_content = True -intents.members = True -intents.reactions = True +conn.set_session(autocommit=True) -bot = commands.Bot(command_prefix="!", intents=intents) - -def get_db_connection(): - try: - conn = psycopg2.connect( - host="localhost", - database="reaction_tracker", - user="postgres", - password="password" - ) - return conn - except psycopg2.Error as e: - print(f"Database connection error: {e}") - return None - -def initialize_db(): - conn = get_db_connection() +def run_migrations(conn): cur = conn.cursor() cur.execute(""" CREATE TABLE IF NOT EXISTS messages ( - id SERIAL PRIMARY KEY, - message_id BIGINT UNIQUE NOT NULL, + message_id BIGINT PRIMARY KEY, author_id BIGINT NOT NULL, channel_id BIGINT NOT NULL, guild_id BIGINT NOT NULL, - content TEXT, - created_at TIMESTAMP NOT NULL, - month_year VARCHAR(7) NOT NULL + created_at TIMESTAMP NOT NULL + ); """) - + cur.execute(""" CREATE TABLE IF NOT EXISTS reactions ( - id SERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, message_id BIGINT REFERENCES messages(message_id), emoji VARCHAR(255) NOT NULL, - count INTEGER DEFAULT 1, - users BIGINT[] DEFAULT ARRAY[]::BIGINT[], - UNIQUE(message_id, emoji) + PRIMARY KEY(user_id, message_id, emoji) + ); """) -bot.run(os.getenv("TOKEN")) + cur.close() +run_migrations(conn) + +dotenv.load_dotenv() + +intents = discord.Intents.default() +intents.message_content = True +intents.members = True +intents.reactions = True + +bot = commands.Bot(command_prefix="!", intents=intents) +@bot.hybrid_command() +async def ping(ctx): + await ctx.send("Pong!") +bot.run(os.getenv("DISCORD_TOKEN")) From ff7e1b90f65c82151ae20f85275f85747b753e2f Mon Sep 17 00:00:00 2001 From: ~Nebula~ Date: Sun, 31 Aug 2025 14:10:20 +0100 Subject: [PATCH 2/2] add example .env --- .env.example | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..4a78013 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DATABASE_URL="postgres://username:password@hostname/database" +DISCORD_TOKEN=abcd123