Add budget view command
This commit is contained in:
@@ -21,14 +21,14 @@ module.exports = {
|
|||||||
)
|
)
|
||||||
.addSubcommand(subcommand =>
|
.addSubcommand(subcommand =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName('spendings')
|
.setName('view')
|
||||||
.setDescription('View your grocery spendings for the current or previous week')
|
.setDescription('View your grocery spendings for the current or previous week')
|
||||||
.addBooleanOption(option =>
|
.addBooleanOption(option =>
|
||||||
option
|
option
|
||||||
.setName('lastweek')
|
.setName('lastweek')
|
||||||
.setDescription('Show spendings from the previous budget cycle instead of the current one')
|
.setDescription('Show spendings from the previous budget cycle instead of the current one')
|
||||||
.setRequired(false)
|
.setRequired(false)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
console.log('budget command entrypoint');
|
console.log('budget command entrypoint');
|
||||||
@@ -46,13 +46,59 @@ module.exports = {
|
|||||||
switch (interaction.options.getSubcommand()) {
|
switch (interaction.options.getSubcommand()) {
|
||||||
case 'set':
|
case 'set':
|
||||||
console.log('budget command set');
|
console.log('budget command set');
|
||||||
let budget = interaction.options.get('amount').value
|
let override = interaction.options.getBoolean('override') ?? false;
|
||||||
|
let budget = interaction.options.getB('amount').value;
|
||||||
db.prepare(`INSERT OR REPLACE INTO bot_config (id, weekly_budget) VALUES (1, ?)`).run(budget);
|
db.prepare(`INSERT OR REPLACE INTO bot_config (id, weekly_budget) VALUES (1, ?)`).run(budget);
|
||||||
await interaction.reply(`Budget set to ${budget}`);
|
if (override) {
|
||||||
|
db.prepare(`
|
||||||
|
UPDATE weekly_budgets
|
||||||
|
SET budget_amount = ?
|
||||||
|
WHERE id = (
|
||||||
|
SELECT id FROM weekly_budgets
|
||||||
|
WHERE date('now', 'localtime') BETWEEN start_date AND end_date
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT 1
|
||||||
|
)`).run(budget);
|
||||||
|
|
||||||
|
}
|
||||||
|
await interaction.reply(`Budget set to ${budget}.`);
|
||||||
break;
|
break;
|
||||||
case 'spendings':
|
case 'view':
|
||||||
console.log('budget command show');
|
const currentBudget = db.prepare(`
|
||||||
await interaction.reply('Not implemented');
|
SELECT id, budget_amount, exchange_rate, start_date, end_date
|
||||||
|
FROM weekly_budgets
|
||||||
|
WHERE date('now', 'localtime') BETWEEN start_date AND end_date
|
||||||
|
LIMIT 1
|
||||||
|
`).get();
|
||||||
|
|
||||||
|
const userSpendings = db.prepare(`
|
||||||
|
SELECT amount, message_raw
|
||||||
|
FROM grocery_spendings
|
||||||
|
WHERE budget_id = ? AND discord_id = ?
|
||||||
|
`).all(currentBudget.id, interaction.member.id);
|
||||||
|
|
||||||
|
const totalSpentEur = userSpendings.reduce((acc, s) => {
|
||||||
|
return acc + s.amount;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const remainingEur = currentBudget.budget_amount - totalSpentEur;
|
||||||
|
const remainingSek = remainingEur * currentBudget.exchange_rate;
|
||||||
|
|
||||||
|
const todayTimestamp = Math.floor(Date.now() / 1000);
|
||||||
|
const endTimestamp = Math.floor(new Date(`${currentBudget.end_date}T23:59:59`).getTime() / 1000);
|
||||||
|
|
||||||
|
const budgetEmbed = new EmbedBuilder()
|
||||||
|
.setColor(remainingEur > 0 ? 0x00ff00 : 0xff0000)
|
||||||
|
.setTitle(`${interaction.member.displayName} 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 },
|
||||||
|
{ name: 'Remaining', value: `${remainingEur.toFixed(2)} €\n${remainingSek.toFixed(2)} kr`, inline: true },
|
||||||
|
)
|
||||||
|
.setFooter({ text: `Exchange rate this week: 1 € = ${currentBudget.exchange_rate} kr` })
|
||||||
|
.setTimestamp();
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [budgetEmbed], ephemeral: true });
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
await interaction.reply(`Sub mismatch. Switching on ${interaction.options.getSubcommand()}`);
|
await interaction.reply(`Sub mismatch. Switching on ${interaction.options.getSubcommand()}`);
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ module.exports = {
|
|||||||
if (!response.ok) await channel.send(`Failed to fetch exchange rate:\n${response.statusText}`);
|
if (!response.ok) await channel.send(`Failed to fetch exchange rate:\n${response.statusText}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
//await channel.send(`Fetched new exchange rate:\n${JSON.stringify(data)}`);
|
//await channel.send(`Fetched new exchange rate:\n${JSON.stringify(data)}`);
|
||||||
await channel.send(`Reset weekly budget to ${config.weekly_budget}EUR / ${config.weekly_budget*data.rates.SEK}SEK`);
|
await channel.send(`Reset weekly budget to ${config.weekly_budget}€ / ${config.weekly_budget*data.rates.SEK} kr`);
|
||||||
|
|
||||||
|
|
||||||
db.prepare(`
|
db.prepare(`
|
||||||
|
|||||||
Reference in New Issue
Block a user