summaryrefslogtreecommitdiff
path: root/commands/inventory.py
diff options
context:
space:
mode:
Diffstat (limited to 'commands/inventory.py')
-rw-r--r--commands/inventory.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/commands/inventory.py b/commands/inventory.py
new file mode 100644
index 0000000..1f81d71
--- /dev/null
+++ b/commands/inventory.py
@@ -0,0 +1,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")