1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-03 00:23:13 +00:00
Lumi/modules/currency-exchange.py

64 lines
2 KiB
Python
Raw Normal View History

2023-07-20 19:26:00 +00:00
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
2024-02-28 13:01:20 +00:00
from services.Currency import Currency
2023-07-20 19:26:00 +00:00
from main import economy_config
2024-02-28 13:07:16 +00:00
from lib import economy_embeds, checks, interaction
2023-07-20 19:26:00 +00:00
load_dotenv('.env')
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
class ExchangeCog(commands.Cog):
2024-02-28 13:01:20 +00:00
def __init__(self, client):
2024-02-28 13:11:22 +00:00
self.client = client
2023-07-20 19:26:00 +00:00
@commands.slash_command(
name="exchange",
description=f"Exchange {special_balance_name} for cash.",
guild_only=True
)
2024-02-28 13:01:20 +00:00
@commands.check(checks.channel)
2023-07-20 19:26:00 +00:00
async def exchange(self, ctx, *, amount: discord.Option(int)):
# Currency handler
ctx_currency = Currency(ctx.author.id)
author_special_balance = ctx_currency.special
exchange_rate = economy_config["exchange_rate"]
total = amount * exchange_rate
if author_special_balance < amount or author_special_balance <= 0:
return await ctx.respond(embed=economy_embeds.not_enough_special_balance())
view = interaction.ExchangeConfirmation(ctx)
embed = discord.Embed(
description=f"You're about to sell **{amount} {special_balance_name}** for **{cash_balance_name}{Currency.format(total)}**. "
f"Are you absolutely sure about this?"
)
await ctx.respond(embed=embed, view=view)
await view.wait()
if view.clickedConfirm:
ctx_currency.add_cash(total)
ctx_currency.take_special(amount)
ctx_currency.push()
2023-07-20 19:37:09 +00:00
embed.colour = discord.Color.green()
embed.description = f"You successfully exchanged **{amount} {special_balance_name}** " \
f"for **{cash_balance_name}{Currency.format(total)}**."
2023-07-20 19:26:00 +00:00
return await ctx.edit(embed=embed)
2023-07-20 19:37:09 +00:00
embed.colour = discord.Color.red()
embed.description = "The exchange was canceled."
await ctx.edit(embed=embed)
2023-07-20 19:26:00 +00:00
2024-02-28 13:01:20 +00:00
def setup(client):
client.add_cog(ExchangeCog(client))