import discord from discord import app_commands from fastapi import FastAPI import uvicorn import asyncio import os import aiohttp from datetime import datetime app = FastAPI() TOKEN = os.environ['TOKEN'] SERVER_ID = 1128366741995659296 CHANNEL_ID = 1296102118326931596 API_URL = "https://bloxfruitsapi.cloudflarescanner.workers.dev/" intents = discord.Intents.default() intents.message_content = True bot = discord.Client(intents=intents) tree = app_commands.CommandTree(bot) api_data = {} async def fetch_and_post(): channel = bot.get_channel(CHANNEL_ID) if not channel: return async with aiohttp.ClientSession() as session: try: async with session.get(API_URL) as response: if response.status == 200: data = await response.json() message = [] if "normal" in data and data["normal"]: message.append("Normal") message.extend(data["normal"]) message.append("") if "mirage" in data and data["mirage"]: message.append("Mirage") message.extend(data["mirage"]) formatted_message = "\n".join(message) await channel.send(formatted_message) except Exception as e: print(f"errer: {e}") async def schedule_api_updates(): while True: await fetch_and_post() await asyncio.sleep(7200) @app.get("/") async def read_root(): return api_data @bot.event async def on_ready(): await tree.sync() print(f"{bot.user} is now online!") asyncio.create_task(schedule_api_updates()) @tree.command(name="clear", description="clear api") async def clear_api(interaction: discord.Interaction): global api_data api_data.clear() await interaction.response.send_message("cleared") @tree.command(name="forceupdate", description="force stock/ update") async def force_update(interaction: discord.Interaction): await fetch_and_post() await interaction.response.send_message("completed") async def run_bot(): await bot.start(TOKEN) @app.on_event("startup") async def startup_event(): asyncio.create_task(run_bot()) if __name__ == "__main__": uvicorn.run("app:app", host="0.0.0.0", port=7860)