perefouras/main.py

73 lines
1.8 KiB
Python

# main.py
import discord
from discord import app_commands
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Discord client setup
intents = discord.Intents.default()
intents.members = True
intents.presences = True
intents.guilds = True
intents.messages = True
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@client.event
async def on_ready():
global client, tree
"""Initialize bot state and sync commands."""
# Import initialization functions
from fouras import load_riddles, _ensure_data_dir
from rhymes import load_rhymes, _ensure_db, _ensure_log_file
# Ensure directories exist
_ensure_data_dir()
_ensure_db()
_ensure_log_file()
# Load riddles and answers
load_riddles()
# Load rhymes
success, msg = load_rhymes()
print(msg)
# Sync commands
async for guild in client.fetch_guilds():
tree.copy_global_to(guild=guild)
await tree.sync(guild=guild)
print(f"Logged in as {client.user} on {len(client.guilds)} servers!")
@client.event
async def on_message(message):
global client, ongoing_riddles
"""Handle incoming messages."""
# Ignore bot's own messages
if message.author == client.user:
return
# Handle fouras module
from fouras import handle_message as handle_fouras
await handle_fouras(message, ongoing_riddles, client)
# Handle rhymes module
from rhymes import handle_message as handle_rhymes
await handle_rhymes(message, client)
# Run the client
if __name__ == "__main__":
token = os.getenv("DISCORD_TOKEN")
if not token:
print("Error: DISCORD_TOKEN not found in environment variables")
exit(1)
client.run(token)