File size: 2,819 Bytes
9bb83f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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

    @app_commands.command(name="inventory", description="View your sticker collection")
    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)

    @app_commands.command(name="use", description="Show off a sticker you own")
    @app_commands.describe(sticker_id="The sticker ID to display")
    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))