coollsd commited on
Commit
b4a96b0
1 Parent(s): 19ba08a

Update petroll.py

Browse files
Files changed (1) hide show
  1. petroll.py +187 -118
petroll.py CHANGED
@@ -16,6 +16,98 @@ first_luck_claimed = set()
16
  auto_roll_users = set()
17
  auto_sell_users = set()
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  async def perform_roll(interaction: discord.Interaction):
20
  async def fetch_data(url):
21
  async with aiohttp.ClientSession() as session:
@@ -37,12 +129,12 @@ async def perform_roll(interaction: discord.Interaction):
37
 
38
  user_id = interaction.user.id
39
  luck_multiplier = luck_multipliers.get(user_id, 1)
40
-
41
  sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty'])
42
-
43
  max_index = len(sorted_pets) - 1
44
  index = int(max_index * (luck_multiplier - 1) / 9)
45
-
46
  rolled_pet = random.choice(sorted_pets[:index+1])
47
 
48
  pet_rap = next((pet for pet in rap_data['data'] if pet['configData']['id'] == rolled_pet['configName']), None)
@@ -82,47 +174,12 @@ async def perform_roll(interaction: discord.Interaction):
82
 
83
  embed.set_footer(text=f"Click 'Roll Again' to roll again!{luck_text}")
84
 
85
- # Create buttons
86
- view = discord.ui.View(timeout=None)
87
-
88
- # Roll Again Button
89
- roll_again_button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again")
90
-
91
- async def roll_again_callback(interaction: discord.Interaction):
92
- await interaction.response.defer()
93
- result = await perform_roll(interaction)
94
- if result:
95
- await interaction.followup.send(embed=result[0], view=result[1])
96
- else:
97
- await interaction.followup.send("An error occurred.")
98
-
99
- roll_again_button.callback = roll_again_callback
100
- view.add_item(roll_again_button)
101
-
102
- # Sell Button
103
- sell_button = discord.ui.Button(style=discord.ButtonStyle.success, label=f"Sell Pet for ${rap_value}", custom_id="sell_pet")
104
-
105
- async def sell_pet_callback(interaction: discord.Interaction):
106
- if interaction.user.id != user_id:
107
- await interaction.response.send_message("You cannot sell this pet.", ephemeral=True)
108
- return
109
-
110
- sell_value = rap_value
111
- user_cash[user_id] = user_cash.get(user_id, 0) + sell_value
112
- await interaction.response.send_message(f"You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True)
113
-
114
- # Remove the sell button after selling to prevent duplicate sales
115
- view.remove_item(sell_button)
116
- await interaction.message.edit(view=view)
117
 
118
- sell_button.callback = sell_pet_callback
119
-
120
- # Add sell button only if auto-sell is not enabled
121
- if user_id not in auto_sell_users:
122
- view.add_item(sell_button)
123
-
124
- # First Luck Button
125
  if user_id not in first_luck_claimed:
 
126
  first_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Claim 100% Luck Forever", custom_id="first_luck")
127
 
128
  async def first_luck_callback(interaction: discord.Interaction):
@@ -135,6 +192,7 @@ async def perform_roll(interaction: discord.Interaction):
135
 
136
  await interaction.response.send_message("You've claimed 100% luck forever!", ephemeral=True)
137
 
 
138
  view.remove_item(first_luck_button)
139
  await interaction.message.edit(view=view)
140
 
@@ -142,6 +200,7 @@ async def perform_roll(interaction: discord.Interaction):
142
  view.add_item(first_luck_button)
143
 
144
  elif random.random() < 0.6 or user_id not in luck_multipliers:
 
145
  luck_opportunities[user_id] = luck_opportunities.get(user_id, 0) + 1
146
  increase_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Increase Luck", custom_id=f"increase_luck_{luck_opportunities[user_id]}")
147
 
@@ -166,6 +225,7 @@ async def perform_roll(interaction: discord.Interaction):
166
  luck_percentage = (new_luck - 1) * 100
167
  await interaction.response.send_message(f"Your luck has been increased to {luck_percentage}% for 30 minutes!", ephemeral=True)
168
 
 
169
  view.remove_item(increase_luck_button)
170
  await interaction.message.edit(view=view)
171
 
@@ -175,76 +235,6 @@ async def perform_roll(interaction: discord.Interaction):
175
  increase_luck_button.callback = increase_luck_callback
176
  view.add_item(increase_luck_button)
177
 
178
- # Auto Roll Button
179
- auto_roll_button = discord.ui.Button(
180
- style=discord.ButtonStyle.primary if user_id not in auto_roll_users else discord.ButtonStyle.danger,
181
- label="Auto Roll" if user_id not in auto_roll_users else "Stop Auto Roll",
182
- custom_id="auto_roll"
183
- )
184
-
185
- async def auto_roll_callback(interaction: discord.Interaction):
186
- if interaction.user.id != user_id:
187
- await interaction.response.send_message("You cannot use this button.", ephemeral=True)
188
- return
189
-
190
- if user_id in auto_roll_users:
191
- auto_roll_users.remove(user_id)
192
- auto_roll_button.style = discord.ButtonStyle.primary
193
- auto_roll_button.label = "Auto Roll"
194
- await interaction.response.send_message("Auto roll stopped.", ephemeral=True)
195
- else:
196
- auto_roll_users.add(user_id)
197
- auto_roll_button.style = discord.ButtonStyle.danger
198
- auto_roll_button.label = "Stop Auto Roll"
199
- await interaction.response.send_message("Auto roll started. It will automatically stop after 3 minutes.", ephemeral=True)
200
- asyncio.create_task(auto_roll(interaction))
201
-
202
- await interaction.message.edit(view=view)
203
-
204
- auto_roll_button.callback = auto_roll_callback
205
- view.add_item(auto_roll_button)
206
-
207
- # Auto Sell Button
208
- auto_sell_button = discord.ui.Button(
209
- style=discord.ButtonStyle.primary if user_id not in auto_sell_users else discord.ButtonStyle.danger,
210
- label="Auto Pet Sell" if user_id not in auto_sell_users else "Stop Auto Pet Sell",
211
- custom_id="auto_pet_sell"
212
- )
213
-
214
- async def auto_sell_callback(interaction: discord.Interaction):
215
- if interaction.user.id != user_id:
216
- await interaction.response.send_message("You cannot use this button.", ephemeral=True)
217
- return
218
-
219
- if user_id in auto_sell_users:
220
- auto_sell_users.remove(user_id)
221
- auto_sell_button.style = discord.ButtonStyle.primary
222
- auto_sell_button.label = "Auto Pet Sell"
223
- await interaction.response.send_message("Auto pet sell stopped.", ephemeral=True)
224
- # Re-add the sell button only if it's not already removed
225
- if not any(item.custom_id == "sell_pet" for item in view.children):
226
- view.add_item(sell_button)
227
- else:
228
- auto_sell_users.add(user_id)
229
- auto_sell_button.style = discord.ButtonStyle.danger
230
- auto_sell_button.label = "Stop Auto Pet Sell"
231
- await interaction.response.send_message("Auto pet sell started. You cannot manually sell pets while auto-sell is enabled.", ephemeral=True)
232
- # Remove the sell button to prevent manual selling
233
- view.remove_item(sell_button)
234
-
235
- await interaction.message.edit(view=view)
236
-
237
- auto_sell_button.callback = auto_sell_callback
238
- view.add_item(auto_sell_button)
239
-
240
- # Handle Auto Sell: Automatically sell the pet if auto-sell is enabled
241
- if user_id in auto_sell_users:
242
- user_cash[user_id] = user_cash.get(user_id, 0) + rap_value
243
- embed.add_field(name="Auto Sell", value=f"Pet automatically sold for ${rap_value}. New balance: ${user_cash[user_id]}", inline=False)
244
- # Remove the sell button since it's auto-sold
245
- if any(item.custom_id == "sell_pet" for item in view.children):
246
- view.remove_item(sell_button)
247
-
248
  return embed, view
249
 
250
  async def schedule_next_luck_opportunity(interaction: discord.Interaction, user_id: int):
@@ -273,7 +263,7 @@ async def schedule_next_luck_opportunity(interaction: discord.Interaction, user_
273
  luck_percentage = (new_luck - 1) * 100
274
  await interaction.response.send_message(f"Your luck has been increased to {luck_percentage}% for 30 minutes!", ephemeral=True)
275
 
276
- # Remove the used button
277
  view = interaction.message.components[0]
278
  view.remove_item(increase_luck_button)
279
  await interaction.message.edit(view=view)
@@ -283,9 +273,68 @@ async def schedule_next_luck_opportunity(interaction: discord.Interaction, user_
283
 
284
  increase_luck_button.callback = increase_luck_callback
285
 
286
- view = interaction.message.components[0]
287
- view.add_item(increase_luck_button)
288
- await interaction.message.edit(view=view)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
  async def auto_roll(interaction: discord.Interaction):
291
  user_id = interaction.user.id
@@ -293,12 +342,32 @@ async def auto_roll(interaction: discord.Interaction):
293
  while user_id in auto_roll_users:
294
  if time.time() - start_time >= 180: # 3 minutes
295
  auto_roll_users.remove(user_id)
296
- await interaction.followup.send("auto roll stopped, to turn it on again, roll again then turn it on", ephemeral=True)
297
  break
298
 
299
  result = await perform_roll(interaction)
300
  if result:
301
- await interaction.followup.send(embed=result[0], view=result[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  else:
303
  await interaction.followup.send("An error occurred.")
304
  await asyncio.sleep(5) # Wait for 5 seconds between rolls
 
16
  auto_roll_users = set()
17
  auto_sell_users = set()
18
 
19
+ class PetRollView(discord.ui.View):
20
+ def __init__(self, rap_value, user_id):
21
+ super().__init__(timeout=None)
22
+ self.rap_value = rap_value
23
+ self.user_id = user_id
24
+ self.pet_sold = False
25
+ self.auto_sell = False
26
+
27
+ # Add buttons
28
+ self.add_item(discord.ui.Button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again"))
29
+ self.add_item(discord.ui.Button(style=discord.ButtonStyle.success, label=f"Sell Pet for ${self.rap_value}", custom_id="sell_pet"))
30
+ self.add_item(discord.ui.Button(style=discord.ButtonStyle.primary, label="Auto Pet Sell", custom_id="auto_pet_sell"))
31
+
32
+ async def interaction_check(self, interaction: discord.Interaction) -> bool:
33
+ if interaction.user.id != self.user_id:
34
+ await interaction.response.send_message("You cannot interact with these buttons.", ephemeral=True)
35
+ return False
36
+ return True
37
+
38
+ @discord.ui.button(style=discord.ButtonStyle.primary, label="Roll Again", custom_id="roll_again")
39
+ async def roll_again_button(self, button: discord.ui.Button, interaction: discord.Interaction):
40
+ await interaction.response.defer()
41
+ result = await perform_roll(interaction)
42
+ if result:
43
+ await interaction.followup.send(embed=result[0], view=result[1])
44
+ else:
45
+ await interaction.followup.send("An error occurred.")
46
+
47
+ @discord.ui.button(style=discord.ButtonStyle.success, label="Sell Pet", custom_id="sell_pet")
48
+ async def sell_pet_button(self, button: discord.ui.Button, interaction: discord.Interaction):
49
+ if self.pet_sold:
50
+ await interaction.response.send_message("This pet has already been sold.", ephemeral=True)
51
+ return
52
+
53
+ sell_value = self.rap_value
54
+ user_cash[self.user_id] = user_cash.get(self.user_id, 0) + sell_value
55
+ self.pet_sold = True
56
+ await interaction.response.send_message(f"You sold the pet for ${sell_value}. Your new balance is ${user_cash[self.user_id]}.", ephemeral=True)
57
+
58
+ # Remove the sell button to prevent duplicate sales
59
+ self.sell_pet_button.disabled = True
60
+ self.sell_pet_button.label = "Sold"
61
+ await interaction.message.edit(view=self)
62
+
63
+ # If auto-sell is enabled, ensure it's disabled since the pet is already sold
64
+ if self.auto_sell:
65
+ self.auto_sell = False
66
+ self.auto_pet_sell_button.label = "Auto Pet Sell"
67
+ await interaction.message.edit(view=self)
68
+
69
+ @discord.ui.button(style=discord.ButtonStyle.primary, label="Auto Pet Sell", custom_id="auto_pet_sell")
70
+ async def auto_pet_sell_button(self, button: discord.ui.Button, interaction: discord.Interaction):
71
+ if self.auto_sell:
72
+ self.auto_sell = False
73
+ auto_sell_users.discard(self.user_id)
74
+ button.label = "Auto Pet Sell"
75
+ await interaction.response.send_message("Auto pet sell stopped.", ephemeral=True)
76
+ else:
77
+ if self.pet_sold:
78
+ await interaction.response.send_message("This pet has already been sold.", ephemeral=True)
79
+ return
80
+ self.auto_sell = True
81
+ auto_sell_users.add(self.user_id)
82
+ button.label = "Stop Auto Pet Sell"
83
+ await interaction.response.send_message("Auto pet sell started. You cannot manually sell pets while auto-sell is enabled.", ephemeral=True)
84
+ asyncio.create_task(auto_sell_process(interaction, self))
85
+
86
+ await interaction.message.edit(view=self)
87
+
88
+ async def auto_sell_process(interaction: discord.Interaction, view: PetRollView):
89
+ user_id = view.user_id
90
+ try:
91
+ while view.auto_sell and not view.pet_sold:
92
+ # Automatically sell the pet
93
+ sell_value = view.rap_value
94
+ user_cash[user_id] = user_cash.get(user_id, 0) + sell_value
95
+ view.pet_sold = True
96
+ await interaction.followup.send(f"Auto-sell: You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True)
97
+
98
+ # Disable the sell button
99
+ view.sell_pet_button.disabled = True
100
+ view.sell_pet_button.label = "Sold"
101
+ # Disable auto-sell as the pet is already sold
102
+ view.auto_sell = False
103
+ view.auto_pet_sell_button.label = "Auto Pet Sell"
104
+ await interaction.message.edit(view=view)
105
+ break
106
+
107
+ # If you want auto-sell to handle multiple pets rolled, you can extend this logic
108
+ except Exception as e:
109
+ print(f"Auto-sell process encountered an error: {e}")
110
+
111
  async def perform_roll(interaction: discord.Interaction):
112
  async def fetch_data(url):
113
  async with aiohttp.ClientSession() as session:
 
129
 
130
  user_id = interaction.user.id
131
  luck_multiplier = luck_multipliers.get(user_id, 1)
132
+
133
  sorted_pets = sorted(pets, key=lambda x: x['configData']['difficulty'])
134
+
135
  max_index = len(sorted_pets) - 1
136
  index = int(max_index * (luck_multiplier - 1) / 9)
137
+
138
  rolled_pet = random.choice(sorted_pets[:index+1])
139
 
140
  pet_rap = next((pet for pet in rap_data['data'] if pet['configData']['id'] == rolled_pet['configName']), None)
 
174
 
175
  embed.set_footer(text=f"Click 'Roll Again' to roll again!{luck_text}")
176
 
177
+ # Initialize the custom view
178
+ view = PetRollView(rap_value=rap_value, user_id=user_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
+ # Handle first luck claim
 
 
 
 
 
 
181
  if user_id not in first_luck_claimed:
182
+ # Add the first luck button
183
  first_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Claim 100% Luck Forever", custom_id="first_luck")
184
 
185
  async def first_luck_callback(interaction: discord.Interaction):
 
192
 
193
  await interaction.response.send_message("You've claimed 100% luck forever!", ephemeral=True)
194
 
195
+ # Remove the first luck button
196
  view.remove_item(first_luck_button)
197
  await interaction.message.edit(view=view)
198
 
 
200
  view.add_item(first_luck_button)
201
 
202
  elif random.random() < 0.6 or user_id not in luck_multipliers:
203
+ # Add the increase luck button
204
  luck_opportunities[user_id] = luck_opportunities.get(user_id, 0) + 1
205
  increase_luck_button = discord.ui.Button(style=discord.ButtonStyle.success, label="Increase Luck", custom_id=f"increase_luck_{luck_opportunities[user_id]}")
206
 
 
225
  luck_percentage = (new_luck - 1) * 100
226
  await interaction.response.send_message(f"Your luck has been increased to {luck_percentage}% for 30 minutes!", ephemeral=True)
227
 
228
+ # Remove the increase luck button
229
  view.remove_item(increase_luck_button)
230
  await interaction.message.edit(view=view)
231
 
 
235
  increase_luck_button.callback = increase_luck_callback
236
  view.add_item(increase_luck_button)
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  return embed, view
239
 
240
  async def schedule_next_luck_opportunity(interaction: discord.Interaction, user_id: int):
 
263
  luck_percentage = (new_luck - 1) * 100
264
  await interaction.response.send_message(f"Your luck has been increased to {luck_percentage}% for 30 minutes!", ephemeral=True)
265
 
266
+ # Remove the increase luck button
267
  view = interaction.message.components[0]
268
  view.remove_item(increase_luck_button)
269
  await interaction.message.edit(view=view)
 
273
 
274
  increase_luck_button.callback = increase_luck_callback
275
 
276
+ # Assuming the original message is the last one sent
277
+ # This might need adjustment based on your actual bot's message handling
278
+ try:
279
+ message = interaction.message
280
+ view = PetRollView(rap_value=0, user_id=user_id) # rap_value is irrelevant here
281
+ view.remove_item(increase_luck_button)
282
+ view.add_item(increase_luck_button)
283
+ await message.edit(view=view)
284
+ except Exception as e:
285
+ print(f"Error scheduling next luck opportunity: {e}")
286
+
287
+ async def auto_sell_process_new_pet(interaction: discord.Interaction, view: PetRollView):
288
+ """
289
+ This function handles auto-selling new pets rolled while auto-sell is enabled.
290
+ """
291
+ user_id = view.user_id
292
+ sell_value = view.rap_value
293
+
294
+ if not view.pet_sold:
295
+ # Automatically sell the pet
296
+ user_cash[user_id] = user_cash.get(user_id, 0) + sell_value
297
+ view.pet_sold = True
298
+ await interaction.followup.send(f"Auto-sell: You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True)
299
+
300
+ # Disable the sell button
301
+ view.sell_pet_button.disabled = True
302
+ view.sell_pet_button.label = "Sold"
303
+
304
+ # Disable auto-sell since the pet is already sold
305
+ view.auto_sell = False
306
+ view.auto_pet_sell_button.label = "Auto Pet Sell"
307
+ await interaction.message.edit(view=view)
308
+
309
+ # Notify the user
310
+ await interaction.followup.send("Auto-sell has been disabled since the pet was automatically sold.", ephemeral=True)
311
+
312
+ async def auto_sell(interaction: discord.Interaction, view: PetRollView):
313
+ """
314
+ This coroutine handles continuous auto-selling while auto-sell is enabled.
315
+ """
316
+ user_id = view.user_id
317
+ while view.auto_sell and not view.pet_sold:
318
+ # Automatically sell the pet
319
+ sell_value = view.rap_value
320
+ user_cash[user_id] = user_cash.get(user_id, 0) + sell_value
321
+ view.pet_sold = True
322
+ await interaction.followup.send(f"Auto-sell: You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True)
323
+
324
+ # Disable the sell button
325
+ view.sell_pet_button.disabled = True
326
+ view.sell_pet_button.label = "Sold"
327
+
328
+ # Disable auto-sell since the pet is already sold
329
+ view.auto_sell = False
330
+ view.auto_pet_sell_button.label = "Auto Pet Sell"
331
+ await interaction.message.edit(view=view)
332
+
333
+ # Notify the user
334
+ await interaction.followup.send("Auto-sell has been disabled since the pet was automatically sold.", ephemeral=True)
335
+ break # Exit the loop since the pet is sold
336
+
337
+ # If you want auto-sell to handle multiple pets rolled over time, remove the break and implement additional logic
338
 
339
  async def auto_roll(interaction: discord.Interaction):
340
  user_id = interaction.user.id
 
342
  while user_id in auto_roll_users:
343
  if time.time() - start_time >= 180: # 3 minutes
344
  auto_roll_users.remove(user_id)
345
+ await interaction.followup.send("auto roll stopped, to turn it on roll again and start it again", ephemeral=True)
346
  break
347
 
348
  result = await perform_roll(interaction)
349
  if result:
350
+ embed, view = result
351
+ # If auto-sell is enabled, automatically sell the pet
352
+ if user_id in auto_sell_users:
353
+ # Wait briefly to ensure the message is sent before auto-selling
354
+ await asyncio.sleep(1)
355
+ # Automatically sell the pet
356
+ sell_value = view.rap_value
357
+ user_cash[user_id] = user_cash.get(user_id, 0) + sell_value
358
+ view.pet_sold = True
359
+ await interaction.followup.send(f"Auto-sell: You sold the pet for ${sell_value}. Your new balance is ${user_cash[user_id]}.", ephemeral=True)
360
+
361
+ # Disable the sell button
362
+ view.sell_pet_button.disabled = True
363
+ view.sell_pet_button.label = "Sold"
364
+
365
+ # Disable auto-sell since the pet is already sold
366
+ view.auto_sell = False
367
+ view.auto_pet_sell_button.label = "Auto Pet Sell"
368
+ await interaction.message.edit(view=view)
369
+
370
+ await interaction.followup.send(embed=embed, view=view)
371
  else:
372
  await interaction.followup.send("An error occurred.")
373
  await asyncio.sleep(5) # Wait for 5 seconds between rolls