coollsd commited on
Commit
7d33bf6
·
verified ·
1 Parent(s): 47503ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -30
app.py CHANGED
@@ -1,35 +1,31 @@
 
1
  import discord
2
  from discord.ext import commands
3
- from fastapi import FastAPI
4
- import uvicorn
5
  import asyncio
6
- import requests
7
  import re
8
  import os
9
- from dotenv import load_dotenv
10
 
11
- # Load environment variables
12
- load_dotenv()
13
 
14
- app = FastAPI()
15
- intents = discord.Intents.default()
16
- bot = commands.Bot(command_prefix='!', intents=intents)
17
-
18
- # Get token from environment variable
19
- TOKEN = os.getenv('TOKEN')
20
  CHANNEL_ID = 1298830206387228682
21
 
22
  PETS_API = 'https://petsgo.biggamesapi.io/api/collection/Pets'
23
  EXISTS_API = 'https://existsapi.deno.dev/'
24
  RAP_API = 'https://petsgo.biggamesapi.io/api/rap'
25
 
 
 
 
26
  pet_images = {}
27
  pet_difficulties = {}
28
  pet_raps = {}
29
 
30
- @app.get("/")
31
- async def read_root():
32
- return {"Hello": "World"}
33
 
34
  async def update_rap_values():
35
  try:
@@ -70,10 +66,15 @@ async def get_huge_secret_pets():
70
  if 'thumbnail' in config_data:
71
  pet_images[pet_name] = config_data['thumbnail']
72
  pet_images[f"Shiny {pet_name}"] = config_data['thumbnail']
 
 
73
  return huge_secret_pets
 
 
 
74
  except Exception as e:
75
  print(f"Error fetching pets list: {e}")
76
- return []
77
 
78
  async def send_embed_message(channel, pet_name, previous_value, current_value, is_change=False):
79
  pet_image_url = pet_images.get(pet_name, None)
@@ -112,11 +113,13 @@ async def send_embed_message(channel, pet_name, previous_value, current_value, i
112
  asset_id = pet_image_url.replace('rbxassetid://', '')
113
  asset_id = re.search(r'^\d+', asset_id).group(0)
114
  pet_image_url = f"https://rbxgleaks.pythonanywhere.com/asset/{asset_id}"
 
115
  embed.set_thumbnail(url=pet_image_url)
116
 
117
  try:
118
  await channel.send(embed=embed)
119
  except discord.HTTPException as e:
 
120
  await channel.send(f"🎲 A {pet_name} was rolled! Exists: {current_display} (Previous: {previous_display})")
121
 
122
  async def petdata(tracked_pets):
@@ -132,8 +135,10 @@ async def petdata(tracked_pets):
132
  value = pet['value']
133
  pet_name = f"Shiny {pet_id}" if is_shiny else pet_id
134
  if pet_name in pet_values:
 
135
  pet_values[pet_name] = value
136
  return pet_values
 
137
  except Exception as e:
138
  print(f"Error: {e}")
139
  return None
@@ -146,6 +151,7 @@ async def main_loop():
146
 
147
  tracked_pets = await get_huge_secret_pets()
148
  lastknown = {pet: None for pet in tracked_pets}
 
149
 
150
  while True:
151
  if not tracked_pets:
@@ -153,8 +159,8 @@ async def main_loop():
153
  lastknown.update({pet: None for pet in tracked_pets if pet not in lastknown})
154
 
155
  await update_rap_values()
156
- vvalues = await petdata(tracked_pets)
157
 
 
158
  if vvalues is not None:
159
  for pet, value in vvalues.items():
160
  if lastknown[pet] is None:
@@ -162,10 +168,13 @@ async def main_loop():
162
  elif value != lastknown[pet]:
163
  await send_embed_message(channel, pet, lastknown[pet], value, is_change=True)
164
  lastknown[pet] = value
165
-
 
 
166
  if len(tracked_pets) > 0:
167
  new_pets = await get_huge_secret_pets()
168
  if set(new_pets) != set(tracked_pets):
 
169
  tracked_pets = new_pets
170
  lastknown.update({pet: None for pet in tracked_pets if pet not in lastknown})
171
 
@@ -176,15 +185,7 @@ async def on_ready():
176
  print(f'Logged in as {bot.user.name}')
177
  bot.loop.create_task(main_loop())
178
 
179
- async def run_bot():
180
- await bot.start(TOKEN)
181
-
182
- @app.on_event("startup")
183
- async def startup_event():
184
- asyncio.create_task(run_bot())
185
-
186
- if __name__ == "__main__":
187
- print("===== Application Startup =====")
188
- print(f"FastAPI running on http://0.0.0.0:7860")
189
- print("Discord bot starting...")
190
- uvicorn.run("main:app", host="0.0.0.0", port=7860)
 
1
+ import requests
2
  import discord
3
  from discord.ext import commands
 
 
4
  import asyncio
 
5
  import re
6
  import os
7
+ from flask import Flask
8
 
9
+ # Flask app setup
10
+ app = Flask(__name__)
11
 
12
+ TOKEN = os.environ['token']
 
 
 
 
 
13
  CHANNEL_ID = 1298830206387228682
14
 
15
  PETS_API = 'https://petsgo.biggamesapi.io/api/collection/Pets'
16
  EXISTS_API = 'https://existsapi.deno.dev/'
17
  RAP_API = 'https://petsgo.biggamesapi.io/api/rap'
18
 
19
+ intents = discord.Intents.default()
20
+ bot = commands.Bot(command_prefix='!', intents=intents)
21
+
22
  pet_images = {}
23
  pet_difficulties = {}
24
  pet_raps = {}
25
 
26
+ @app.route('/')
27
+ def home():
28
+ return 'Hello World'
29
 
30
  async def update_rap_values():
31
  try:
 
66
  if 'thumbnail' in config_data:
67
  pet_images[pet_name] = config_data['thumbnail']
68
  pet_images[f"Shiny {pet_name}"] = config_data['thumbnail']
69
+ print(f"Stored image URL for {pet_name}: {config_data['thumbnail']}")
70
+ print(f"Found {len(huge_secret_pets)} pets to track (including shiny versions)")
71
  return huge_secret_pets
72
+ else:
73
+ print("API response status not OK")
74
+ return []
75
  except Exception as e:
76
  print(f"Error fetching pets list: {e}")
77
+ return []
78
 
79
  async def send_embed_message(channel, pet_name, previous_value, current_value, is_change=False):
80
  pet_image_url = pet_images.get(pet_name, None)
 
113
  asset_id = pet_image_url.replace('rbxassetid://', '')
114
  asset_id = re.search(r'^\d+', asset_id).group(0)
115
  pet_image_url = f"https://rbxgleaks.pythonanywhere.com/asset/{asset_id}"
116
+ print(f"Using image URL for {pet_name}: {pet_image_url}")
117
  embed.set_thumbnail(url=pet_image_url)
118
 
119
  try:
120
  await channel.send(embed=embed)
121
  except discord.HTTPException as e:
122
+ print(f"Failed to send embed for {pet_name}: {e}")
123
  await channel.send(f"🎲 A {pet_name} was rolled! Exists: {current_display} (Previous: {previous_display})")
124
 
125
  async def petdata(tracked_pets):
 
135
  value = pet['value']
136
  pet_name = f"Shiny {pet_id}" if is_shiny else pet_id
137
  if pet_name in pet_values:
138
+ print(f"Found pet: {pet_name} with exist count {value}")
139
  pet_values[pet_name] = value
140
  return pet_values
141
+ print(f"Error code: {response.status_code}")
142
  except Exception as e:
143
  print(f"Error: {e}")
144
  return None
 
151
 
152
  tracked_pets = await get_huge_secret_pets()
153
  lastknown = {pet: None for pet in tracked_pets}
154
+ print(f"Initially tracking {len(tracked_pets)} pets")
155
 
156
  while True:
157
  if not tracked_pets:
 
159
  lastknown.update({pet: None for pet in tracked_pets if pet not in lastknown})
160
 
161
  await update_rap_values()
 
162
 
163
+ vvalues = await petdata(tracked_pets)
164
  if vvalues is not None:
165
  for pet, value in vvalues.items():
166
  if lastknown[pet] is None:
 
168
  elif value != lastknown[pet]:
169
  await send_embed_message(channel, pet, lastknown[pet], value, is_change=True)
170
  lastknown[pet] = value
171
+ else:
172
+ print("Broken check")
173
+
174
  if len(tracked_pets) > 0:
175
  new_pets = await get_huge_secret_pets()
176
  if set(new_pets) != set(tracked_pets):
177
+ print("Pet list updated!")
178
  tracked_pets = new_pets
179
  lastknown.update({pet: None for pet in tracked_pets if pet not in lastknown})
180
 
 
185
  print(f'Logged in as {bot.user.name}')
186
  bot.loop.create_task(main_loop())
187
 
188
+ if __name__ == '__main__':
189
+ import threading
190
+ threading.Thread(target=lambda: app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False)).start()
191
+ bot.run(TOKEN)