Spaces:
Building
Building
import discord | |
from discord import app_commands | |
import aiohttp | |
import random | |
import time | |
import asyncio | |
from cash import user_cash | |
luck_multipliers = {} | |
luck_expiration = {} | |
luck_opportunities = {} | |
used_luck_opportunities = set() | |
first_luck_claimed = set() | |
# Cache for pet data | |
pet_cache = None | |
pet_cache_expiry = 0 | |
CACHE_DURATION = 300 # 5 minutes | |
async def fetch_and_process_pets(): | |
global pet_cache, pet_cache_expiry | |
current_time = time.time() | |
if pet_cache and current_time < pet_cache_expiry: | |
return pet_cache | |
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, collection_data = await asyncio.gather( | |
fetch_data("https://petsgo.biggamesapi.io/api/Rap"), | |
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 | |
# Sort pets by difficulty (rarity) | |
sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty']) | |
# Divide pets into tiers | |
tier_size = len(sorted_pets) // 5 | |
tiers = [sorted_pets[i:i+tier_size] for i in range(0, len(sorted_pets), tier_size)] | |
pet_cache = { | |
'tiers': tiers, | |
'rap_data': rap_data | |
} | |
pet_cache_expiry = current_time + CACHE_DURATION | |
return pet_cache | |
async def perform_roll(interaction: discord.Interaction): | |
pet_data = await fetch_and_process_pets() | |
if not pet_data: | |
return None | |
tiers = pet_data['tiers'] | |
rap_data = pet_data['rap_data'] | |
user_id = interaction.user.id | |
luck_multiplier = luck_multipliers.get(user_id, 1) | |
# Adjust probabilities based on luck | |
tier_probabilities = [0.5, 0.25, 0.15, 0.07, 0.03] | |
luck_boost = (luck_multiplier - 1) * 0.1 # 10% boost per luck level | |
for i in range(len(tier_probabilities)): | |
if i < len(tier_probabilities) - 1: | |
tier_probabilities[i] -= luck_boost | |
tier_probabilities[i+1] += luck_boost | |
# Ensure probabilities are non-negative and sum to 1 | |
tier_probabilities = [max(0, p) for p in tier_probabilities] | |
total = sum(tier_probabilities) | |
tier_probabilities = [p / total for p in tier_probabilities] | |
# Select tier based on probabilities | |
selected_tier = random.choices(tiers, weights=tier_probabilities)[0] | |
# Select pet from the chosen tier | |
rolled_pet = random.choice(selected_tier) | |
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_percentage = (luck_multiplier - 1) * 10 | |
luck_text = f"\nYou have {remaining_time // 60} minutes and {remaining_time % 60} seconds of luck left! ({luck_percentage}% luck)" | |
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("An error occurred.") | |
roll_again_button.callback = roll_again_callback | |
view = discord.ui.View() | |
view.add_item(roll_again_button) | |
sell_button = discord.ui.Button(style=discord.ButtonStyle.success, label=f"Sell Pet for ${rap_value}", custom_id="sell_pet") | |
async def sell_pet_callback(interaction: discord.Interaction): | |
if interaction.user.id != user_id: | |
await interaction.response.send_message("You cannot sell this pet.", ephemeral=True) | |
return | |
sell_value = rap_value | |
user_cash[user_id] = user_cash.get(user_id, 0) + sell_value | |
await interaction.response.send_message(f"You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True) | |
for item in view.children: | |
if item.custom_id == "sell_pet": | |
view.remove_item(item) | |
break | |
await interaction.message.edit(view=view) | |
sell_button.callback = sell_pet_callback | |
view.add_item(sell_button) | |
if user_id not in first_luck_claimed: | |
first_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Claim 10% Luck Forever", custom_id="first_luck") | |
async def first_luck_callback(interaction: discord.Interaction): | |
if interaction.user.id != user_id: | |
await interaction.response.send_message("You cannot use this button.", ephemeral=True) | |
return | |
luck_multipliers[user_id] = 2 # 10% luck | |
first_luck_claimed.add(user_id) | |
await interaction.response.send_message("You've claimed 10% luck forever!", ephemeral=True) | |
for item in view.children: | |
if item.custom_id == "first_luck": | |
view.remove_item(item) | |
break | |
await interaction.message.edit(view=view) | |
first_luck_button.callback = first_luck_callback | |
view.add_item(first_luck_button) | |
elif random.random() < 0.6 and luck_opportunities.get(user_id, 0) < 4: | |
luck_opportunities[user_id] = luck_opportunities.get(user_id, 0) + 1 | |
increase_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Increase Luck", custom_id=f"increase_luck_{luck_opportunities[user_id]}") | |
async def increase_luck_callback(interaction: discord.Interaction): | |
if interaction.user.id != user_id: | |
await interaction.response.send_message("You cannot use this button.", ephemeral=True) | |
return | |
if user_id in used_luck_opportunities and len(used_luck_opportunities[user_id]) >= 4: | |
await interaction.response.send_message("You have already used all your luck opportunities.", ephemeral=True) | |
return | |
current_luck = luck_multipliers.get(user_id, 1) | |
new_luck = min(current_luck + 0.1, 2) # Max 20% luck | |
luck_multipliers[user_id] = new_luck | |
luck_expiration[user_id] = time.time() + 1800 | |
if user_id not in used_luck_opportunities: | |
used_luck_opportunities[user_id] = set() | |
used_luck_opportunities[user_id].add(luck_opportunities[user_id]) | |
luck_percentage = (new_luck - 1) * 100 | |
await interaction.response.send_message(f"Your luck has been increased to {luck_percentage:.1f}% for 30 minutes!", ephemeral=True) | |
for item in view.children: | |
if item.custom_id == interaction.custom_id: | |
view.remove_item(item) | |
break | |
await interaction.message.edit(view=view) | |
increase_luck_button.callback = increase_luck_callback | |
view.add_item(increase_luck_button) | |
return embed, view | |
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("An error occurred.") | |
async def balance(interaction: discord.Interaction): | |
user_id = interaction.user.id | |
current_balance = user_cash.get(user_id, 0) | |
await interaction.response.send_message(f"Your current balance is ${current_balance}.", ephemeral=True) | |
async def dice(interaction: discord.Interaction, bet: int): | |
await roll_dice(interaction, bet) | |
async def roll_dice(interaction: discord.Interaction, bet: int): | |
user_id = interaction.user.id | |
balance = user_cash.get(user_id, 0) | |
if bet <= 0: | |
await interaction.response.send_message("Bet Higher than 0 Idiot.") | |
return | |
if bet > balance: | |
await interaction.response.send_message(f"You don't have enough cash. Your current balance is ${balance:.2f}") | |
return | |
embed = discord.Embed(title="Dice Roll", description=f"{interaction.user.name} is betting ${bet:.2f}", color=0x787878) | |
embed.add_field(name="Current Balance", value=f"${balance:.2f}", inline=False) | |
roll_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll the Dice", custom_id="roll_dice") | |
async def roll_dice_callback(interaction: discord.Interaction): | |
nonlocal balance | |
result = random.choice(["win", "lose"]) | |
if result == "win": | |
winnings = bet | |
balance += winnings | |
result_text = f"You won ${winnings:.2f}!" | |
else: | |
balance -= bet | |
result_text = f"You lost ${bet:.2f}." | |
user_cash[user_id] = balance | |
embed.clear_fields() | |
embed.add_field(name="Result", value=result_text, inline=False) | |
embed.add_field(name="New Balance", value=f"${balance:.2f}", inline=False) | |
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): | |
if interaction.user.id == user_id: | |
await roll_dice(interaction, bet) | |
else: | |
await interaction.response.send_message("you cant roll this", ephemeral=True) | |
roll_again_button.callback = roll_again_callback | |
new_view = discord.ui.View() | |
new_view.add_item(roll_again_button) | |
await interaction.response.edit_message(embed=embed, view=new_view) | |
roll_button.callback = roll_dice_callback | |
view = discord.ui.View() | |
view.add_item(roll_button) | |
if interaction.response.is_done(): | |
await interaction.followup.send(embed=embed, view=view) | |
else: | |
await interaction.response.send_message(embed=embed, view=view) |