discord-bot / app.py
NameIsJACK's picture
Update app.py
b07b1c9 verified
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()
@app.on_event("startup")
async def startup_event():
asyncio.create_task(bot.start(DISCORD_BOT_TOKEN))
@app.on_event("shutdown")
async def shutdown_event():
await bot.close()
@bot.event
async def on_ready():
print(f"πŸ€– Logged in as {bot.user}")
@bot.event
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}")
@app.get("/")
async def root():
return {"message": "FastAPI is running with the Discord bot"}