mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 20:23:12 +00:00
refactor: Improve embed formatting on .daily
This commit is contained in:
parent
ea99afba6f
commit
e9bb5814fa
6 changed files with 85 additions and 84 deletions
|
@ -1,5 +1,11 @@
|
|||
{
|
||||
"bet_limit": "❌ | **{0}** you cannot place any bets above **${1}**.",
|
||||
"daily_already_claimed_author": "Already Claimed",
|
||||
"daily_already_claimed_description": "you can claim your daily reward again <t:{0}:R>.",
|
||||
"daily_already_claimed_footer": "Daily reset is at 7 AM EST",
|
||||
"daily_streak_footer": "You're on a streak of {0} days",
|
||||
"daily_success_claim_author": "Reward Claimed",
|
||||
"daily_success_claim_description": "you claimed your reward of **${0}**!",
|
||||
"error_bad_argument_author": "Bad Argument",
|
||||
"error_bad_argument_description": "{0}",
|
||||
"error_birthdays_disabled_author": "Birthdays Disabled",
|
||||
|
@ -21,6 +27,8 @@
|
|||
"error_not_owner_description": "this command requires Lumi ownership permissions.",
|
||||
"error_private_message_only_author": "Private Message Only",
|
||||
"error_private_message_only_description": "this command can only be used in private messages.",
|
||||
"error_unknown_error_author": "Unknown Error",
|
||||
"error_unknown_error_description": "an unknown error occurred. Please try again later.",
|
||||
"help_use_prefix": "Please use Lumi's prefix to get help. Type `{0}help`",
|
||||
"info_api_version": "**API:** v{0}\n",
|
||||
"info_database_records": "**Database:** {0} records",
|
||||
|
|
|
@ -80,24 +80,6 @@ class GenericErrors:
|
|||
|
||||
|
||||
class EconErrors:
|
||||
@staticmethod
|
||||
def daily_already_claimed(ctx, unix_time):
|
||||
embed = clean_error_embed(ctx)
|
||||
if embed.description is None:
|
||||
embed.description = (
|
||||
f"already claimed. You can claim your reward again <t:{unix_time}:R>."
|
||||
)
|
||||
else:
|
||||
embed.description += (
|
||||
f"already claimed. You can claim your reward again <t:{unix_time}:R>."
|
||||
)
|
||||
embed.set_footer(
|
||||
text=f"For more info do '{formatter.get_prefix(ctx)}help {formatter.get_invoked_name(ctx)}'",
|
||||
icon_url=CONST.QUESTION_ICON,
|
||||
)
|
||||
|
||||
return embed
|
||||
|
||||
@staticmethod
|
||||
def out_of_time(ctx):
|
||||
embed = clean_error_embed(ctx)
|
||||
|
|
|
@ -18,20 +18,6 @@ def clean_info_embed(ctx):
|
|||
return embed
|
||||
|
||||
|
||||
class EconInfo:
|
||||
@staticmethod
|
||||
def daily_reward_claimed(ctx, formatted_amount, streak):
|
||||
embed = clean_info_embed(ctx)
|
||||
embed.description += f"you claimed your reward of **${formatted_amount}**!"
|
||||
|
||||
if streak > 1:
|
||||
embed.set_footer(
|
||||
text=f"You're on a streak of {streak} days", icon_url=streak_icon
|
||||
)
|
||||
|
||||
return embed
|
||||
|
||||
|
||||
class MiscInfo:
|
||||
@staticmethod
|
||||
def ping(ctx, client):
|
||||
|
|
|
@ -2,11 +2,10 @@ import discord
|
|||
from discord.ext import commands, bridge
|
||||
|
||||
from lib import checks
|
||||
from modules.economy import blackjack, slots, balance, stats, give, inventory, daily
|
||||
from modules.economy import blackjack, slots, balance, give, daily
|
||||
|
||||
|
||||
class Economy(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
|
@ -16,7 +15,7 @@ class Economy(commands.Cog):
|
|||
description="Shows your current Lumi balance.",
|
||||
help="Shows your current Lumi balance. The economy system is global, meaning your balance will be synced in "
|
||||
"all servers.",
|
||||
guild_only=True
|
||||
guild_only=True,
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
|
@ -29,7 +28,7 @@ class Economy(commands.Cog):
|
|||
aliases=["bj"],
|
||||
description="Start a game of blackjack.",
|
||||
help="Start a game of blackjack.",
|
||||
guild_only=True
|
||||
guild_only=True,
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
|
@ -41,18 +40,15 @@ class Economy(commands.Cog):
|
|||
aliases=["timely"],
|
||||
description="Claim your daily reward.",
|
||||
help="Claim your daily reward! Reset is at 7 AM EST.",
|
||||
guild_only=True
|
||||
guild_only=True,
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
@commands.cooldown(1, 30, commands.BucketType.user)
|
||||
async def daily_command(self, ctx):
|
||||
return await daily.cmd(ctx)
|
||||
|
||||
@commands.slash_command(
|
||||
name="give",
|
||||
description="Give a server member some cash.",
|
||||
guild_only=True
|
||||
name="give", description="Give a server member some cash.", guild_only=True
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
|
@ -61,12 +57,11 @@ class Economy(commands.Cog):
|
|||
|
||||
@commands.command(
|
||||
name="give",
|
||||
help="Give a server member some cash. You can use ID or mention them."
|
||||
help="Give a server member some cash. You can use ID or mention them.",
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
async def give_command_prefixed(self, ctx, user: discord.User, *, amount: int):
|
||||
|
||||
try:
|
||||
member = await ctx.guild.fetch_member(user.id)
|
||||
except discord.HTTPException:
|
||||
|
@ -79,7 +74,7 @@ class Economy(commands.Cog):
|
|||
aliases=["slot"],
|
||||
description="Start a slots game.",
|
||||
help="Spin the slots for a chance to win the jackpot!",
|
||||
guild_only=True
|
||||
guild_only=True,
|
||||
)
|
||||
@commands.guild_only()
|
||||
@checks.allowed_in_channel()
|
||||
|
|
|
@ -1,27 +1,43 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
import lib.time
|
||||
from lib.embeds.error import EconErrors
|
||||
from lib.embeds.info import EconInfo
|
||||
from lib.embed_builder import EmbedBuilder
|
||||
from services.currency_service import Currency
|
||||
from services.daily_service import Dailies
|
||||
from lib.constants import CONST
|
||||
|
||||
|
||||
async def cmd(ctx):
|
||||
async def cmd(ctx) -> None:
|
||||
ctx_daily = Dailies(ctx.author.id)
|
||||
|
||||
if not ctx_daily.can_be_claimed():
|
||||
wait_time = datetime.now() + timedelta(seconds=lib.time.seconds_until(7, 0))
|
||||
unix_time = int(round(wait_time.timestamp()))
|
||||
embed = EconErrors.daily_already_claimed(ctx, unix_time)
|
||||
return await ctx.respond(embed=embed)
|
||||
|
||||
error_embed = EmbedBuilder.create_error_embed(
|
||||
ctx,
|
||||
author_text=CONST.STRINGS["daily_already_claimed_author"],
|
||||
description=CONST.STRINGS["daily_already_claimed_description"].format(
|
||||
unix_time
|
||||
),
|
||||
footer_text=CONST.STRINGS["daily_already_claimed_footer"],
|
||||
)
|
||||
await ctx.respond(embed=error_embed)
|
||||
return
|
||||
ctx_daily.streak = ctx_daily.streak + 1 if ctx_daily.streak_check() else 1
|
||||
ctx_daily.claimed_at = datetime.now(tz=ctx_daily.tz).isoformat()
|
||||
ctx_daily.amount = int(100 * (12 * (ctx_daily.streak - 1)))
|
||||
ctx_daily.claimed_at = datetime.now(tz=ctx_daily.tz)
|
||||
ctx_daily.amount = 100 * 12 * (ctx_daily.streak - 1)
|
||||
|
||||
ctx_daily.refresh()
|
||||
|
||||
embed = EconInfo.daily_reward_claimed(ctx, Currency.format(ctx_daily.amount), ctx_daily.streak)
|
||||
embed = EmbedBuilder.create_success_embed(
|
||||
ctx,
|
||||
author_text=CONST.STRINGS["daily_success_claim_author"],
|
||||
description=CONST.STRINGS["daily_success_claim_description"].format(
|
||||
Currency.format(ctx_daily.amount)
|
||||
),
|
||||
footer_text=CONST.STRINGS["daily_streak_footer"].format(ctx_daily.streak)
|
||||
if ctx_daily.streak > 1
|
||||
else None,
|
||||
)
|
||||
|
||||
await ctx.respond(embed=embed)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from datetime import datetime, timedelta
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
import pytz
|
||||
|
||||
|
@ -10,44 +11,50 @@ resources = JsonCache.read_json("resources")
|
|||
|
||||
|
||||
class Dailies:
|
||||
def __init__(self, user_id):
|
||||
self.user_id = user_id
|
||||
self.amount = 0
|
||||
self.tz = pytz.timezone('US/Eastern')
|
||||
self.time_now = datetime.now(tz=self.tz)
|
||||
self.reset_time = self.time_now.replace(hour=7, minute=0, second=0, microsecond=0)
|
||||
def __init__(self, user_id: int) -> None:
|
||||
self.user_id: int = user_id
|
||||
self.amount: int = 0
|
||||
self.tz = pytz.timezone("US/Eastern")
|
||||
self.time_now: datetime = datetime.now(tz=self.tz)
|
||||
self.reset_time: datetime = self.time_now.replace(
|
||||
hour=7, minute=0, second=0, microsecond=0
|
||||
)
|
||||
|
||||
data = Dailies.get_data(user_id)
|
||||
data: Tuple[Optional[str], int] = Dailies.get_data(user_id)
|
||||
|
||||
if data[0] is not None:
|
||||
self.claimed_at = datetime.fromisoformat(data[0])
|
||||
self.claimed_at: datetime = datetime.fromisoformat(data[0])
|
||||
else:
|
||||
# set date as yesterday to pretend as a valid claimed_at.
|
||||
self.claimed_at = datetime.now(tz=self.tz) - timedelta(days=2)
|
||||
# set date as yesterday to mock a valid claimed_at.
|
||||
self.claimed_at: datetime = datetime.now(tz=self.tz) - timedelta(days=2)
|
||||
|
||||
self.streak = int(data[1])
|
||||
self.streak: int = int(data[1])
|
||||
|
||||
def refresh(self):
|
||||
def refresh(self) -> None:
|
||||
if self.amount == 0:
|
||||
self.amount = resources["daily_reward"]
|
||||
|
||||
query = """
|
||||
query: str = """
|
||||
INSERT INTO dailies (user_id, amount, claimed_at, streak)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
"""
|
||||
values = (self.user_id, self.amount, self.claimed_at, self.streak)
|
||||
values: Tuple[int, int, str, int] = (
|
||||
self.user_id,
|
||||
self.amount,
|
||||
self.claimed_at.isoformat(),
|
||||
self.streak,
|
||||
)
|
||||
database.execute_query(query, values)
|
||||
|
||||
cash = Currency(self.user_id)
|
||||
cash.add_balance(self.amount)
|
||||
cash.push()
|
||||
|
||||
def can_be_claimed(self):
|
||||
def can_be_claimed(self) -> bool:
|
||||
if self.claimed_at is None:
|
||||
return True
|
||||
|
||||
else:
|
||||
|
||||
if self.time_now < self.reset_time:
|
||||
self.reset_time -= timedelta(days=1)
|
||||
|
||||
|
@ -56,7 +63,7 @@ class Dailies:
|
|||
|
||||
return False
|
||||
|
||||
def streak_check(self):
|
||||
def streak_check(self) -> bool:
|
||||
"""
|
||||
Three checks are performed, only one has to return True.
|
||||
1. the daily was claimed yesterday
|
||||
|
@ -65,15 +72,22 @@ class Dailies:
|
|||
:return:
|
||||
"""
|
||||
|
||||
check_1 = self.claimed_at.date() == (self.time_now - timedelta(days=1)).date()
|
||||
check_2 = self.claimed_at.date() == (self.time_now - timedelta(days=2)).date()
|
||||
check_3 = self.claimed_at.date() == self.time_now.date() and self.claimed_at < self.reset_time
|
||||
check_1: bool = (
|
||||
self.claimed_at.date() == (self.time_now - timedelta(days=1)).date()
|
||||
)
|
||||
check_2: bool = (
|
||||
self.claimed_at.date() == (self.time_now - timedelta(days=2)).date()
|
||||
)
|
||||
check_3: bool = (
|
||||
self.claimed_at.date() == self.time_now.date()
|
||||
and self.claimed_at < self.reset_time
|
||||
)
|
||||
|
||||
return check_1 or check_2 or check_3
|
||||
|
||||
@staticmethod
|
||||
def get_data(user_id):
|
||||
query = """
|
||||
def get_data(user_id: int) -> Tuple[Optional[str], int]:
|
||||
query: str = """
|
||||
SELECT claimed_at, streak
|
||||
FROM dailies
|
||||
WHERE id = (
|
||||
|
@ -91,22 +105,22 @@ class Dailies:
|
|||
return claimed_at, streak
|
||||
|
||||
@staticmethod
|
||||
def load_leaderboard():
|
||||
query = """
|
||||
def load_leaderboard() -> List[Tuple[int, int, str, int]]:
|
||||
query: str = """
|
||||
SELECT user_id, MAX(streak), claimed_at
|
||||
FROM dailies
|
||||
GROUP BY user_id
|
||||
ORDER BY MAX(streak) DESC;
|
||||
"""
|
||||
|
||||
data = database.select_query(query)
|
||||
data: List[Tuple[int, int, str]] = database.select_query(query)
|
||||
|
||||
leaderboard = []
|
||||
rank = 1
|
||||
leaderboard: List[Tuple[int, int, str, int]] = []
|
||||
rank: int = 1
|
||||
for row in data:
|
||||
row_user_id = row[0]
|
||||
streak = row[1]
|
||||
claimed_at = row[2]
|
||||
row_user_id: int = row[0]
|
||||
streak: int = row[1]
|
||||
claimed_at: str = row[2]
|
||||
leaderboard.append((row_user_id, streak, claimed_at, rank))
|
||||
rank += 1
|
||||
|
||||
|
|
Loading…
Reference in a new issue