limport discord from discord import app_commands from fastapi import FastAPI import uvicorn import asyncio import os import aiohttp app = FastAPI() TOKEN = os.environ['TOKEN'] SERVER_ID = 1128366741995659296 CHANNEL_ID = 1296102118326931596 API_URL = "https://fruitblox.deno.dev/" intents = discord.Intents.default() intents.message_content = True bot = discord.Client(intents=intents) tree = app_commands.CommandTree(bot) api_data = {} previous_data = None async def format_and_send_message(data, channel): message = [] if "normal" in data and data["normal"]: message.append("Normal") for fruit in data["normal"]: message.append(fruit) message.append("") if "mirage" in data and data["mirage"]: message.append("Mirage") for fruit in data["mirage"]: message.append(fruit) formatted_message = "\n".join(message) await channel.send(formatted_message) async def check_api_changes(): global previous_data 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: current_data = await response.json() if previous_data is None: # First run, post initial state await format_and_send_message(current_data, channel) elif current_data != previous_data: # Data has changed, post update await format_and_send_message(current_data, channel) previous_data = current_data except Exception as e: print(f"Error fetching API: {e}") async def schedule_api_checks(): while True: await check_api_changes() await asyncio.sleep(30) # Check every 30 seconds @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_checks()) @bot.event async def on_message(message): if message.author == bot.user or message.guild.id != SERVER_ID or message.channel.id != CHANNEL_ID: return content = message.content.strip().split('\n') current_category = None updated = False api_data.clear() for line in content: line = line.strip() if line.lower() in ["normal", "mirage"]: current_category = line.lower() if current_category not in api_data: api_data[current_category] = [] updated = True elif current_category and line: api_data[current_category].append(line) updated = True if updated: await message.channel.send("Posted in API") @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/check ") async def force_update(interaction: discord.Interaction): await check_api_changes() 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)