Spaces:
Runtime error
Runtime error
| import discord | |
| from discord.ext import commands | |
| from discord import app_commands | |
| import os, sys | |
| sys.path.append(os.path.dirname(os.path.dirname(__file__))) | |
| from database.models import get_db | |
| class Inventory(commands.Cog): | |
| def __init__(self, bot): | |
| self.bot = bot | |
| async def inventory(self, interaction: discord.Interaction): | |
| await interaction.response.defer(ephemeral=True) | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute(""" | |
| SELECT s.* FROM stickers s | |
| JOIN inventory i ON s.id = i.sticker_id | |
| WHERE i.discord_user_id = ? | |
| ORDER BY s.character, s.name | |
| """, (str(interaction.user.id),)) | |
| owned = c.fetchall() | |
| conn.close() | |
| if not owned: | |
| await interaction.followup.send( | |
| "You don't own any stickers yet! Use `/shop` to browse.", ephemeral=True | |
| ) | |
| return | |
| embed = discord.Embed( | |
| title=f"🎒 {interaction.user.display_name}'s Collection", | |
| description=f"You own **{len(owned)}** sticker(s)", | |
| color=0x9B59B6 | |
| ) | |
| by_char = {} | |
| for s in owned: | |
| by_char.setdefault(s["character"], []).append(s["name"]) | |
| for char, names in by_char.items(): | |
| embed.add_field(name=f"✦ {char}", value="\n".join(f"• {n}" for n in names), inline=True) | |
| await interaction.followup.send(embed=embed, ephemeral=True) | |
| async def use(self, interaction: discord.Interaction, sticker_id: str): | |
| await interaction.response.defer() | |
| conn = get_db() | |
| c = conn.cursor() | |
| c.execute(""" | |
| SELECT s.* FROM stickers s | |
| JOIN inventory i ON s.id = i.sticker_id | |
| WHERE i.discord_user_id = ? AND s.id = ? | |
| """, (str(interaction.user.id), sticker_id)) | |
| s = c.fetchone() | |
| conn.close() | |
| if not s: | |
| await interaction.followup.send( | |
| f"You don't own `{sticker_id}`. Use `/shop` to buy it or `/inventory` to see what you have." | |
| ) | |
| return | |
| if not os.path.exists(s["image_path"]): | |
| await interaction.followup.send("Image file not found.") | |
| return | |
| with open(s["image_path"], "rb") as f: | |
| file = discord.File(f, filename="sticker.jpg") | |
| await interaction.followup.send( | |
| content=f"✨ **{interaction.user.display_name}** uses **{s['name']}**!", | |
| file=file | |
| ) | |
| async def setup(bot): | |
| await bot.add_cog(Inventory(bot)) | |