summaryrefslogtreecommitdiff
path: root/commands/inventory.py
blob: 1f81d717f90ec1e7da2d765cc1259dd72bde5e5b (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from discord.ext.commands import Bot, Cog
from discord_slash import cog_ext, SlashContext
from discord_slash.utils.manage_commands import create_option, create_choice
from base import config
from models.user import User
from models.level import Level
from models.item import Item
from models.inventory import Inventory
from peewee import DoesNotExist
import random
import yaml


class InventoryCmd(Cog):
    def __init__(self, bot: Bot):
        self.bot = bot
        with open("config.yaml", "r") as yamlfile:
            config = yaml.load(yamlfile, Loader=yaml.CLoader)
            print("Read config successful")
            self.config = config
            print(config["discord_server_id"])

    @cog_ext.cog_slash(
        name="inventory",
        description="See your inventory",
        guild_ids=[config["discord_server_id"]],
    )
    async def inventory(self, ctx: SlashContext):
        user = User.get(User.discord_id == ctx.author_id)
        inventory = Inventory.select().join(Item).limit(100).where(Inventory.user == user.id)
        print(inventory)
        shop_text = ""
        for inventory_item in inventory:
            shop_text += (
                f"#{inventory_item.item.id}: {inventory_item.item.name} x{inventory_item.stock} \n"
            )
        await ctx.send(content=shop_text)

    @cog_ext.cog_slash(
        name="use",
        description="Use an item",
        guild_ids=[config["discord_server_id"]],
        options=[
            create_option(
                name="item",
                description="Item to use",
                option_type=3,
                required=True,
            ),
            create_option(
                name="target",
                description="User to use the item on",
                option_type=6,
                required=False,
            ),
        ],
    )
    async def use(self, ctx: SlashContext, item: str, target=None):
        user = User.get(User.discord_id == ctx.author_id)

        try:
            if is_number(item):
                using_item = Item.get(Item.id == item)
            else:
                using_item = Item.get(Item.name == item)
        except DoesNotExist:
            await ctx.send(content=f"Item {item} not found. Try using the item number")

        try:
            print(f"user: {user}, item: {using_item}")
            inventory_item = Inventory.get(user=user, item=using_item)
        except DoesNotExist:
            await ctx.send(content=f"You don't own any {item}!")
        if inventory_item.stock > 0:
            inventory_item.stock -= 1
            inventory_item.save()
            use_text = using_item.text
            use_text = use_text.replace("{u}", ctx.author.mention)
            if target is not None:
                use_text = use_text.replace("{t}", target.mention)
            message = await ctx.send(content=f"Solarus used *{using_item.name}*!")
            await ctx.send(content=f"*{use_text}*")
            if using_item.reward_rank is not None:
                print(using_item.reward_rank)
                role = ctx.author.guild.get_role(int(using_item.reward_rank))
                await ctx.author.add_roles(role)
        else:
            await ctx.send(content=f"You don't own any more {using_item.name}!")


def is_number(s):
    try:
        int(s)
        return True
    except ValueError:
        return False


def setup(bot):
    bot.add_cog(InventoryCmd(bot))
    print("Loaded Inventory")