const { Events, channelLink, discordSort } = require('discord.js'); const { HOUSEHOLD_IDS } = require('../core/constants.js'); module.exports = { name: Events.MessageCreate, async execute(message) { if (message.author.bot) return; const receiptsChannelId = process.env.NODE_ENV === 'development' ? '1468186493251227658' : '1462060674766344370'; if(message.channel.id === receiptsChannelId) { const CURRENCY_MAP = { [HOUSEHOLD_IDS.MINZ]: "EUR", [HOUSEHOLD_IDS.MIFFY]: "SEK", }; let reply = ""; let db = await message.client.localDB; const receiptsChannel = message.channel; const authorId = message.author.id; 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 (!currentBudget) { await message.reply("No active budget period found for this week yet - try again in a moment."); return; } if (message.content === "fuck") { let lastSpend = db.prepare(` SELECT id, amount, message_raw, created_at FROM grocery_spendings WHERE discord_id = ? AND budget_id = ? AND is_transfer = 0 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; } const transferRegex = /^transfer\s+(\d+(?:[.,]\d+)?)$/i; const transferMatch = message.content.match(transferRegex); if (transferMatch) { if (authorId !== HOUSEHOLD_IDS.MINZ && authorId !== HOUSEHOLD_IDS.MIFFY) { await message.reply("Only Minz and Miffy can use the transfer command."); return; } let amount = parseFloat(transferMatch[1].replace(',', '.')); const targetId = authorId === HOUSEHOLD_IDS.MINZ ? HOUSEHOLD_IDS.MIFFY : HOUSEHOLD_IDS.MINZ; const insertSpending = db.prepare(` INSERT INTO grocery_spendings (discord_id, message_id, message_raw, amount, budget_id, is_transfer) VALUES (?, ?, ?, ?, ?, 1) `); const runTransfer = db.transaction(() => { insertSpending.run(authorId, message.id, message.content, amount, currentBudget.id); insertSpending.run(targetId, message.id, message.content, -amount, currentBudget.id); }); runTransfer(); const targetName = authorId === HOUSEHOLD_IDS.MINZ ? "Miffy" : "Minz"; await message.reply(`Transferred ${amount.toFixed(2)}€ to ${targetName}.`); return; } let lines = message.content.split("\n"); let additionalSpent = 0.0; lines.forEach(line => { if (line.startsWith('-')) { console.log(`Matching on line ${line}`); const regex = /-\s*([0-9]+(?:[.,][0-9]+)?)/; let match = regex.exec(line) console.log(match); if (match) { additionalSpent = additionalSpent + parseFloat(match[1].replace(',', '.')); } } }); 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.toFixed(2)}€ / ${(additionalSpent * currentBudget.exchange_rate).toFixed(2)} kr of their budget.\nThey have ${(currentBudget.budget_amount - totalSpent).toFixed(2)}€ / ${((currentBudget.budget_amount - totalSpent) * currentBudget.exchange_rate).toFixed(2)} kr remaining.`; await message.reply(reply); } } }, };