Skip to content

Commit 8cc0f8e

Browse files
committed
Added new functions to purchase_logger.py
- Made purchase functions subcommand under main command "purchase" - Created display and remove commands with basic functionality
1 parent 1c06e1a commit 8cc0f8e

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

purchase_logger.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, bot):
1313
self.database = sqlite3.connect('database.db')
1414
self.cursor = self.database.cursor()
1515

16-
self.database.execute("CREATE TABLE IF NOT EXISTS purchases(user_id INT, category STRING, title STRING, description STRING, cost FLOAT)")
16+
self.database.execute("CREATE TABLE IF NOT EXISTS purchases(user_id INT, category STRING, title STRING, description STRING, cost FLOAT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)")
1717
self.database.execute("CREATE TABLE IF NOT EXISTS categories(user_id INT, category STRING)")
1818

1919

@@ -33,7 +33,7 @@ async def add_category(
3333
):
3434
"""Adds a category into the category list that is called when using a command involving any purchases"""
3535

36-
# Fetching all added categories by user in database
36+
# Fetching all added categories by user in databasenextcord.errors.ApplicationInvokeError: Command raised an exception: OperationalError: table purchases has 6 columns but 5 values were supplied
3737
categories = self.fetch_categories(interaction)
3838

3939
if category in categories:
@@ -58,7 +58,7 @@ async def clear_categories(
5858
):
5959
"""Removes a category from the category list that is called when using a command involving any purchases"""
6060
pass
61-
61+
6262

6363
@category.subcommand(description="Removes a category from the available list")
6464
async def remove_category(
@@ -69,8 +69,39 @@ async def remove_category(
6969
"""Removes a category from the category list that is called when using a command involving any purchases"""
7070
pass
7171

72+
73+
@nextcord.slash_command(guild_ids=[TESTING_GUILD_ID], description="main command")
74+
async def purchase(self, interaction: nextcord.Interaction):
75+
"""
76+
This is the main slash command that will be the prefix of all commands below.
77+
This will never get called since it has subcommands.
78+
"""
79+
80+
81+
@purchase.subcommand(description="Lists all purchases")
82+
async def display(self, interaction: nextcord.Interaction):
83+
query = "SELECT * FROM purchases WHERE user_id = ?"
84+
data = self.cursor.execute(query, (interaction.user.id,))
85+
86+
await interaction.response.send_message(f"{data.fetchall()}")
87+
88+
@purchase.subcommand(description="Remove latest entry")
89+
async def remove(self, interaction: nextcord.Interaction):
90+
query = """DELETE FROM purchases
91+
WHERE timestamp = (
92+
SELECT timestamp
93+
FROM purchases
94+
WHERE user_id = ? -- Replace ? with the user_id you want to target
95+
ORDER BY timestamp DESC
96+
LIMIT 1
97+
);"""
98+
self.cursor.execute(query, (interaction.user.id,))
99+
self.database.commit()
100+
101+
await interaction.response.send_message("Done!")
72102

73-
@nextcord.slash_command(description="Logging new entry", guild_ids=[TESTING_GUILD_ID])
103+
104+
@purchase.subcommand(description="Logging new entry")
74105
async def add_purchase(
75106
self,
76107
interaction: nextcord.Interaction,
@@ -83,8 +114,9 @@ async def add_purchase(
83114
):
84115
"""Adds a new purchase into the database"""
85116

117+
# Check if category is in category DB, if title is present, and if cost is also present.
86118
# Inserting new purchase into purchase table in database
87-
query = "INSERT INTO purchases VALUES (?, ?, ?, ?, ?)"
119+
query = "INSERT INTO purchases (user_id, category, title, description, cost) VALUES (?, ?, ?, ?, ?)"
88120
self.cursor.execute(query, (interaction.user.id, category, title, description, cost, ))
89121
self.database.commit()
90122

0 commit comments

Comments
 (0)