From efedea8c454963e025a9fcb13cf1671569685ee6 Mon Sep 17 00:00:00 2001 From: Minz Date: Tue, 3 Feb 2026 12:21:16 +0100 Subject: [PATCH] Receipts: Add user param for budget commands --- commands/receipts/receiptCommands.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/commands/receipts/receiptCommands.js b/commands/receipts/receiptCommands.js index 5fd986d..c7f3cba 100644 --- a/commands/receipts/receiptCommands.js +++ b/commands/receipts/receiptCommands.js @@ -23,6 +23,10 @@ module.exports = { subcommand .setName('view') .setDescription('View your grocery spendings for the current or previous week') + .addUserOption(option => + option.setName('user') + .setRequired(false) + .setDescription('User to view budget for')) .addBooleanOption(option => option .setName('lastweek') @@ -34,10 +38,15 @@ module.exports = { subcommand .setName('stats') .setDescription('View detailed spending and savings statistics') + .addUserOption(option => + option.setName('user') + .setRequired(false) + .setDescription('User to view stats for')) ), async execute(interaction) { console.log('budget command entrypoint'); let db = await interaction.client.localDB; + let targetUser = interaction.options.getUser('user') ?? interaction.user; let discordId = interaction.member.id; console.log(db, discordId); let allowed = ['372115788498468864', '222457277708369928'].includes(discordId); @@ -66,21 +75,28 @@ module.exports = { )`).run(budget); } - await interaction.reply(`Budget set to ${Number(budget).toFixed(2)}.`); + await interaction.reply(`Budget set for ${targetUser.username} to ${Number(budget).toFixed(2)}.`); break; case 'view': + const lastWeek = interaction.options.getBoolean('lastweek') ?? false; const currentBudget = db.prepare(` SELECT id, budget_amount, exchange_rate, start_date, end_date FROM weekly_budgets - WHERE date('now', 'localtime') BETWEEN start_date AND end_date + WHERE ${lastWeek ? "start_date < date('now', 'localtime')" : "date('now', 'localtime') BETWEEN start_date AND end_date"} + ORDER BY start_date DESC LIMIT 1 `).get(); + if (!currentBudget) { + await interaction.reply("No budget period found."); + return; + } + const userSpendings = db.prepare(` SELECT amount, message_raw FROM grocery_spendings WHERE budget_id = ? AND discord_id = ? - `).all(currentBudget.id, interaction.member.id); + `).all(currentBudget.id, targetUser.id); const totalSpentEur = userSpendings.reduce((acc, s) => { return acc + s.amount; @@ -94,7 +110,7 @@ module.exports = { const budgetEmbed = new EmbedBuilder() .setColor(remainingEur > 0 ? 0x00ff00 : 0xff0000) - .setTitle(`${interaction.member.displayName} Budget as of `) + .setTitle(`${targetUser.globalName ?? targetUser.username} Budget as of `) .setDescription(`Period: \`${currentBudget.start_date}\` to \`${currentBudget.end_date}\`\nReset `) .addFields( { name: 'Spent', value: `${totalSpentEur.toFixed(2)} €\n${(totalSpentEur * currentBudget.exchange_rate).toFixed(2)} kr`, inline: true }, @@ -121,7 +137,7 @@ module.exports = { SELECT amount FROM grocery_spendings WHERE budget_id = ? AND discord_id = ? - `).all(budgetPeriod.id, interaction.member.id); + `).all(budgetPeriod.id, targetUser.id); const totalSpentInPeriodEur = periodSpendings.reduce((acc, s) => acc + s.amount, 0); const savedInPeriodEur = budgetPeriod.budget_amount - totalSpentInPeriodEur; @@ -140,7 +156,7 @@ module.exports = { const statsEmbed = new EmbedBuilder() .setColor(0x0099ff) - .setTitle(`${interaction.member.displayName}'s Budget Statistics`) + .setTitle(`${targetUser.globalName ?? targetUser.username}'s Budget Statistics`) .setDescription(statsDescription) .setTimestamp();