coollsd commited on
Commit
8323479
·
verified ·
1 Parent(s): 0e4fbb8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import discord
3
+ from discord.ext import commands
4
+ import asyncio
5
+ from fastapi import FastAPI
6
+ import uvicorn
7
+
8
+ app = FastAPI()
9
+
10
+ TOKEN = 'MTI5NTU4MzM4OTE5MzM0Mjk3OQ.GasbdG.j3XjTe8nsG4yvJphhBUWpnGiJ700kw7f17OK9s'
11
+ CHANNEL_ID = 1295583330951102494
12
+
13
+ api = 'https://ps99.biggamesapi.io/api/exists'
14
+ petst = [
15
+ 'Huge Dino Cat', 'Golden Huge Dino Cat', 'Rainbow Huge Dino Cat', 'Shiny Huge Dino Cat', 'Golden Shiny Huge Dino Cat', 'Rainbow Shiny Huge Dino Cat',
16
+ ]
17
+
18
+ pet_images = {
19
+ 'Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209703172313178/14976397288.png?ex=671174db&is=6710235b&hm=079d37b0492d2b6451572a4a6a7c8c3721666d08415bbe956c170024aeae3c98&=&format=webp&quality=lossless&width=224&height=224',
20
+ 'Golden Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209702220206080/14976397095.png?ex=671174db&is=6710235b&hm=43909eb7906ce13f1d385675021e7d5d0301cde26303a957a0a8a7066cf7cca3&=&format=webp&quality=lossless&width=224&height=224',
21
+ 'Rainbow Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209702912528485/rb_dino.png?ex=671174db&is=6710235b&hm=ad5c03bc4ce8a7a187c6e996f179e79d2b780f6e95e836bb91ac41b14510d836&=&format=webp&quality=lossless&width=224&height=224',
22
+ 'Shiny Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209702572523583/shiny_dino.png?ex=671174db&is=6710235b&hm=0ba086e9cc24dc7b46fac01495ca07f6b5df0b34b5399ae7b5c3e9142c9cddf1&=&format=webp&quality=lossless&width=224&height=224',
23
+ 'Golden Shiny Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209701956096121/golden_shiny_dino.png?ex=671174db&is=6710235b&hm=233838fe44924ae59b72c320a2086afbadfa38a84890289e574daca84e119bc5&=&format=webp&quality=lossless&width=224&height=224',
24
+ 'Rainbow Shiny Huge Dino Cat': 'https://media.discordapp.net/attachments/1295832789534507089/1296209701653975090/rb_shiny_din.png?ex=671174db&is=6710235b&hm=c81ecefdb06ddcf21c79493606bf37ad5bd4cba4c23ffa5f215f2c29aaec7293&=&format=webp&quality=lossless&width=224&height=224',
25
+ }
26
+
27
+ intents = discord.Intents.default()
28
+ bot = commands.Bot(command_prefix='!', intents=intents)
29
+
30
+ async def send_embed_message(channel, pet_name, previous_value, current_value, is_change=False):
31
+ pet_image_url = pet_images.get(pet_name, None)
32
+ embed = discord.Embed(
33
+ title=f"{'🚨 New Pet has been redeemed!' if is_change else 'Current Count'}",
34
+ description=f"{pet_name} has been redeemed with a serial of: **{global_serial_count}**",
35
+ color=discord.Color.blue() if not is_change else discord.Color.orange(),
36
+ )
37
+ if pet_image_url:
38
+ embed.set_thumbnail(url=pet_image_url)
39
+ await channel.send(embed=embed)
40
+
41
+ last_known_values = {
42
+ 'Huge Dino Cat': 1,
43
+ 'Golden Huge Dino Cat': 0,
44
+ 'Rainbow Huge Dino Cat': 0,
45
+ 'Shiny Huge Dino Cat': 0,
46
+ 'Golden Shiny Huge Dino Cat': 0,
47
+ 'Rainbow Shiny Huge Dino Cat': 0
48
+ }
49
+
50
+ global_serial_count = 1
51
+
52
+ async def petdata():
53
+ global global_serial_count
54
+ try:
55
+ response = requests.get(api)
56
+ if response.status_code == 200:
57
+ data = response.json()
58
+ if data['status'] == 'ok':
59
+ pet_values = {pet: 0 for pet in petst}
60
+
61
+ for pet in data['data']:
62
+ pet_id = pet['configData']['id']
63
+ is_shiny = pet['configData'].get('sh', False)
64
+ pet_type = pet['configData'].get('pt', None)
65
+ value = pet['value']
66
+
67
+ if pet_type == 1 and is_shiny:
68
+ pet_name = f"Golden Shiny {pet_id}"
69
+ elif pet_type == 2 and is_shiny:
70
+ pet_name = f"Rainbow Shiny {pet_id}"
71
+ elif pet_type == 1:
72
+ pet_name = f"Golden {pet_id}"
73
+ elif pet_type == 2:
74
+ pet_name = f"Rainbow {pet_id}"
75
+ elif pet_type is None:
76
+ if is_shiny:
77
+ pet_name = f"Shiny {pet_id}"
78
+ else:
79
+ pet_name = f"{pet_id}"
80
+
81
+ if pet_name in pet_values:
82
+ print(f"Found pet: {pet_name} with exist count {value}")
83
+
84
+ pet_values[pet_name] = value
85
+
86
+ if last_known_values[pet_name] != value:
87
+ global_serial_count += 1
88
+ serial_number = global_serial_count
89
+
90
+ print(f"New redemption detected for {pet_name}. Incremented serial to {serial_number}")
91
+
92
+ last_known_values[pet_name] = value
93
+
94
+ return pet_values
95
+ print(f"Error code: {response.status_code}")
96
+ except Exception as e:
97
+ print(f"Error: {e}")
98
+ return None
99
+
100
+ async def main_loop():
101
+ lastknown = {pet: None for pet in petst}
102
+ channel = bot.get_channel(CHANNEL_ID)
103
+ if channel is None:
104
+ print("Invalid channel ID. Please check your channel ID.")
105
+ return
106
+
107
+ while True:
108
+ vvalues = await petdata()
109
+ if vvalues is not None:
110
+ for pet, value in vvalues.items():
111
+ if lastknown[pet] is None:
112
+ print(pet, value)
113
+ elif value != lastknown[pet]:
114
+ await send_embed_message(channel, pet, lastknown[pet], value, is_change=True)
115
+ lastknown[pet] = value
116
+ else:
117
+ print("Broken check")
118
+ await asyncio.sleep(60)
119
+
120
+ @bot.event
121
+ async def on_ready():
122
+ print(f'Logged in as {bot.user.name}')
123
+ bot.loop.create_task(main_loop())
124
+
125
+ @app.get("/")
126
+ async def read_root():
127
+ return {"Hello": "World"}
128
+
129
+ async def run_bot():
130
+ await bot.start(TOKEN)
131
+
132
+ @app.on_event("startup")
133
+ async def startup_event():
134
+ asyncio.create_task(run_bot())
135
+
136
+ if __name__ == "__main__":
137
+ print("===== Application Startup =====")
138
+ print(f"FastAPI running on http://0.0.0.0:7860")
139
+ print("Discord bot starting...")
140
+ uvicorn.run("main:app", host="0.0.0.0", port=7860)