|
| 1 | +import discord |
| 2 | +from discord.ext import commands |
| 3 | +from kumikocore import KumikoCore |
| 4 | +from Libs.cache import KumikoCache |
| 5 | +from Libs.cog_utils.economy import is_economy_enabled |
| 6 | +from Libs.ui.economy import RegisterView |
| 7 | +from Libs.ui.marketplace import ItemPages |
| 8 | +from Libs.utils import ConfirmEmbed, Embed, is_manager |
| 9 | + |
| 10 | + |
| 11 | +class Economy(commands.Cog): |
| 12 | + """Trade, earn, and gamble your way to the top! |
| 13 | +
|
| 14 | + This is Kumiko's flagship module. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, bot: KumikoCore) -> None: |
| 18 | + self.bot = bot |
| 19 | + self.pool = self.bot.pool |
| 20 | + self.redis_pool = self.bot.redis_pool |
| 21 | + |
| 22 | + @property |
| 23 | + def display_emoji(self) -> discord.PartialEmoji: |
| 24 | + return discord.PartialEmoji.from_str("<:upward_stonks:739614245997641740>") |
| 25 | + |
| 26 | + @commands.hybrid_group(name="eco", aliases=["economy"]) |
| 27 | + async def eco(self, ctx: commands.Context) -> None: |
| 28 | + if ctx.invoked_subcommand is None: |
| 29 | + await ctx.send_help(ctx.command) |
| 30 | + |
| 31 | + # Throw checks on these later |
| 32 | + @is_manager() |
| 33 | + @eco.command(name="enable") |
| 34 | + async def enable(self, ctx: commands.Context) -> None: |
| 35 | + """Enables the economy module for your server""" |
| 36 | + key = f"cache:kumiko:{ctx.guild.id}:guild_config" # type: ignore |
| 37 | + cache = KumikoCache(connection_pool=self.redis_pool) |
| 38 | + query = """ |
| 39 | + UPDATE guild |
| 40 | + SET local_economy = $2 |
| 41 | + WHERE id = $1; |
| 42 | + """ |
| 43 | + result = await cache.getJSONCache(key=key, path="$.local_economy") |
| 44 | + if result is True: |
| 45 | + await ctx.send("Economy is already enabled for your server!") |
| 46 | + return |
| 47 | + else: |
| 48 | + await self.pool.execute(query, ctx.guild.id, True) # type: ignore |
| 49 | + await cache.mergeJSONCache( |
| 50 | + key=key, value=True, path="$.local_economy", ttl=None |
| 51 | + ) |
| 52 | + await ctx.send("Enabled economy!") |
| 53 | + return |
| 54 | + |
| 55 | + @is_manager() |
| 56 | + @is_economy_enabled() |
| 57 | + @eco.command(name="disable") |
| 58 | + async def disable(self, ctx: commands.Context) -> None: |
| 59 | + """Disables the economy module for your server""" |
| 60 | + key = f"cache:kumiko:{ctx.guild.id}:config" # type: ignore |
| 61 | + cache = KumikoCache(connection_pool=self.redis_pool) |
| 62 | + query = """ |
| 63 | + UPDATE guild |
| 64 | + SET local_economy = $2 |
| 65 | + WHERE id = $1; |
| 66 | + """ |
| 67 | + if await cache.cacheExists(key=key): |
| 68 | + result = await cache.getJSONCache(key=key, path=".local_economy") |
| 69 | + if result is True: |
| 70 | + await self.pool.execute(query, ctx.guild.id, False) # type: ignore |
| 71 | + await cache.mergeJSONCache( |
| 72 | + key=key, value=False, path="$.local_economy", ttl=None |
| 73 | + ) |
| 74 | + await ctx.send( |
| 75 | + "Economy is now disabled for your server. Please enable it first." |
| 76 | + ) |
| 77 | + return |
| 78 | + else: |
| 79 | + await ctx.send("Economy is already disabled for your server!") |
| 80 | + return |
| 81 | + |
| 82 | + @is_economy_enabled() |
| 83 | + @eco.command(name="wallet", aliases=["bal", "balance"]) |
| 84 | + async def wallet(self, ctx: commands.Context) -> None: |
| 85 | + """View your eco wallet""" |
| 86 | + sql = """ |
| 87 | + SELECT rank, petals, created_at |
| 88 | + FROM eco_user |
| 89 | + WHERE id = $1; |
| 90 | + """ |
| 91 | + user = await self.pool.fetchrow(sql, ctx.author.id) |
| 92 | + if user is None: |
| 93 | + await ctx.send( |
| 94 | + f"You have not created an economy account yet! Run `{ctx.prefix}eco register` to create one." |
| 95 | + ) |
| 96 | + return |
| 97 | + dictUser = dict(user) |
| 98 | + embed = Embed() |
| 99 | + embed.set_author( |
| 100 | + name=f"{ctx.author.display_name}'s Balance", |
| 101 | + icon_url=ctx.author.display_avatar.url, |
| 102 | + ) |
| 103 | + embed.set_footer(text="Created at") |
| 104 | + embed.timestamp = dictUser["created_at"] |
| 105 | + embed.add_field(name="Rank", value=dictUser["rank"], inline=True) |
| 106 | + embed.add_field(name="Petals", value=dictUser["petals"], inline=True) |
| 107 | + await ctx.send(embed=embed) |
| 108 | + |
| 109 | + @is_economy_enabled() |
| 110 | + @eco.command(name="register") |
| 111 | + async def register(self, ctx: commands.Context) -> None: |
| 112 | + """Register for an economy account""" |
| 113 | + view = RegisterView(self.pool) |
| 114 | + embed = ConfirmEmbed() |
| 115 | + embed.description = "Do you want to make an account? The account can only be accessed from your current guild" |
| 116 | + await ctx.send(embed=embed, view=view) |
| 117 | + |
| 118 | + @is_economy_enabled() |
| 119 | + @eco.command(name="inventory", aliases=["inv"]) |
| 120 | + async def inventory(self, ctx: commands.Context) -> None: |
| 121 | + """View your inventory""" |
| 122 | + query = """ |
| 123 | + SELECT eco_item.id, eco_item.name, eco_item.description, eco_item.price, eco_item.amount, eco_item.producer_id |
| 124 | + FROM eco_item_lookup |
| 125 | + INNER JOIN eco_item ON eco_item.id = eco_item_lookup.item_id |
| 126 | + WHERE eco_item.guild_id = $1 AND eco_item.owner_id = $2; |
| 127 | + """ |
| 128 | + rows = await self.pool.fetch(query, ctx.guild.id, ctx.author.id) # type: ignore |
| 129 | + if len(rows) == 0: |
| 130 | + await ctx.send("No items available") |
| 131 | + return |
| 132 | + |
| 133 | + pages = ItemPages(entries=rows, ctx=ctx, per_page=1) |
| 134 | + await pages.start() |
| 135 | + |
| 136 | + |
| 137 | +async def setup(bot: KumikoCore) -> None: |
| 138 | + await bot.add_cog(Economy(bot)) |
0 commit comments