Add basic receipt logging
This commit is contained in:
@@ -6,19 +6,40 @@ module.exports = {
|
||||
if (message.author.bot) return;
|
||||
|
||||
if(message.channel.id === '1462060674766344370') {
|
||||
const CURRENCY_MAP = {
|
||||
"222457277708369928": "EUR", // Minz
|
||||
"372115788498468864": "SEK", // Miffy
|
||||
};
|
||||
|
||||
let reply = "";
|
||||
let db = await message.client.localDB;
|
||||
const receiptsChannel = message.channel;
|
||||
const authorId = message.author.id;
|
||||
|
||||
let budgets = await db.prepare(`SELECT * FROM grocery_budgets WHERE discord_id = ?`).get(authorId);
|
||||
let weeklyBudget = await (await db.prepare(`SELECT weekly_budget FROM bot_config`).get()).weekly_budget;
|
||||
|
||||
console.log(weeklyBudget, budgets);
|
||||
if (budgets === undefined) {
|
||||
console.log(`No budget row in db for ${authorId}`);
|
||||
db.prepare(`INSERT OR REPLACE INTO grocery_budgets (discord_id, budget_spent, total_spent) VALUES (?,0,0)`).run(authorId);
|
||||
|
||||
|
||||
const currentBudget = db.prepare(`
|
||||
SELECT id, exchange_rate, budget_amount FROM weekly_budgets
|
||||
WHERE date('now', 'localtime') BETWEEN start_date AND end_date
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
`).get();
|
||||
|
||||
if (message.content === "fuck") {
|
||||
|
||||
let lastSpend = db.prepare(`
|
||||
SELECT id, amount, message_raw, created_at
|
||||
FROM grocery_spendings
|
||||
WHERE discord_id = ? AND budget_id = ?
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1
|
||||
`).get(authorId, currentBudget.id);
|
||||
if (lastSpend) {
|
||||
db.prepare(`DELETE FROM grocery_spendings WHERE id = ?`).run(lastSpend.id);
|
||||
await message.reply(`Your last spending of ${lastSpend.amount.toFixed(2)}€ has been deleted.`);
|
||||
} else {
|
||||
await message.reply("You don't have any entries within the current budgeting period!");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let matches = [];
|
||||
@@ -27,19 +48,56 @@ module.exports = {
|
||||
lines.forEach(line => {0
|
||||
if (line.startsWith('-')) {
|
||||
console.log(`Matching on line ${line}`);
|
||||
const regex = /-(?=[0-9])([0-9]*,?[0-9]*)/m;
|
||||
const regex = /-(?=[0-9])([0-9]*[.,]?[0-9]*)/m;
|
||||
let match = regex.exec(line)
|
||||
console.log(match);
|
||||
additionalSpent = additionalSpent + parseFloat(match[1]);
|
||||
if (match) {
|
||||
additionalSpent = additionalSpent + parseFloat(match[1].replace(',', '.'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (additionalSpent > 0) {
|
||||
db.prepare(`UPDATE grocery_budgets SET budget_spent = ?, total_spent = ? WHERE discord_id = ?`).run(budgets.budget_spent + additionalSpent, budgets.total_spent + additionalSpent, authorId);
|
||||
let remainingBudget = weeklyBudget - (budgets.budget_spent + additionalSpent);
|
||||
|
||||
reply = `${message.author.globalName} spent ${additionalSpent} of their budget.\nThey have ${remainingBudget} remaining.`;
|
||||
await receiptsChannel.send(reply);
|
||||
if (additionalSpent > 0) {
|
||||
|
||||
|
||||
const currency = CURRENCY_MAP[authorId] || "EUR"
|
||||
switch (currency) {
|
||||
case "SEK":
|
||||
additionalSpent = additionalSpent / currentBudget.exchange_rate;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
db.prepare(`
|
||||
INSERT INTO grocery_spendings (
|
||||
discord_id,
|
||||
message_id,
|
||||
message_raw,
|
||||
amount,
|
||||
budget_id
|
||||
) VALUES (?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
authorId,
|
||||
message.id,
|
||||
message.content,
|
||||
additionalSpent,
|
||||
currentBudget.id
|
||||
);
|
||||
|
||||
const allSpendings = db.prepare(`
|
||||
SELECT amount
|
||||
FROM grocery_spendings
|
||||
WHERE budget_id = ? AND discord_id = ?
|
||||
`).all(currentBudget.id, authorId);
|
||||
|
||||
const totalSpent = allSpendings.reduce((acc, entry) => {
|
||||
return acc + entry.amount;
|
||||
}, 0);
|
||||
|
||||
reply = `${message.author.globalName} spent ${additionalSpent}€ / ${(additionalSpent * currentBudget.exchange_rate).toFixed(2)} kr of their budget.\nThey have ${currentBudget.budget_amount - totalSpent}€ / ${((currentBudget.budget_amount - totalSpent) * currentBudget.exchange_rate).toFixed(2)} kr remaining.`;
|
||||
await message.reply(reply);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user