Files
general-purpose-bot/events/receiptsMessageCreate.js

46 lines
2.0 KiB
JavaScript

const { Events, channelLink, discordSort } = require('discord.js');
module.exports = {
name: Events.MessageCreate,
async execute(message) {
if (message.author.bot) return;
if(message.channel.id === '1462060674766344370') {
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);
}
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);
additionalSpent = additionalSpent + parseFloat(match[1]);
}
});
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);
}
}
},
};