Skip to content

Commit

Permalink
🗓️ Add rewards cog for daily, weekly, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
tookender committed Sep 15, 2024
1 parent 312d2cf commit e3c8e02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion data/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ CREATE TABLE IF NOT EXISTS economy (
balance BIGINT,
bank BIGINT,
job TEXT,
last_job_claim TIMESTAMP WITH TIME ZONE
last_job_claim TIMESTAMP WITH TIME ZONE,

last_daily TIMESTAMP WITH TIME ZONE,
last_weekly TIMESTAMP WITH TIME ZONE,
last_monthly TIMESTAMP WITH TIME ZONE
);

CREATE TABLE IF NOT EXISTS guilds (
Expand Down
3 changes: 2 additions & 1 deletion extensions/economy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from .gambling import GamblingCog
from .work import WorkCog
from .job import JobCog
from .rewards import RewardsCog


class Economy(BasicCog, GamblingCog, WorkCog, BankCog, JobCog):
class Economy(BasicCog, GamblingCog, WorkCog, BankCog, JobCog, RewardsCog):
pass


Expand Down
39 changes: 39 additions & 0 deletions extensions/economy/rewards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import discord
from datetime import timedelta
from discord.ext import commands

from ._base import EconomyBase
from .utils import add_money


class RewardsCog(EconomyBase):
@commands.hybrid_command(description="Claim your daily reward of $1000.")
async def daily(self, ctx):
await self.claim_reward(ctx, "daily", 1000, timedelta(days=1), "You can claim your next daily reward {time}.")

@commands.hybrid_command(description="Claim your weekly reward of $5000.")
async def weekly(self, ctx):
await self.claim_reward(ctx, "weekly", 5000, timedelta(days=7), "You can claim your next weekly reward {time}.")

@commands.hybrid_command(description="Claim your monthly reward of $10000.")
async def monthly(self, ctx):
await self.claim_reward(ctx, "monthly", 10000, timedelta(days=31), "You can claim your next monthly reward {time}.")

async def claim_reward(self, ctx, reward_type, amount, cooldown, message):
last_claim_column = f"last_{reward_type}"
last_claim = await self.bot.pool.fetchval(f"SELECT {last_claim_column} FROM economy WHERE user_id = $1", ctx.author.id)

now = discord.utils.utcnow()

if last_claim is not None:
time_since_last_claim = now - last_claim

if time_since_last_claim < cooldown:
next_claim_time = last_claim + cooldown
formatted_time = discord.utils.format_dt(next_claim_time, style="R")
await self.send_embed(ctx, text=message.format(time=formatted_time), return_embed=False)
return

await self.bot.pool.execute(f"UPDATE economy SET {last_claim_column} = $1 WHERE user_id = $2", now, ctx.author.id)
await add_money(self.bot, ctx.author.id, amount)
await self.send_embed(ctx, text=f"You've claimed your {reward_type} reward of ${amount}!", return_embed=False)

0 comments on commit e3c8e02

Please sign in to comment.