Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.7 kB
7
Indexable
Never
    @commands.hybrid_command(name='send', description="Send money to anyone.")
    @app_commands.describe(member="The user to which you want to send the Money.", amount="The amount that you want to send. Use a constant number like '193', a shorthand like '1m' or '1b', or a keyword like 'max' or 'all'.")
    async def send(self, ctx, member: discord.Member, amount: str):
        parsed_amount = self.parse_amount(amount)
        if parsed_amount < 1:
            await ctx.send(f"{ctx.author.mention}, You must send at least 1 currency.")
            return False
        
        if ctx.author.id in self.active_commands:
            await ctx.send(f"{ctx.author.mention}, you have an active command. Please complete or wait for it to timeout.")
            return

        self.active_commands[ctx.author.id] = 'send'

        auth = ctx.author
        amount = self.parse_amount(amount)
        if amount <= 0:
            await ctx.send(f"{ctx.author.mention}, Please enter a valid amount to send.")
            del self.active_commands[ctx.author.id]
            return

        sender = self.get_user(ctx.author.id)
        if not sender or sender[1] < amount:
            await ctx.send(f"{ctx.author.mention}, You do not have enough money to send.")
            del self.active_commands[ctx.author.id]
            return

        confirm_embed = discord.Embed(
            title="Transaction Confirmation",
            description=f"Are you sure you want to send {self.moneyemoji} {amount:,} to {member.display_name}?",
            colour=discord.Colour.blue())
        view = discord.ui.View(timeout=30)

        
        async def button_callback(interaction: discord.Interaction, action: str):
            if interaction.user != ctx.author:
                return await interaction.response.send_message("This is not for you!", ephemeral=True)
            
            for item in view.children:
                item.disabled = True
            await interaction.response.edit_message(view=view)

            if action == 'yes':
                self.update_balance(auth.id, -amount)
                self.update_balance(member.id, +amount)
                success_embed = discord.Embed(
                    title="Money Sent",
                    description=f"Successfully sent {self.moneyemoji} {amount:,} to {member.mention}.",
                    colour=discord.Colour.green())
                await ctx.send(f"{ctx.author.mention}", embed=success_embed)
            else:
                embed = discord.Embed(description=f"{ctx.author.mention}, Trade successfully cancelled.", color=discord.Color.red())
                await ctx.send(embed=embed)

            del self.active_commands[ctx.author.id]
            view.stop()  # Stop listening for interactions

        yes_button = discord.ui.Button(label="Yes", style=discord.ButtonStyle.green)
        no_button = discord.ui.Button(label="No", style=discord.ButtonStyle.red)

        yes_button.callback = lambda i: button_callback(i, 'yes')
        no_button.callback = lambda i: button_callback(i, 'no')

        view.add_item(yes_button)
        view.add_item(no_button)

        async def on_timeout():
            if ctx.author.id in self.active_commands:
                del self.active_commands[ctx.author.id]
            for item in view.children:
                item.disabled = True
            await ctx.interaction.edit_original_response(view=view)
            await ctx.send("Transaction cancelled due to timeout.")

        view.on_timeout = on_timeout

        await ctx.send(embed=confirm_embed, view=view)
Leave a Comment