105 lines
4.1 KiB
JavaScript
105 lines
4.1 KiB
JavaScript
const { Events, channelLink, discordSort } = require('discord.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 = {
|
|
"222457277708369928": "EUR", // Minz
|
|
"372115788498468864": "SEK", // Miffy
|
|
};
|
|
|
|
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 (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 = [];
|
|
let lines = message.content.split("\n");
|
|
let additionalSpent = 0.0;
|
|
lines.forEach(line => {0
|
|
if (line.startsWith('-')) {
|
|
console.log(`Matching on line ${line}`);
|
|
const regex = /-(?=[0-9])([0-9]*[.,]?[0-9]*)/m;
|
|
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}€ / ${(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);
|
|
}
|
|
}
|
|
},
|
|
}; |