Spaces:
Runtime error
Runtime error
File size: 3,588 Bytes
4338078 e16b26a 676dbeb 1ad73a2 6b7c0b1 e16b26a 676dbeb e16b26a c5430d2 a66d12a f55ed1f e16b26a f2707b8 107b66f 6b7c0b1 107b66f 6b7c0b1 107b66f 6b7c0b1 107b66f 6b7c0b1 56a1e0b 6b7c0b1 107b66f 6b7c0b1 107b66f 6b03c8c e16b26a 107b66f f2707b8 56a1e0b f2707b8 0d134d7 f2707b8 d6b6f58 107b66f 6b7c0b1 107b66f 6b7c0b1 676dbeb 99c2841 676dbeb 83989d2 676dbeb 83989d2 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
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) |