Upload files to "/"

This commit is contained in:
2025-08-15 08:29:56 -04:00
commit f710459edd
5 changed files with 511 additions and 0 deletions

100
bot.py Normal file
View File

@@ -0,0 +1,100 @@
import discord
from discord.ext import commands
import aiohttp
import asyncio
API_BASE_URL = "http://127.0.0.1:8000" # Change if API is remote
# Add the privileged message_content intent here
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
user_sessions = {} # Track user's active session_id here
@bot.event
async def on_ready():
print(f"Bot connected as {bot.user}")
@bot.command(name="start")
async def start_session(ctx):
username = str(ctx.author)
async with aiohttp.ClientSession() as session:
payload = {"username": username}
async with session.post(f"{API_BASE_URL}/session/start", json=payload) as resp:
if resp.status == 200:
data = await resp.json()
user_sessions[username] = data["session_id"]
await ctx.send(f"Session started for **{username}**. Session ID: {data['session_id']}")
else:
await ctx.send(f"Failed to start session. Status code: {resp.status}")
@bot.command(name="stop")
async def stop_session(ctx):
username = str(ctx.author)
session_id = user_sessions.get(username)
if not session_id:
await ctx.send("You don't have an active session. Use `!start` to begin one.")
return
async with aiohttp.ClientSession() as session:
async with session.post(f"{API_BASE_URL}/session/{session_id}/stop") as resp:
if resp.status == 200:
data = await resp.json()
user_sessions.pop(username, None)
await ctx.send(
f"Session stopped for **{username}**.\n"
f"Duration: {data['duration_seconds']} seconds\n"
f"Carbon Footprint: {data['carbon_footprint_kg']:.6f} kg CO2\n"
f"Entropy Score: {data['entropy_score']:.4f} bits"
)
else:
await ctx.send(f"Failed to stop session. Status code: {resp.status}")
@bot.command(name="leaderboard")
async def leaderboard(ctx):
async with aiohttp.ClientSession() as session:
async with session.get(f"{API_BASE_URL}/leaderboard") as resp:
if resp.status == 200:
data = await resp.json()
lifetime = data.get("top_lifetime_users", [])
trivia = data.get("top_trivia_users", [])
embed = discord.Embed(title="Cosmic Timer Leaderboards", color=0x00ff00)
if lifetime:
lifetime_text = "\n".join(
[f"#{i+1} **{u['username']}** — {u['lifetime_seconds']} sec" for i, u in enumerate(lifetime)]
)
embed.add_field(name="Top Lifetime Users", value=lifetime_text, inline=False)
if trivia:
trivia_text = "\n".join(
[f"#{i+1} **{u['username']}** — {u['trivia_points']} points" for i, u in enumerate(trivia)]
)
embed.add_field(name="Top Trivia Users", value=trivia_text, inline=False)
await ctx.send(embed=embed)
else:
await ctx.send(f"Failed to fetch leaderboard. Status code: {resp.status}")
@bot.command(name="stats")
async def user_stats(ctx, member: discord.Member = None):
member = member or ctx.author
username = str(member)
async with aiohttp.ClientSession() as session:
async with session.get(f"{API_BASE_URL}/user/{username}/stats") as resp:
if resp.status == 200:
data = await resp.json()
embed = discord.Embed(title=f"Stats for {username}", color=0x3498db)
embed.add_field(name="Lifetime Seconds", value=data["lifetime_seconds"])
embed.add_field(name="Trivia Points", value=data["trivia_points"])
embed.add_field(name="Carbon Footprint (kg CO2)", value=f"{data['carbon_footprint_kg']:.6f}")
embed.add_field(name="Entropy Score (bits)", value=f"{data['entropy_score']:.4f}")
await ctx.send(embed=embed)
else:
await ctx.send(f"User stats not found for {username}.")
if __name__ == "__main__":
TOKEN = "MTQwNDEzOTY0MTAxOTMwMjAyMQ.GyuOPC.eMCigYAsIZbziZmi-PDVNrzSoNZuCkoLuSkw0U"
bot.run(TOKEN)