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

107 lines
3.3 KiB
Python
Raw Normal View History

2023-06-19 14:20:17 +00:00
import json
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
2024-02-28 13:07:16 +00:00
from lib import economy_embeds, checks
2023-06-19 14:20:17 +00:00
load_dotenv('.env')
special_balance_name = os.getenv("SPECIAL_BALANCE_NAME")
cash_balance_name = os.getenv("CASH_BALANCE_NAME")
with open("config/economy.json") as file:
2023-06-19 14:20:17 +00:00
json_data = json.load(file)
2024-02-27 14:09:59 +00:00
class AwardCog(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-06-19 14:20:17 +00:00
@commands.slash_command(
name="give",
description="Give another user some currency.",
guild_only=True
)
2024-02-28 13:01:20 +00:00
@commands.check(checks.channel)
2023-06-19 14:20:17 +00:00
async def give(self, ctx, *,
user: discord.Option(discord.Member),
currency: discord.Option(choices=["cash", special_balance_name]),
amount: discord.Option(int)):
if ctx.author.id == user.id:
return await ctx.respond(embed=economy_embeds.give_yourself_error(currency))
elif user.bot:
return await ctx.respond(embed=economy_embeds.give_bot_error(currency))
2023-06-19 18:18:23 +00:00
# Currency handler
ctx_currency = Currency(ctx.author.id)
target_currency = Currency(user.id)
2023-06-19 14:20:17 +00:00
try:
if currency == "cash":
2023-06-19 18:18:23 +00:00
author_cash_balance = ctx_currency.cash
2023-06-19 14:20:17 +00:00
if author_cash_balance < amount or author_cash_balance <= 0:
return await ctx.respond(embed=economy_embeds.not_enough_cash())
2023-06-19 18:18:23 +00:00
target_currency.add_cash(amount)
ctx_currency.take_cash(amount)
2023-06-19 14:20:17 +00:00
elif currency == special_balance_name:
2023-06-19 18:18:23 +00:00
author_special_balance = ctx_currency.special
2023-06-19 14:20:17 +00:00
if author_special_balance < amount or author_special_balance <= 0:
return await ctx.respond(embed=economy_embeds.not_enough_special_balance())
2023-06-19 18:18:23 +00:00
target_currency.add_special(amount)
2023-07-03 10:16:47 +00:00
ctx_currency.take_special(amount)
2023-06-19 14:20:17 +00:00
2023-06-19 18:18:23 +00:00
ctx_currency.push()
target_currency.push()
2023-06-19 14:20:17 +00:00
except Exception as e:
2024-02-28 14:59:07 +00:00
await ctx.channel.respond("Something funky happened.. Sorry about that.", ephemeral=True)
2023-06-19 14:20:17 +00:00
print(e)
return
2023-07-18 19:00:21 +00:00
await ctx.respond(embed=economy_embeds.give(ctx, user, currency, Currency.format(amount)))
2023-06-19 14:20:17 +00:00
@commands.slash_command(
name="award",
description="Award currency - owner only command.",
guild_only=True
)
2024-02-28 13:01:20 +00:00
@commands.check(checks.channel)
@commands.check(checks.bot_owner)
2023-06-19 14:20:17 +00:00
async def award(self, ctx, *,
user: discord.Option(discord.Member),
currency: discord.Option(choices=["cash_balance", "special_balance"]),
amount: discord.Option(int)):
2023-06-19 18:18:23 +00:00
# Currency handler
target_currency = Currency(user.id)
2023-06-19 14:20:17 +00:00
try:
if currency == "cash_balance":
2023-06-19 18:18:23 +00:00
target_currency.add_cash(amount)
2023-06-19 14:20:17 +00:00
else:
2023-06-19 18:18:23 +00:00
target_currency.add_special(amount)
target_currency.push()
2023-06-19 14:20:17 +00:00
except Exception as e:
await ctx.channel.respond("Something went wrong. Check console.", ephemeral=True)
print(e)
return
2023-07-18 19:00:21 +00:00
await ctx.respond(embed=economy_embeds.award(user, currency, Currency.format(amount)))
2023-06-19 14:20:17 +00:00
2024-02-28 13:01:20 +00:00
def setup(client):
client.add_cog(AwardCog(client))