botbotbotbot / cashapp.py
coollsd's picture
Create cashapp.py
ab5e10b verified
raw
history blame
2.94 kB
import discord
from discord import app_commands
from discord.ui import Button, View
from cash import user_cash
class PaymentButtons(View):
def __init__(self, requester: discord.User, amount: int):
super().__init__(timeout=None)
self.requester = requester
self.amount = amount
@discord.ui.button(label="Pay", style=discord.ButtonStyle.green)
async def pay_button(self, interaction: discord.Interaction, button: Button):
payer_id = interaction.user.id
if user_cash.get(payer_id, 0) >= self.amount:
user_cash[payer_id] -= self.amount
user_cash[self.requester.id] = user_cash.get(self.requester.id, 0) + self.amount
await interaction.response.send_message(f"You paid ${self.amount:.2f} to {self.requester.name}.")
await self.requester.send(f"{interaction.user.name} has paid your request of ${self.amount:.2f}.")
else:
await interaction.response.send_message("You don't have enough money mf.", ephemeral=True)
@discord.ui.button(label="Decline", style=discord.ButtonStyle.red)
async def decline_button(self, interaction: discord.Interaction, button: Button):
await interaction.response.send_message(f"You declined the payment request from {self.requester.name}.")
await self.requester.send(f"{interaction.user.name} has declined your request of ${self.amount:.2f}.")
@app_commands.command(name="cashapp", description="Request or pay money to others")
@app_commands.describe(
action="Choose 'request' or 'pay'",
user="The user to request from or pay to",
amount="The amount of money to request or pay"
)
async def cashapp(interaction: discord.Interaction, action: str, user: discord.User, amount: int):
if action.lower() not in ["request", "pay"]:
await interaction.response.send_message("invalid. use 'request' or 'pay'.", ephemeral=True)
return
if amount <= 0:
await interaction.response.send_message("POOR MF IT NEEDS TO BE OVER $0.", ephemeral=True)
return
if action.lower() == "request":
view = PaymentButtons(interaction.user, amount)
await user.send(
f"{interaction.user.name} is requesting ${amount:.2f} from you.",
view=view
)
await interaction.response.send_message(f"Your request for ${amount:.2f} has been sent to {user.name}.")
elif action.lower() == "pay":
payer_id = interaction.user.id
if user_cash.get(payer_id, 0) >= amount:
user_cash[payer_id] -= amount
user_cash[user.id] = user_cash.get(user.id, 0) + amount
await interaction.response.send_message(f"You paid ${amount:.2f} to {user.name}.")
await user.send(f"{interaction.user.name} has paid you ${amount:.2f}.")
else:
await interaction.response.send_message("you are too poor to make this payment.", ephemeral=True)