coollsd commited on
Commit
6b7c0b1
1 Parent(s): a66d12a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -4,12 +4,15 @@ from fastapi import FastAPI
4
  import uvicorn
5
  import asyncio
6
  import os
 
 
7
 
8
  app = FastAPI()
9
 
10
  TOKEN = os.environ['TOKEN']
11
  SERVER_ID = 1128366741995659296
12
  CHANNEL_ID = 1296102118326931596
 
13
 
14
  intents = discord.Intents.default()
15
  intents.message_content = True
@@ -18,6 +21,39 @@ tree = app_commands.CommandTree(bot)
18
 
19
  api_data = {}
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @app.get("/")
22
  async def read_root():
23
  return api_data
@@ -26,32 +62,7 @@ async def read_root():
26
  async def on_ready():
27
  await tree.sync()
28
  print(f"{bot.user} is now online!")
29
-
30
- @bot.event
31
- async def on_message(message):
32
- if message.author == bot.user or message.guild.id != SERVER_ID or message.channel.id != CHANNEL_ID:
33
- return
34
-
35
- content = message.content.strip().split('\n')
36
- current_category = None
37
- updated = False
38
-
39
- # Clear existing data
40
- api_data.clear()
41
-
42
- for line in content:
43
- line = line.strip()
44
- if line.lower() in ["normal", "mirage"]:
45
- current_category = line.lower()
46
- if current_category not in api_data:
47
- api_data[current_category] = []
48
- updated = True
49
- elif current_category and line:
50
- api_data[current_category].append(line)
51
- updated = True
52
-
53
- if updated:
54
- await message.channel.send("Posted in API")
55
 
56
  @tree.command(name="clear", description="clear api")
57
  async def clear_api(interaction: discord.Interaction):
@@ -59,6 +70,11 @@ async def clear_api(interaction: discord.Interaction):
59
  api_data.clear()
60
  await interaction.response.send_message("cleared")
61
 
 
 
 
 
 
62
  async def run_bot():
63
  await bot.start(TOKEN)
64
 
 
4
  import uvicorn
5
  import asyncio
6
  import os
7
+ import aiohttp
8
+ from datetime import datetime
9
 
10
  app = FastAPI()
11
 
12
  TOKEN = os.environ['TOKEN']
13
  SERVER_ID = 1128366741995659296
14
  CHANNEL_ID = 1296102118326931596
15
+ API_URL = "https://bloxfruitsapi.cloudflarescanner.workers.dev/"
16
 
17
  intents = discord.Intents.default()
18
  intents.message_content = True
 
21
 
22
  api_data = {}
23
 
24
+ async def fetch_and_post():
25
+ channel = bot.get_channel(CHANNEL_ID)
26
+ if not channel:
27
+ return
28
+
29
+ async with aiohttp.ClientSession() as session:
30
+ try:
31
+ async with session.get(API_URL) as response:
32
+ if response.status == 200:
33
+ data = await response.json()
34
+
35
+ message = []
36
+
37
+ if "normal" in data and data["normal"]:
38
+ message.append("Normal")
39
+ message.extend(data["normal"])
40
+ message.append("")
41
+
42
+ if "mirage" in data and data["mirage"]:
43
+ message.append("Mirage")
44
+ message.extend(data["mirage"])
45
+
46
+ formatted_message = "\n".join(message)
47
+ await channel.send(formatted_message)
48
+
49
+ except Exception as e:
50
+ print(f"errer: {e}")
51
+
52
+ async def schedule_api_updates():
53
+ while True:
54
+ await fetch_and_post()
55
+ await asyncio.sleep(7200)
56
+
57
  @app.get("/")
58
  async def read_root():
59
  return api_data
 
62
  async def on_ready():
63
  await tree.sync()
64
  print(f"{bot.user} is now online!")
65
+ asyncio.create_task(schedule_api_updates())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  @tree.command(name="clear", description="clear api")
68
  async def clear_api(interaction: discord.Interaction):
 
70
  api_data.clear()
71
  await interaction.response.send_message("cleared")
72
 
73
+ @tree.command(name="forceupdate", description="force stock/ update")
74
+ async def force_update(interaction: discord.Interaction):
75
+ await fetch_and_post()
76
+ await interaction.response.send_message("completed")
77
+
78
  async def run_bot():
79
  await bot.start(TOKEN)
80