Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_URL="postgres://username:password@hostname/database"
DISCORD_TOKEN=abcd123
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
token.env
myenv
.env
61 changes: 25 additions & 36 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))