Spaces:
Sleeping
Sleeping
import os | |
import asyncio | |
from fastapi import FastAPI | |
from discord.ext import commands | |
from dotenv import load_dotenv | |
import discord | |
import aiohttp | |
import io | |
load_dotenv() | |
from config import settings | |
WELCOME_CHANNEL_ID = settings.WELCOME_CHANNEL_ID | |
WELCOME_IMAGE_API = settings.WELCOME_IMAGE_API | |
DISCORD_BOT_TOKEN = settings.DISCORD_BOT_TOKEN | |
intents = discord.Intents.default() | |
intents.members = True | |
bot = commands.Bot(command_prefix="!", intents=intents) | |
app = FastAPI() | |
channel_id= 1377071313016852540 | |
app = FastAPI() | |
async def startup_event(): | |
asyncio.create_task(bot.start(DISCORD_BOT_TOKEN)) | |
async def shutdown_event(): | |
await bot.close() | |
async def on_ready(): | |
print(f"π€ Logged in as {bot.user}") | |
async def on_member_join(member): | |
username = member.name | |
avatar_url = member.display_avatar.replace(format="png", size=256).url | |
user_id = member.id | |
print(f"π€ New member: {username} ({user_id})") | |
try: | |
async with aiohttp.ClientSession() as session: | |
async with session.post(WELCOME_IMAGE_API, json={ | |
"username": username, | |
"avatar_url": avatar_url | |
}) as resp: | |
if resp.status != 200: | |
print("β Image generation failed") | |
return | |
image_data = await resp.read() | |
channel = bot.get_channel(WELCOME_CHANNEL_ID) | |
if channel is None: | |
print("β Channel not found") | |
return | |
file = discord.File(fp=io.BytesIO(image_data), filename=f"welcome-{username}.png") | |
await channel.send( | |
content=( | |
f"Explore, learn, and automate smarter β welcome to the AI Automation Club, <@{user_id}>!\nπ Check out <#{channel_id}> for the latest updates" | |
), | |
file=file | |
) | |
print(f"β Welcome message sent to channel for {username}") | |
except Exception as e: | |
print(f"β Error in welcome process: {e}") | |
async def root(): | |
return {"message": "FastAPI is running with the Discord bot"} |