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 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"))