99from datetime import datetime , timedelta
1010from discord .ui import View , Button
1111from math import ceil
12+ from collections import Counter
1213
1314dotenv .load_dotenv ()
1415
@@ -50,6 +51,13 @@ def award_xp(user_id, xp):
5051 save_user_data (user_id , user_data )
5152 return user_data
5253
54+ # Function to provide autocomplete options
55+ async def item_autocomplete (interaction : discord .Interaction , current : str ):
56+ # Fetch item names from the database and filter based on the user's input
57+ all_items = [item ["item_name" ] for item in store_collection .find ()]
58+ matching_items = [app_commands .Choice (name = item , value = item ) for item in all_items if current .lower () in item .lower ()]
59+ return matching_items [:25 ] # Return up to 25 matches (Discord's limit)
60+
5361@bot .event
5462async def on_ready ():
5563 # load_data()
@@ -175,7 +183,9 @@ async def give_xp(interaction: discord.Interaction, member: discord.Member, xp:
175183 await interaction .response .send_message (embed = embed )
176184
177185@bot .tree .command (name = "gift" , description = "Gift an item to another user." )
178- async def gift (interaction : discord .Interaction , recipient : discord .Member , * , item_name : str ):
186+ @app_commands .describe (item = "The item you want to purchase" )
187+ @app_commands .autocomplete (item = item_autocomplete )
188+ async def gift (interaction : discord .Interaction , recipient : discord .Member , * , item : str ):
179189 giver_id = str (interaction .user .id )
180190 recipient_id = str (recipient .id )
181191
@@ -212,23 +222,23 @@ async def gift(interaction: discord.Interaction, recipient: discord.Member, *, i
212222 giver_inventory = giver_data .get ("inventory" , [])
213223
214224 # Check if the giver owns the item
215- if item_name not in giver_inventory :
225+ if item not in giver_inventory :
216226 embed = discord .Embed (
217227 title = f"Gift { recipient .display_name } " ,
218- description = f"**❌ You don't own an item called { item_name } .**" ,
228+ description = f"**❌ You don't own an item called { item } .**" ,
219229 color = discord .Color .gold ()
220230 )
221231 embed .set_thumbnail (url = recipient .display_avatar .url )
222232 await interaction .response .send_message (embed = embed )
223233 return
224234
225235 # Update giver's inventory
226- giver_inventory .remove (item_name )
236+ giver_inventory .remove (item )
227237 giver_data ["inventory" ] = giver_inventory
228238
229239 # Update recipient's inventory
230240 recipient_inventory = recipient_data .get ("inventory" , [])
231- recipient_inventory .append (item_name )
241+ recipient_inventory .append (item )
232242 recipient_data ["inventory" ] = recipient_inventory
233243
234244 # Save data to database
@@ -239,7 +249,7 @@ async def gift(interaction: discord.Interaction, recipient: discord.Member, *, i
239249 embed = discord .Embed (
240250 title = "🎁 Gift Successful!" ,
241251 description = (
242- f"{ interaction .user .mention } has gifted **{ item_name } ** to { recipient .mention } !\n "
252+ f"{ interaction .user .mention } has gifted **{ item } ** to { recipient .mention } !\n "
243253 f"Check your inventory to see the updated items."
244254 ),
245255 color = discord .Color .green ()
@@ -321,11 +331,16 @@ async def inventory(interaction: discord.Interaction, user: discord.Member = Non
321331 return
322332
323333 inventory_items = user_data ["inventory" ]
324- items_list = ', ' .join (inventory_items )
334+ item_counts = Counter (inventory_items ) # Count occurrences of each item
335+
336+ # Format the inventory to show "Item XCount"
337+ formatted_inventory = "\n " .join (
338+ f"{ item } x{ count } " for item , count in item_counts .items ()
339+ )
325340
326341 embed = discord .Embed (
327342 title = f"{ user .display_name } 's Inventory:" ,
328- description = f"**{ items_list } ** \n " ,
343+ description = f"**{ formatted_inventory } **" ,
329344 color = discord .Color .gold ()
330345 )
331346 embed .set_thumbnail (url = user .display_avatar .url )
@@ -390,13 +405,6 @@ async def rob_bank(interaction: discord.Interaction):
390405
391406 await interaction .response .send_message (embed = embed )
392407
393- # Function to provide autocomplete options
394- async def item_autocomplete (interaction : discord .Interaction , current : str ):
395- # Fetch item names from the database and filter based on the user's input
396- all_items = [item ["item_name" ] for item in store_collection .find ()]
397- matching_items = [app_commands .Choice (name = item , value = item ) for item in all_items if current .lower () in item .lower ()]
398- return matching_items [:25 ] # Return up to 25 matches (Discord's limit)
399-
400408# Buy command with autocomplete
401409@bot .tree .command (name = "buy" , description = "Buy items from the shop" )
402410@app_commands .describe (item = "The item you want to purchase" )
0 commit comments