Receipts: Add user param for budget commands
This commit is contained in:
@@ -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 <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>`)
|
||||
.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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user