Spaces:
Sleeping
Sleeping
File size: 2,167 Bytes
23a6f06 414fd9d 23a6f06 414fd9d 23a6f06 b07b1c9 23a6f06 15007a8 b07b1c9 15007a8 23a6f06 bf4fb62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
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"} |