Receipts: Add user param for budget commands

This commit is contained in:
2026-02-03 12:21:16 +01:00
parent fb3f659831
commit efedea8c45

View File

@@ -23,6 +23,10 @@ module.exports = {
subcommand subcommand
.setName('view') .setName('view')
.setDescription('View your grocery spendings for the current or previous week') .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 => .addBooleanOption(option =>
option option
.setName('lastweek') .setName('lastweek')
@@ -34,10 +38,15 @@ module.exports = {
subcommand subcommand
.setName('stats') .setName('stats')
.setDescription('View detailed spending and savings statistics') .setDescription('View detailed spending and savings statistics')
.addUserOption(option =>
option.setName('user')
.setRequired(false)
.setDescription('User to view stats for'))
), ),
async execute(interaction) { async execute(interaction) {
console.log('budget command entrypoint'); console.log('budget command entrypoint');
let db = await interaction.client.localDB; let db = await interaction.client.localDB;
let targetUser = interaction.options.getUser('user') ?? interaction.user;
let discordId = interaction.member.id; let discordId = interaction.member.id;
console.log(db, discordId); console.log(db, discordId);
let allowed = ['372115788498468864', '222457277708369928'].includes(discordId); let allowed = ['372115788498468864', '222457277708369928'].includes(discordId);
@@ -66,21 +75,28 @@ module.exports = {
)`).run(budget); )`).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; break;
case 'view': case 'view':
const lastWeek = interaction.options.getBoolean('lastweek') ?? false;
const currentBudget = db.prepare(` const currentBudget = db.prepare(`
SELECT id, budget_amount, exchange_rate, start_date, end_date SELECT id, budget_amount, exchange_rate, start_date, end_date
FROM weekly_budgets 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 LIMIT 1
`).get(); `).get();
if (!currentBudget) {
await interaction.reply("No budget period found.");
return;
}
const userSpendings = db.prepare(` const userSpendings = db.prepare(`
SELECT amount, message_raw SELECT amount, message_raw
FROM grocery_spendings FROM grocery_spendings
WHERE budget_id = ? AND discord_id = ? WHERE budget_id = ? AND discord_id = ?
`).all(currentBudget.id, interaction.member.id); `).all(currentBudget.id, targetUser.id);
const totalSpentEur = userSpendings.reduce((acc, s) => { const totalSpentEur = userSpendings.reduce((acc, s) => {
return acc + s.amount; return acc + s.amount;
@@ -94,7 +110,7 @@ module.exports = {
const budgetEmbed = new EmbedBuilder() const budgetEmbed = new EmbedBuilder()
.setColor(remainingEur > 0 ? 0x00ff00 : 0xff0000) .setColor(remainingEur > 0 ? 0x00ff00 : 0xff0000)
.setTitle(`${interaction.member.displayName} Budget as of <t:${todayTimestamp}:F>`) .setTitle(`${targetUser.globalName ?? targetUser.username} Budget as of <t:${todayTimestamp}:F>`)
.setDescription(`Period: \`${currentBudget.start_date}\` to \`${currentBudget.end_date}\`\nReset <t:${endTimestamp}:R>`) .setDescription(`Period: \`${currentBudget.start_date}\` to \`${currentBudget.end_date}\`\nReset <t:${endTimestamp}:R>`)
.addFields( .addFields(
{ name: 'Spent', value: `${totalSpentEur.toFixed(2)}\n${(totalSpentEur * currentBudget.exchange_rate).toFixed(2)} kr`, inline: true }, { name: 'Spent', value: `${totalSpentEur.toFixed(2)}\n${(totalSpentEur * currentBudget.exchange_rate).toFixed(2)} kr`, inline: true },
@@ -121,7 +137,7 @@ module.exports = {
SELECT amount SELECT amount
FROM grocery_spendings FROM grocery_spendings
WHERE budget_id = ? AND discord_id = ? 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 totalSpentInPeriodEur = periodSpendings.reduce((acc, s) => acc + s.amount, 0);
const savedInPeriodEur = budgetPeriod.budget_amount - totalSpentInPeriodEur; const savedInPeriodEur = budgetPeriod.budget_amount - totalSpentInPeriodEur;
@@ -140,7 +156,7 @@ module.exports = {
const statsEmbed = new EmbedBuilder() const statsEmbed = new EmbedBuilder()
.setColor(0x0099ff) .setColor(0x0099ff)
.setTitle(`${interaction.member.displayName}'s Budget Statistics`) .setTitle(`${targetUser.globalName ?? targetUser.username}'s Budget Statistics`)
.setDescription(statsDescription) .setDescription(statsDescription)
.setTimestamp(); .setTimestamp();