botbotbotbot / app.py
coollsd's picture
Update app.py
fd8f27d verified
raw
history blame
8.28 kB
import discord
from discord import app_commands
import aiohttp
import asyncio
from fastapi import FastAPI
import uvicorn
import random
import time
app = FastAPI()
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Client(intents=intents)
tree = app_commands.CommandTree(bot)
luck_multipliers = {}
luck_expiration = {}
luck_button_used = set()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@tree.command(name="petsimgo", description="get info on pet on petsgo u")
async def petsimgo(interaction: discord.Interaction, petname: str):
await interaction.response.defer()
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
return await response.json()
return None
exists_data = await fetch_data("https://petsgo.biggamesapi.io/api/exists")
rap_data = await fetch_data("https://petsgo.biggamesapi.io/api/Rap")
collection_data = await fetch_data("https://petsgo.biggamesapi.io/api/collection/Pets")
if not exists_data or not rap_data or not collection_data:
await interaction.followup.send("error")
return
pet_exists = next((pet for pet in exists_data['data'] if pet['configData']['id'].lower() == petname.lower()), None)
pet_rap = next((pet for pet in rap_data['data'] if pet['configData']['id'].lower() == petname.lower()), None)
pet_info = next((pet for pet in collection_data['data'] if pet['configName'].lower() == petname.lower()), None)
if not pet_exists or not pet_rap or not pet_info:
await interaction.followup.send(f"Pet '{petname}' not found.")
return
exists_value = pet_exists['value']
rap_value = pet_rap['value']
thumbnail_id = pet_info['configData']['thumbnail'].split('://')[1]
thumbnail_url = f"https://api.rbxgleaks1.workers.dev/asset/{thumbnail_id}"
def format_difficulty(difficulty):
if difficulty >= 1_000_000_000:
return f"{difficulty / 1_000_000_000:.1f}B ({difficulty:,})"
elif difficulty >= 1_000_000:
return f"{difficulty / 1_000_000:.1f}M ({difficulty:,})"
elif difficulty >= 1_000:
return f"{difficulty / 1_000:.1f}K ({difficulty:,})"
else:
return f"{difficulty} ({difficulty:,})"
embed = discord.Embed(title=f"PetsGo: {pet_info['configData']['name']}", color=0x787878)
embed.add_field(name="value", value=f"{rap_value:,} diamonds", inline=True)
embed.add_field(name="existing", value=f"{exists_value:,}", inline=True)
embed.add_field(name="difficulty", value=format_difficulty(pet_info['configData']['difficulty']), inline=True)
embed.add_field(name="category", value=pet_info['category'], inline=True)
embed.set_thumbnail(url=thumbnail_url)
embed.set_footer(text="hello everyone can i please get a burrito now")
await interaction.followup.send(embed=embed)
async def perform_roll(interaction: discord.Interaction):
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
return await response.json()
return None
rap_data = await fetch_data("https://petsgo.biggamesapi.io/api/Rap")
collection_data = await fetch_data("https://petsgo.biggamesapi.io/api/collection/Pets")
if not rap_data or not collection_data:
return None
pets = [pet for pet in collection_data['data'] if pet['configName'] in [p['configData']['id'] for p in rap_data['data']]]
if not pets:
return None
user_id = interaction.user.id
luck_multiplier = luck_multipliers.get(user_id, 1)
# Sort pets by difficulty in descending order
sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty'], reverse=True)
# Calculate the number of pets to consider based on luck
num_pets_to_consider = max(1, int(len(sorted_pets) * (luck_multiplier / 10)))
# Select a random pet from the top pets based on luck
rolled_pet = random.choice(sorted_pets[:num_pets_to_consider])
pet_rap = next((pet for pet in rap_data['data'] if pet['configData']['id'] == rolled_pet['configName']), None)
if not pet_rap:
return None
rap_value = pet_rap['value']
thumbnail_id = rolled_pet['configData']['thumbnail'].split('://')[1]
thumbnail_url = f"https://api.rbxgleaks1.workers.dev/asset/{thumbnail_id}"
def format_difficulty(difficulty):
if difficulty >= 1_000_000_000:
return f"{difficulty / 1_000_000_000:.1f}B ({difficulty:,})"
elif difficulty >= 1_000_000:
return f"{difficulty / 1_000_000:.1f}M ({difficulty:,})"
elif difficulty >= 1_000:
return f"{difficulty / 1_000:.1f}K ({difficulty:,})"
else:
return f"{difficulty} ({difficulty:,})"
embed = discord.Embed(title=f"{interaction.user.name} rolled: {rolled_pet['configData']['name']}", color=0x787878)
embed.add_field(name="value", value=f"{rap_value:,} diamonds", inline=True)
embed.add_field(name="difficulty", value=format_difficulty(rolled_pet['configData']['difficulty']), inline=True)
embed.add_field(name="category", value=rolled_pet['category'], inline=True)
embed.set_thumbnail(url=thumbnail_url)
luck_text = ""
if user_id in luck_expiration:
remaining_time = int(luck_expiration[user_id] - time.time())
if remaining_time > 0:
luck_text = f"\nYou have {remaining_time // 60} minutes and {remaining_time % 60} seconds of luck left!"
else:
del luck_multipliers[user_id]
del luck_expiration[user_id]
embed.set_footer(text=f"Click 'Roll Again' to roll again!{luck_text}")
roll_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again")
async def roll_again_callback(interaction: discord.Interaction):
await interaction.response.defer()
result = await perform_roll(interaction)
if result:
await interaction.followup.send(embed=result[0], view=result[1])
else:
await interaction.followup.send("errer.")
roll_again_button.callback = roll_again_callback
view = discord.ui.View()
view.add_item(roll_again_button)
if random.random() < 0.2 and user_id not in luck_button_used:
increase_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Increase Luck", custom_id="increase_luck")
async def increase_luck_callback(interaction: discord.Interaction):
if interaction.user.id != user_id:
await interaction.response.send_message("You cannot use this look!", ephemeral=True)
return
if user_id in luck_button_used:
await interaction.response.send_message("You already used your lick!", ephemeral=True)
return
luck_multipliers[user_id] = 10
luck_expiration[user_id] = time.time() + 1800 # 30 minutes
luck_button_used.add(user_id)
await interaction.response.send_message("Your luck has been increased for 30 minutes!", ephemeral=True)
increase_luck_button.callback = increase_luck_callback
view.add_item(increase_luck_button)
return embed, view
@tree.command(name="petroll", description="Roll for a random pet")
async def petroll(interaction: discord.Interaction):
await interaction.response.defer()
result = await perform_roll(interaction)
if result:
await interaction.followup.send(embed=result[0], view=result[1])
else:
await interaction.followup.send("errer")
@bot.event
async def on_ready():
await tree.sync()
print(f"{bot.user} is now online!")
async def run_bot():
await bot.start("MTI5MjkxMDYzMjg3MzQ5MjU4Mw.GbVmvy.8kEhPZyNLrACzBWYEorT7UqNRME7gp6Lvz6lg8")
@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)