Skip to content

Commit

Permalink
Economy Update aiosqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Aug 1, 2024
1 parent e051730 commit 76bbad9
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 249 deletions.
39 changes: 16 additions & 23 deletions cogs/economy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class EconomyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
init_db()
self.bot.loop.create_task(init_db())
self.load_config()
self.work_cooldown = {}
self.daily_cooldown = {}
Expand Down Expand Up @@ -87,7 +87,7 @@ def format_time(seconds):
)
async def toppoints(self, interaction: nextcord.Interaction):
try:
top_points = get_top_points()
top_points = await get_top_points()
embed = nextcord.Embed(
title=f"Top {self.currency}", color=nextcord.Color.blurple()
)
Expand All @@ -96,13 +96,8 @@ async def toppoints(self, interaction: nextcord.Interaction):
name=f"{i}. {user_name}",
value=f"{points} {self.currency}",
inline=False,
)
if interaction.response.is_done():
await interaction.followup.send(embed=embed)
else:
await interaction.response.send_message(embed=embed)
except nextcord.errors.NotFound:
print("Interaction not found or expired.")
)
await interaction.response.send_message(embed=embed)
except Exception as e:
print(f"Unexpected error when handling leaderboard command: {e}")

Expand All @@ -120,19 +115,17 @@ async def transferpoints(
):
user_id = str(interaction.user.id)
user_name = interaction.user.display_name
user_name, user_points = get_points(user_id, user_name)
user_name, user_points = await get_points(user_id)
recipient_id = str(recipient.id)
recipient_name = recipient.display_name
recipient_name, recipient_points = get_points(recipient_id, recipient_name)
recipient_name, recipient_points = await get_points(recipient_id)
if user_points < points:
await interaction.response.send_message(
f"You do not have enough {self.currency} to transfer.", ephemeral=True
)
return
new_user_points = user_points - points
new_recipient_points = recipient_points + points
set_points(user_id, user_name, new_user_points)
set_points(recipient_id, recipient_name, new_recipient_points)
await set_points(user_id, user_name, user_points - points)
await set_points(recipient_id, recipient_name, recipient_points + points)
embed = nextcord.Embed(
title=f"{self.currency} Transfer",
description=f"Transferred {points} {self.currency} to {recipient_name}.",
Expand All @@ -144,7 +137,7 @@ async def transferpoints(
async def balance(self, interaction: nextcord.Interaction):
user_id = str(interaction.user.id)
user_name = interaction.user.display_name
user_name, points = get_points(user_id, user_name)
user_name, points = await get_points(user_id)
embed = nextcord.Embed(
title=f"Your {self.currency} Balance",
description=f"You have {str(points)} {self.currency} in your account.",
Expand All @@ -156,8 +149,8 @@ async def balance(self, interaction: nextcord.Interaction):
async def profile(self, interaction: nextcord.Interaction):
user_id = str(interaction.user.id)
user_name = interaction.user.display_name
user_name, points = get_points(user_id, user_name)
steam_id = get_steam_id(user_id)
user_name, points = await get_points(user_id)
steam_id = await get_steam_id(user_id)
embed = nextcord.Embed(
title=f"{user_name}'s Profile", color=nextcord.Color.blurple()
)
Expand All @@ -173,7 +166,7 @@ async def profile(self, interaction: nextcord.Interaction):
name="topinvites", description="Display the top invite leaderboard."
)
async def inviteleaderboard(self, interaction: nextcord.Interaction):
top_invites = get_top_invites()
top_invites = await get_top_invites()
embed = nextcord.Embed(title="Top Invites", color=nextcord.Color.blurple())
if top_invites:
for i, (user_name, invite_count) in enumerate(top_invites, start=1):
Expand Down Expand Up @@ -242,11 +235,11 @@ async def work(self, interaction: nextcord.Interaction):
)
return
user_name = interaction.user.display_name
user_name, points = get_points(user_id, user_name)
user_name, points = await get_points(user_id, user_name)
base_points = random.randint(self.work_min, self.work_max)
earned_points = await self.apply_bonus(base_points, interaction.user)
new_points = points + earned_points
set_points(user_id, user_name, new_points)
await set_points(user_id, user_name, new_points)
desc_text = random.choice(self.work_descriptions).format(
earned_points=earned_points, currency=self.currency
)
Expand Down Expand Up @@ -281,11 +274,11 @@ async def daily(self, interaction: nextcord.Interaction):
return

user_name = interaction.user.display_name
user_name, points = get_points(user_id, user_name)
user_name, points = await get_points(user_id, user_name)
base_points = self.daily_reward
earned_points = await self.apply_bonus(base_points, interaction.user)
new_points = points + earned_points
set_points(user_id, user_name, new_points)
await set_points(user_id, user_name, new_points)

embed = nextcord.Embed(
title="Daily Reward",
Expand Down
4 changes: 2 additions & 2 deletions cogs/economy/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ async def on_member_join(self, member):
print(
f"Member {member.display_name} joined using invite from {invite.inviter.display_name}."
)
add_points(
await add_points(
str(invite.inviter.id),
invite.inviter.display_name,
self.invite_payment,
)
add_invite(str(invite.inviter.id), invite.inviter.display_name)
await add_invite(str(invite.inviter.id), invite.inviter.display_name)
break
self.invites[guild.id] = new_invites

Expand Down
46 changes: 26 additions & 20 deletions cogs/economy/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,51 @@ async def economyset(self, _interaction: nextcord.Interaction):
async def addpoints(
self,
interaction: nextcord.Interaction,
user: nextcord.Member = nextcord.SlashOption(description="Select the user"),
points: int = nextcord.SlashOption(description="How many points to add"),
user: nextcord.Member = nextcord.SlashOption(
description="Select the user"),
points: int = nextcord.SlashOption(
description="How many points to add"),
):
user_id = str(user.id)
user_name = user.display_name
add_points(user_id, user_name, points)
emebd = nextcord.Embed(
await add_points(user_id, user_name, points)
embed = nextcord.Embed(
title=f"Added {self.currency}",
description=f"Added {points} {self.currency} to {user_name}.",
color=nextcord.Color.blurple(),
)
await interaction.response.send_message(embed=emebd, ephemeral=True)
await interaction.response.send_message(embed=embed, ephemeral=True)

@economyset.subcommand(name="checkpoints", description="Check a user's points.")
async def checkpoints(
self,
interaction: nextcord.Interaction,
user: nextcord.Member = nextcord.SlashOption(description="Select the user"),
user: nextcord.Member = nextcord.SlashOption(
description="Select the user"),
):
user_id = str(user.id)
user_name = user.display_name
user_name, points = get_points(user_id, user_name)
user_name, points = await get_points(user_id, user_name)

embed = nextcord.Embed(
title=f"Check {self.currency}",
description=f"{user_name} has {points} {self.currency}.",
color=nextcord.Color.blurple(),
)
if user_name:
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
await interaction.response.send_message("User not found.", ephemeral=True)
await interaction.response.send_message(embed=embed, ephemeral=True)

@economyset.subcommand(name="setpoints", description="Set a user's points.")
async def setpoints(
self,
interaction: nextcord.Interaction,
user: nextcord.Member = nextcord.SlashOption(description="Select the user"),
points: int = nextcord.SlashOption(description="How many points to set"),
user: nextcord.Member = nextcord.SlashOption(
description="Select the user"),
points: int = nextcord.SlashOption(
description="How many points to set"),
):
user_id = str(user.id)
user_name = user.display_name
set_points(user_id, user_name, points)
await set_points(user_id, user_name, points)
embed = nextcord.Embed(
title=f"Set {self.currency}",
description=f"Set {user_name}'s {self.currency} to {points}.",
Expand All @@ -89,8 +91,10 @@ async def setpoints(
async def force_steam(
self,
interaction: nextcord.Interaction,
user: nextcord.Member = nextcord.SlashOption(description="Select the user"),
steam_id: str = nextcord.SlashOption(description="Enter the user's Steam ID"),
user: nextcord.Member = nextcord.SlashOption(
description="Select the user"),
steam_id: str = nextcord.SlashOption(
description="Enter the user's Steam ID"),
):
user_id = str(user.id)
user_name = user.display_name
Expand All @@ -108,20 +112,22 @@ async def force_steam(
async def removepoints(
self,
interaction: nextcord.Interaction,
user: nextcord.Member = nextcord.SlashOption(description="Select the user"),
points: int = nextcord.SlashOption(description="How many points to remove"),
user: nextcord.Member = nextcord.SlashOption(
description="Select the user"),
points: int = nextcord.SlashOption(
description="How many points to remove"),
):
user_id = str(user.id)
user_name = user.display_name
user_name, current_points = get_points(user_id, user_name)
user_name, current_points = await get_points(user_id, user_name)
if current_points < points:
await interaction.response.send_message(
f"{user_name} does not have enough {self.currency} to remove.",
ephemeral=True,
)
return
new_points = current_points - points
set_points(user_id, user_name, new_points)
await set_points(user_id, user_name, new_points)
embed = nextcord.Embed(
title=f"Removed {self.currency}",
description=f"Removed {points} {self.currency} from {user_name}.",
Expand Down
62 changes: 14 additions & 48 deletions cogs/economy/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ async def generate_shop_embed(self):
item_info = self.shop_items[item_name]
embed.add_field(
name=item_name,
value=f"{item_info['description']}\n"
f"**Price:** {item_info['price']} {self.currency}",
value=f"{item_info['description']}\n**Price:** {item_info['price']} {self.currency}",
inline=False,
)
embed.set_footer(
Expand Down Expand Up @@ -81,10 +80,7 @@ def load_shop_items(self):
shop_items_path = os.path.join(config_path, "kits.json")
with open(shop_items_path) as shop_items_file:
all_items = json.load(shop_items_file)
# Filtering out items with a price of 0
self.shop_items = {
key: value for key, value in all_items.items() if value["price"] > 0
}
self.shop_items = {key: value for key, value in all_items.items() if value["price"] > 0}

@nextcord.slash_command(name="shop", description="Shop commands.")
async def shop(self, _interaction: nextcord.Interaction):
Expand All @@ -100,28 +96,16 @@ async def menu(self, interaction: nextcord.Interaction):
async def redeem(
self,
interaction: nextcord.Interaction,
item_name: str = nextcord.SlashOption(
description="The name of the item to redeem.", autocomplete=True
),
server: str = nextcord.SlashOption(
description="Select a server", autocomplete=True
),
item_name: str = nextcord.SlashOption(description="The name of the item to redeem.", autocomplete=True),
server: str = nextcord.SlashOption(description="Select a server", autocomplete=True),
):
await interaction.response.defer()
await interaction.response.defer(ephemeral=True)
user_id = str(interaction.user.id)
user_name = interaction.user.display_name
user_name, points = await get_points(user_id)

data = get_points(user_id, user_name)
if not data:
await interaction.followup.send(
"There was an error retrieving your data.", ephemeral=True
)
return

user_name, points = data
steam_id = get_steam_id(user_id)

if steam_id is None:
steam_id = await get_steam_id(user_id)
if not steam_id:
await interaction.followup.send("No Steam ID linked.", ephemeral=True)
return

Expand All @@ -130,22 +114,12 @@ async def redeem(
await interaction.followup.send("Item not found.", ephemeral=True)
return

# Added price check so that items with a price of 0 cannot be redeemed
if item["price"] <= 0:
await interaction.followup.send(
"This item cannot be redeemed.", ephemeral=True
)
return

if points < item["price"]:
await interaction.followup.send(
f"You do not have enough {self.currency} to redeem this item.",
ephemeral=True,
)
await interaction.followup.send(f"You do not have enough {self.currency} to redeem this item.", ephemeral=True)
return

new_points = points - item["price"]
set_points(user_id, user_name, new_points)
await set_points(user_id, user_name, new_points)

for command_template in item["commands"]:
command = command_template.format(steamid=steam_id)
Expand All @@ -160,21 +134,13 @@ async def redeem(
await interaction.followup.send(embed=embed, ephemeral=True)

@redeem.on_autocomplete("server")
async def on_autocomplete_server(
self, interaction: nextcord.Interaction, current: str
):
choices = [
server for server in self.servers if current.lower() in server.lower()
][:25]
async def on_autocomplete_server(self, interaction: nextcord.Interaction, current: str):
choices = [server for server in self.servers if current.lower() in server.lower()][:25]
await interaction.response.send_autocomplete(choices)

@redeem.on_autocomplete("item_name")
async def on_autocomplete_shop_items(
self, interaction: nextcord.Interaction, current: str
):
choices = [name for name in self.shop_items if current.lower() in name.lower()][
:25
]
async def on_autocomplete_shop_items(self, interaction: nextcord.Interaction, current: str):
choices = [name for name in self.shop_items if current.lower() in name.lower()][:25]
await interaction.response.send_autocomplete(choices)

def setup(bot):
Expand Down
14 changes: 9 additions & 5 deletions cogs/economy/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,30 @@ async def vote_status(self, steam_id):
url = f"https://serverlist.gg/api/rewards/{self.server_slug}&key={self.api_key}&steamid={steam_id}&type=check"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
if response.status == 200:
return await response.text()
return None

async def claim_reward(self, steam_id):
url = f"https://serverlist.gg/api/rewards/{self.server_slug}&key={self.api_key}&steamid={steam_id}&type=claim"
async with aiohttp.ClientSession() as session:
async with session.post(url) as response:
return await response.text()
if response.status == 200:
return await response.text()
return None

@nextcord.slash_command(name="claimreward", description="Claim your vote reward.")
async def votereward(self, interaction: nextcord.Interaction):
user_id = str(interaction.user.id)
steam_id = get_steam_id(user_id)
steam_id = await get_steam_id(user_id)
if steam_id is None:
await interaction.response.send_message("Your Steam ID is not linked.", ephemeral=True)
return

vote_status = await self.vote_status(steam_id)
if vote_status == "1":
await self.claim_reward(steam_id)
add_points(user_id, interaction.user.display_name, self.vote_reward)
await add_points(user_id, interaction.user.display_name, self.vote_reward)
await interaction.response.send_message(f"Thank you for voting! Your reward of {self.vote_reward} {self.currency} has been claimed.", ephemeral=True)
elif vote_status == "2":
await interaction.response.send_message("You have already claimed your reward.", ephemeral=True)
Expand All @@ -65,7 +69,7 @@ async def votereward(self, interaction: nextcord.Interaction):
@nextcord.slash_command(name="claimkit", description="Claim a kit as a vote reward.")
async def claim_kit(self, interaction: nextcord.Interaction, kit_name: str = nextcord.SlashOption(description="Select a kit", autocomplete=True), server: str = nextcord.SlashOption(description="Select a server", autocomplete=True)):
user_id = str(interaction.user.id)
steam_id = get_steam_id(user_id)
steam_id = await get_steam_id(user_id)
if steam_id is None:
await interaction.response.send_message("Your Steam ID is not linked.", ephemeral=True)
return
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ nextcord==2.6.0
python-dotenv==1.0.1
gamercon-async==1.0.6
aiohttp==3.9.5
aiosqlite==0.20.0
pytz==2024.1
steam==1.4.4
Loading

0 comments on commit 76bbad9

Please sign in to comment.