Untitled

 avatar
unknown
plain_text
5 months ago
3.5 kB
3
Indexable
    @commands.hybrid_command(name='redeem', description='Redeem a code for rewards')
    @app_commands.describe(code="The code to redeem")
    async def redeem(self, ctx, code: str):
        if code not in self.codes:
            embed = discord.Embed(description="Invalid code. Please try again.", color=discord.Color.red())
            await ctx.send(embed=embed)
            return

        code_data = self.codes[code]
        if datetime.now() > code_data["expiration"]:
            embed = discord.Embed(description=f"The code '{code}' has expired.", color=discord.Color.red())
            await ctx.send(embed=embed)
            del self.codes[code]
            return

        reward = code_data["reward"]
        del self.codes[code]  # Remove the code after use

        if isinstance(reward, int):
            # Money reward
            self.update_balance(ctx.author.id, reward)
            embed = discord.Embed(description=f"You successfully redeemed the code and got {self.moneyemoji} {reward:,}!", color=discord.Color.green())
        else:
            # Item reward
            item, quantity = reward
            self.update_inventory(ctx.author.id, item, quantity)
            item_data = self.shop_items.get(item, self.items.get(item, {}))
            item_emoji = item_data.get('emoji', '')
            embed = discord.Embed(description=f"You successfully redeemed the code and got {quantity} {item_emoji} **{item}**!", color=discord.Color.green())

        await ctx.send(embed=embed)





    @commands.hybrid_command(name='codegen', description='Generate a redeem code. Owner Only')
    @commands.is_owner()
    @app_commands.autocomplete(item=item_autocomplete)
    @app_commands.describe(quantity="The quantity of money or items", item="The item to generate a code for (optional)")
    async def codegen(self, ctx, quantity: str, item: str = None):
        try:
            quantity = int(quantity)
        except ValueError:
            quantity = self.parse_amount(quantity)

        if quantity < 1:
            embed = discord.Embed(description="You can't put a value less than 1.", color=discord.Color.green())
            await ctx.send(embed=embed)
            return

        code = self.generate_code()
        expiration_time = datetime.now() + timedelta(hours=2)

        if item:
            item = self.find_item(item)
            if not item:
                embed = discord.Embed(description="Invalid item. Please try again.", color=discord.Color.green())
                await ctx.send(embed=embed)
                return
            item_data = self.shop_items.get(item, self.items.get(item, {}))
            item_emoji = item_data.get('emoji', '')
            self.codes[code] = {"reward": (item, quantity), "expiration": expiration_time}
            dm_message = f"Your redeem code for `{quantity}` {item_emoji} **{item}** is: `{code}`\nThis code will expire in 2 hours."
        else:
            self.codes[code] = {"reward": quantity, "expiration": expiration_time}
            dm_message = f"Your redeem code for {self.moneyemoji} {quantity:,} is: `{code}`\nThis code will expire in 2 hours."

        embed = discord.Embed(description="Sent the redeem code in your DM's", color=discord.Color.green())
        await ctx.send(embed=embed)

        dm_embed = discord.Embed(description=dm_message, color=discord.Color.green())
        await ctx.author.send(embed=dm_embed)
Editor is loading...
Leave a Comment