@@ -88,21 +88,17 @@ async def buy_items(self, interaction: discord.Interaction, button: discord.ui.B
8888 await interaction .response .send_message ("❌ No items available in the bazaar right now." , ephemeral = True )
8989 return
9090
91- # First send the select menu
9291 modal = ItemSelectModal (self .cog , self .cog .current_items )
93- await interaction .response .send_message (
94- "Select an item to purchase:" ,
95- view = modal .select_view ,
96- ephemeral = True
97- )
98-
99- # Then send the modal for amount
100- await interaction .followup .send_modal (modal )
92+ await interaction .response .send_modal (modal )
10193
10294 @discord .ui .button (label = "📈 Buy Stock" , style = discord .ButtonStyle .secondary )
10395 async def buy_stock (self , interaction : discord .Interaction , button : discord .ui .Button ):
10496 await self .cog .handle_stock_purchase (interaction )
10597
98+ @discord .ui .button (label = "📉 Sell Stock" , style = discord .ButtonStyle .secondary )
99+ async def sell_stock (self , interaction : discord .Interaction , button : discord .ui .Button ):
100+ await self .cog .handle_stock_sale (interaction )
101+
106102 @discord .ui .button (label = "🗑️ Close" , style = discord .ButtonStyle .danger )
107103 async def close (self , interaction : discord .Interaction , button : discord .ui .Button ):
108104 await interaction .response .defer ()
@@ -225,7 +221,7 @@ async def reset_bazaar(self):
225221 num_items = random .randint (3 , 5 )
226222 self .current_items = random .sample (category_items , min (num_items , len (category_items )))
227223
228- # Apply bazaar- specific modifications
224+ # Apply bazaar specific modifications
229225 for item in self .current_items :
230226 # Apply random discount (10-30%)
231227 discount = random .uniform (0.1 , 0.3 )
@@ -340,8 +336,9 @@ async def bazaar(self, ctx):
340336 embed = discord .Embed (
341337 title = "🛒 The Wandering Bazaar" ,
342338 description = f"*Exotic goods from distant lands*\n \n "
343- f"Your Bazaar Stock: **{ user_stock } ** (Discount: **{ user_discount * 100 :.0f} %**)\n "
344- f"Current Stock Price: **{ self .calculate_stock_price ()} ** { self .currency } " ,
339+ f"Your Bazaar Stock: **{ user_stock } ** (Discount: **{ user_discount * 100 :.0f} %**)\n "
340+ f"Current Stock Price: **{ self .calculate_stock_price ()} ** { self .currency } \n "
341+ f"Sell Price: **{ int (self .calculate_stock_price () * 0.8 )} ** { self .currency } (80%)" ,
345342 color = 0x9b59b6
346343 )
347344
@@ -381,12 +378,142 @@ async def bazaar(self, ctx):
381378 message = await ctx .reply (embed = embed , view = view )
382379 view .message = message
383380
384- @commands .command (name = "bazaar-buy " , aliases = ["bbuy" ])
381+ @commands .command (name = "bazaarbuy " , aliases = ["bbuy" ])
385382 @commands .cooldown (1 , 5 , commands .BucketType .user )
386383 async def bazaar_buy (self , ctx , item_id : str , amount : int = 1 ):
387384 """Buy an item from the bazaar"""
388385 await self .handle_bazaar_purchase (ctx , item_id , amount )
389386
387+ @commands .command (name = "bazaarsell" , aliases = ["bsell" ])
388+ @commands .cooldown (1 , 10 , commands .BucketType .user )
389+ async def bazaar_sell (self , ctx , amount : int = 1 ):
390+ """Sell your bazaar stock"""
391+ await self .handle_stock_sale (ctx , amount )
392+
393+ async def handle_stock_sale (self , interaction_or_ctx , amount : int = 1 ):
394+ """Handle stock sale from either interaction or command"""
395+ is_interaction = isinstance (interaction_or_ctx , discord .Interaction )
396+
397+ if is_interaction :
398+ interaction = interaction_or_ctx
399+ user = interaction .user
400+ guild = interaction .guild
401+ respond = interaction .response .send_message
402+ else :
403+ ctx = interaction_or_ctx
404+ user = ctx .author
405+ guild = ctx .guild
406+ respond = ctx .reply
407+
408+ if amount <= 0 :
409+ msg = "❌ Amount must be positive."
410+ if is_interaction :
411+ await respond (msg , ephemeral = True )
412+ else :
413+ await respond (msg )
414+ return
415+
416+ # Get user's current stock
417+ current_stock = await self .get_user_stock (user .id )
418+
419+ if current_stock < amount :
420+ msg = f"❌ You only have { current_stock } stock to sell!"
421+ if is_interaction :
422+ await respond (msg , ephemeral = True )
423+ else :
424+ await respond (msg )
425+ return
426+
427+ # Calculate sale price (80% of current stock price)
428+ stock_price = int (self .calculate_stock_price () * 0.8 )
429+ total_gain = stock_price * amount
430+
431+ # For interactions, defer first
432+ if is_interaction :
433+ await interaction .response .defer ()
434+
435+ # Add money to wallet
436+ if not await db .update_wallet (user .id , total_gain , guild .id if guild else None ):
437+ msg = "❌ Failed to process sale. Please try again."
438+ if is_interaction :
439+ await interaction .followup .send (msg , ephemeral = True )
440+ else :
441+ await respond (msg )
442+ return
443+
444+ # Remove stock
445+ result = await db .db .users .update_one (
446+ {"_id" : str (user .id )},
447+ {"$set" : {"bazaar_stock" : current_stock - amount }}
448+ )
449+
450+ if result .modified_count == 0 :
451+ # Refund if failed
452+ await db .update_wallet (user .id , - total_gain , guild .id if guild else None )
453+ msg = "❌ Failed to remove stock. Transaction cancelled."
454+ if is_interaction :
455+ await interaction .followup .send (msg , ephemeral = True )
456+ else :
457+ await respond (msg )
458+ return
459+
460+ # Get updated user data
461+ new_stock = current_stock - amount
462+ new_discount = self .calculate_discount (new_stock )
463+ new_balance = await db .get_wallet_balance (user .id , guild .id if guild else None )
464+
465+ # Create embed
466+ embed = discord .Embed (
467+ title = "✅ Stock Sale Complete" ,
468+ description = f"You sold **{ amount } ** bazaar stock!" ,
469+ color = 0x00ff00
470+ )
471+
472+ embed .add_field (
473+ name = "Sale Details" ,
474+ value = f"Price per Stock: **{ stock_price } ** { self .currency } \n "
475+ f"Total Gain: **{ total_gain } ** { self .currency } " ,
476+ inline = False
477+ )
478+
479+ embed .add_field (
480+ name = "Your New Holdings" ,
481+ value = f"Remaining Stock: **{ new_stock } **\n "
482+ f"Current Discount: **{ new_discount * 100 :.0f} %**\n "
483+ f"New Balance: **{ new_balance } ** { self .currency } " ,
484+ inline = False
485+ )
486+
487+ # Check if they lost secret shop access
488+ if new_stock < self .stock_threshold and current_stock >= self .stock_threshold :
489+ embed .add_field (
490+ name = "🔒 Secret Shop Lost" ,
491+ value = f"You no longer meet the { self .stock_threshold } stock requirement for secret shop access!" ,
492+ inline = False
493+ )
494+
495+ try :
496+ if hasattr (self , 'last_stock_message' ) and self .last_stock_message :
497+ # Edit the existing message
498+ if is_interaction :
499+ await self .last_stock_message .edit (embed = embed )
500+ else :
501+ await self .last_stock_message .edit (embed = embed )
502+ else :
503+ # Send new message and store reference
504+ if is_interaction :
505+ msg = await interaction .followup .send (embed = embed )
506+ else :
507+ msg = await respond (embed = embed )
508+ self .last_stock_message = msg
509+ except Exception as e :
510+ self .logger .error (f"Error updating stock sale message: { e } " )
511+ # Fallback to sending new message if edit fails
512+ if is_interaction :
513+ await interaction .followup .send (embed = embed )
514+ else :
515+ await respond (embed = embed )
516+
390517 async def handle_bazaar_purchase (self , interaction_or_ctx , item_id : str = None , amount : int = 1 ):
391518 """Handle bazaar purchase from either interaction or command"""
392519 is_interaction = isinstance (interaction_or_ctx , discord .Interaction )
@@ -554,7 +681,7 @@ async def handle_bazaar_purchase(self, interaction_or_ctx, item_id: str = None,
554681 else :
555682 await respond (embed = embed )
556683
557- @commands .command (name = "bazaar-stock " , aliases = ["bstock" ])
684+ @commands .command (name = "bazaarstock " , aliases = ["bstock" ])
558685 @commands .cooldown (1 , 10 , commands .BucketType .user )
559686 async def bazaar_stock (self , ctx , amount : int = 1 ):
560687 """Buy bazaar stock"""
0 commit comments