Skip to content

Commit

Permalink
Updated Shop and Kits, Autocomplete Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Sep 29, 2024
1 parent 282ecbe commit ea0f50d
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 17 deletions.
97 changes: 93 additions & 4 deletions cogs/economy/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,24 @@ async def get_server_info(self, server_name: str):
"password": details[2]
}
return None

@nextcord.slash_command(description=t("ShopCog", "shop.description"))
async def shop(self, _interaction: nextcord.Interaction):
pass

@nextcord.slash_command(name="shop", description=t("ShopCog", "shop.description"))
@shop.subcommand(name="menu", description=t("ShopCog", "shop.description"))
@restrict_command()
async def shop(self, interaction: nextcord.Interaction, server: str = nextcord.SlashOption(description=t("ShopCog", "shop.server_description"), autocomplete=True)):
async def menu(self, interaction: nextcord.Interaction, server: str = nextcord.SlashOption(description=t("ShopCog", "shop.server_description"), autocomplete=True)):
view = ShopView(self.shop_items, self.currency, self, server)
embed = await view.generate_shop_embed()
await interaction.response.send_message(embed=embed, view=view, ephemeral=True)

@shop.on_autocomplete("server")
@menu.on_autocomplete("server")
async def on_autocomplete_server(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

choices = [server for server in self.servers if current.lower() in server.lower()][:25]
choices = [server for server in self.servers if current.lower() in server.lower()][:10]
await interaction.response.send_autocomplete(choices)

async def purchase_item(self, interaction: nextcord.Interaction, item_name: str, server: str):
Expand Down Expand Up @@ -200,6 +204,91 @@ async def purchase_item(self, interaction: nextcord.Interaction, item_name: str,
logging.info(f"User {user_name} (ID: {user_id}) purchased {item_name} for {item['price']} {self.currency} on server {server}. Remaining points: {new_points}")

await interaction.followup.send(embed=embed, ephemeral=True)

@shop.subcommand(name="redeem", description=t("ShopCog", "shop.redeem.description"))
@restrict_command()
async def redeem(
self,
interaction: nextcord.Interaction,
item_name: str = nextcord.SlashOption(
description=t("ShopCog", "shop.redeem.item_description"), autocomplete=True
),
server: str = nextcord.SlashOption(
description=t("ShopCog", "shop.redeem.server_description"), autocomplete=True
),
):
await interaction.response.defer(ephemeral=True)
user_id = str(interaction.user.id)
user_name = interaction.user.display_name

data = await get_points(user_id, user_name)
if not data:
await interaction.followup.send(
t("ShopCog", "shop.redeem.error_retrieve_data"), ephemeral=True
)
return

user_name, points = data
steam_id = await get_steam_id(user_id)

if steam_id is None:
await interaction.followup.send(t("ShopCog", "shop.redeem.error_no_steamid"), ephemeral=True)
return

item = self.shop_items.get(item_name)
if not item:
await interaction.followup.send(t("ShopCog", "shop.redeem.error_item_not_found"), ephemeral=True)
return

if points < item["price"]:
await interaction.followup.send(
t("ShopCog", "shop.redeem.error_not_enough_points").format(currency=self.currency),
ephemeral=True,
)
return

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

server_info = await self.get_server_info(server)
if not server_info:
await interaction.followup.send(f"Server {server} not found.", ephemeral=True)
return

for command_template in json.loads(item["commands"]):
command = command_template.format(steamid=steam_id)
try:
await self.rcon_util.rcon_command(server_info, command)
await asyncio.sleep(1)
except Exception as e:
await interaction.followup.send(f"Error executing command '{command}': {e}", ephemeral=True)
return

embed = nextcord.Embed(
title=t("ShopCog", "shop.redeem.success_title").format(item_name=item_name),
description=t("ShopCog", "shop.redeem.success_description").format(
item_name=item_name, item_price=item['price'], currency=self.currency, server=server, remaining_points=new_points
),
color=nextcord.Color.green(),
)
await interaction.followup.send(embed=embed, ephemeral=True)

@redeem.on_autocomplete("server")
async def on_autocomplete_server(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

choices = [server for server in self.servers if current.lower() in server.lower()][:10]
await interaction.response.send_autocomplete(choices)

@redeem.on_autocomplete("item_name")
async def on_autocomplete_shop_items(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

choices = [name for name in self.shop_items if current.lower() in name.lower()][:10]
await interaction.response.send_autocomplete(choices)


def setup(bot):
bot.add_cog(ShopCog(bot))
58 changes: 53 additions & 5 deletions cogs/kits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
autocomplete_kits,
save_kit,
delete_kit,
fetch_all_kits,
KitModal
)
import json
import asyncio
import os
from utils.translations import t
from utils.errorhandling import restrict_command

Expand All @@ -27,7 +29,7 @@ async def load_servers(self):
self.servers = await server_autocomplete()

async def autocomplete_server(self, interaction: nextcord.Interaction, current: str):
choices = [server for server in self.servers if current.lower() in server.lower()][:25]
choices = [server for server in self.servers if current.lower() in server.lower()][:10]
await interaction.response.send_autocomplete(choices)

async def get_server_info(self, server_name: str):
Expand Down Expand Up @@ -76,12 +78,18 @@ async def givekit(self, interaction: nextcord.Interaction, steamid: str, kit_nam

@givekit.on_autocomplete("server")
async def on_autocomplete_rcon(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return []

await self.autocomplete_server(interaction, current)

@givekit.on_autocomplete("kit_name")
async def on_autocomplete_kits(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return []

choices = await autocomplete_kits(current)
await interaction.response.send_autocomplete(choices)
await interaction.response.send_autocomplete(choices[:10])

@nextcord.slash_command(name="managekits", description=t("KitsCog", "manage_kits.description"), default_member_permissions=nextcord.Permissions(administrator=True))
@restrict_command()
Expand All @@ -100,8 +108,11 @@ async def manage_kits(self, interaction: nextcord.Interaction, kit_name: str = "

@manage_kits.on_autocomplete("kit_name")
async def on_autocomplete_kits(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

choices = await autocomplete_kits(current)
await interaction.response.send_autocomplete(choices[:25])
await interaction.response.send_autocomplete(choices[:10])

@nextcord.slash_command(name="deletekit", description=t("KitsCog", "delete_kit.description"), default_member_permissions=nextcord.Permissions(administrator=True))
@restrict_command()
Expand All @@ -113,8 +124,11 @@ async def delete_kit(self, interaction: nextcord.Interaction, kit_name: str):

@delete_kit.on_autocomplete("kit_name")
async def on_autocomplete_kits(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

choices = await autocomplete_kits(current)
await interaction.response.send_autocomplete(choices[:25])
await interaction.response.send_autocomplete(choices[:10])

@nextcord.slash_command(name="uploadkits", description=t("KitsCog", "uploadkits.description"), default_member_permissions=nextcord.Permissions(administrator=True))
@restrict_command()
Expand All @@ -140,6 +154,39 @@ async def uploadkits(self, interaction: nextcord.Interaction, json_file: nextcor
except Exception as e:
await interaction.followup.send(t("KitsCog", "uploadkits.error").format(error=e), ephemeral=True)

@nextcord.slash_command(name="exportkits", description="Export all kits as a JSON file.", default_member_permissions=nextcord.Permissions(administrator=True))
@restrict_command()
async def exportkits(self, interaction: nextcord.Interaction):
await interaction.response.defer(ephemeral=True)

try:
kits = await fetch_all_kits()
if not kits:
await interaction.followup.send("No kits found to export.", ephemeral=True)
return

kits_data = {
kit[0]: {
'commands': json.loads(kit[1]),
'description': kit[2],
'price': kit[3]
} for kit in kits
}

kits_json = json.dumps(kits_data, indent=4)
file_path = "kits_exported.json"

with open(file_path, "w") as file:
file.write(kits_json)

await interaction.followup.send(file=nextcord.File(file_path), ephemeral=True)
os.remove(file_path)

except Exception as e:
await interaction.followup.send(f"An error occurred while exporting kits: {str(e)}", ephemeral=True)
if os.path.exists(file_path):
os.remove(file_path)

def setup(bot):
cog = KitsCog(bot)
bot.add_cog(cog)
Expand All @@ -150,6 +197,7 @@ def setup(bot):
cog.givekit,
cog.manage_kits,
cog.delete_kit,
cog.uploadkits
cog.uploadkits,
cog.exportkits
]
)
11 changes: 9 additions & 2 deletions cogs/palgame/battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ def load_pals(self):

async def pal_autocomplete(self, interaction: nextcord.Interaction, current: str):
user_pals = await get_pals(str(interaction.user.id))
top_pals = sorted(user_pals, key=lambda pal: pal[1], reverse=True)[:5]
choices = [pal[0] for pal in top_pals if current.lower() in pal[0].lower()]
if current:
choices = [pal[0] for pal in user_pals if current.lower() in pal[0].lower()]
else:
top_pals = sorted(user_pals, key=lambda pal: pal[1], reverse=True)[:5]
choices = [pal[0] for pal in top_pals]

await interaction.response.send_autocomplete(choices=choices)

@nextcord.slash_command(
Expand Down Expand Up @@ -157,6 +161,9 @@ def calculate_damage(self, skill_power, attack_type, user_pal, opponent_pal):

@battle.on_autocomplete("pal_name")
async def on_autocomplete_pal(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

await self.pal_autocomplete(interaction, current)

def setup(bot):
Expand Down
11 changes: 5 additions & 6 deletions cogs/palgame/paldex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def load_game_data(self):
self.game_data = json.load(game_data_file)

async def autocomplete_pal(self, interaction: nextcord.Interaction, current: str):
choices = [
pal["Name"] for pal in self.game_data if current.lower() in pal["Name"].lower()
][:10]
choices = [pal["Name"] for pal in self.game_data if current.lower() in pal["Name"].lower()][:10]
await interaction.response.send_autocomplete(choices)

@nextcord.slash_command(description="Search for a Pal in the Paldex")
Expand Down Expand Up @@ -50,9 +48,10 @@ async def paldex(
await interaction.followup.send("Pal not found.")

@paldex.on_autocomplete("name")
async def autocomplete_pal_name(
self, interaction: nextcord.Interaction, current: str
):
async def autocomplete_pal_name(self, interaction: nextcord.Interaction, current: str):
if interaction.guild is None:
return[]

await self.autocomplete_pal(interaction, current)

def setup(bot):
Expand Down
6 changes: 6 additions & 0 deletions utils/kitutility.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ async def autocomplete_kits(current: str):
kits = await cursor.fetchall()
return [kit[0] for kit in kits]

async def fetch_all_kits():
async with aiosqlite.connect(DATABASE_PATH) as db:
cursor = await db.execute('SELECT name, commands, description, price FROM kits')
kits = await cursor.fetchall()
return kits

async def load_shop_items():
shop_items = {}
async with aiosqlite.connect(DATABASE_PATH) as db:
Expand Down

0 comments on commit ea0f50d

Please sign in to comment.