WIP: receipt feature implementation
This commit is contained in:
52
commands/utility/receiptCommands.js
Normal file
52
commands/utility/receiptCommands.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
category: 'utility',
|
||||||
|
global: true,
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName('budget')
|
||||||
|
.setDescription('The thing')
|
||||||
|
.addSubcommand(subcommand =>
|
||||||
|
subcommand
|
||||||
|
.setName('set')
|
||||||
|
.setDescription('Set weekly budget')
|
||||||
|
.addIntegerOption(option =>
|
||||||
|
option.setName('amount')
|
||||||
|
.setRequired(true)
|
||||||
|
.setDescription('Amount as int'))
|
||||||
|
)
|
||||||
|
.addSubcommand(subcommand =>
|
||||||
|
subcommand
|
||||||
|
.setName('show')
|
||||||
|
.setDescription('not implemented')
|
||||||
|
),
|
||||||
|
async execute(interaction) {
|
||||||
|
console.log('budget command entrypoint');
|
||||||
|
let db = await interaction.client.localDB;
|
||||||
|
let discordId = interaction.member.id;
|
||||||
|
console.log(db, discordId);
|
||||||
|
let allowed = ['372115788498468864', '222457277708369928'].includes(discordId);
|
||||||
|
console.log(allowed);
|
||||||
|
if (!allowed) {
|
||||||
|
console.log('budget command denied');
|
||||||
|
await interaction.reply('This command may only be invoked by Miffy!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(`Switcching on sub ${interaction.options.getSubcommand()}`);
|
||||||
|
switch (interaction.options.getSubcommand()) {
|
||||||
|
case 'set':
|
||||||
|
console.log('budget command set');
|
||||||
|
let budget = interaction.options.get('amount').value
|
||||||
|
db.prepare(`INSERT OR REPLACE INTO bot_config (id, weekly_budget) VALUES (1, ?)`).run(budget);
|
||||||
|
await interaction.reply(`Budget set to ${budget}`);
|
||||||
|
break;
|
||||||
|
case 'show':
|
||||||
|
console.log('budget command show');
|
||||||
|
await interaction.reply('Not implemented');
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
await interaction.reply(`Sub mismatch. Switching on ${interaction.options.getSubcommand()}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
22
core/db.js
22
core/db.js
@@ -34,6 +34,28 @@ async function initDB(db) {
|
|||||||
lastfm_name TEXT
|
lastfm_name TEXT
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
await db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS bot_config (
|
||||||
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||||
|
weekly_budget REAL DEFAULT 0,
|
||||||
|
exchange_rate_eur_kr REAL DEFAULT 0,
|
||||||
|
last_budget_notification_date TEXT
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
await db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS grocery_budgets (
|
||||||
|
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
discord_id VARCHAR(50) NOT NULL,
|
||||||
|
budget_spent REAL,
|
||||||
|
total_spent REAL
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Optional: Initialize the row if it doesn't exist yet
|
||||||
|
await db.exec(`
|
||||||
|
INSERT OR IGNORE INTO bot_config (id, weekly_budget, last_budget_notification_date)
|
||||||
|
VALUES (1, 0, NULL);
|
||||||
|
`);
|
||||||
console.log('[DATABASE] Created new DB table');
|
console.log('[DATABASE] Created new DB table');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,14 +36,14 @@ const rest = new REST().setToken(token);
|
|||||||
// and deploy your commands!
|
// and deploy your commands!
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
console.log(`[GUILD] Started refreshing ${guildCommands.length} application (/) commands.`);
|
console.log(`[GUILD] Started refreshing ${guildCommands.length} Guild (/) commands.`);
|
||||||
|
|
||||||
let data = await rest.put(
|
let data = await rest.put(
|
||||||
Routes.applicationGuildCommands(clientId, guildId),
|
Routes.applicationGuildCommands(clientId, guildId),
|
||||||
{ body: guildCommands },
|
{ body: guildCommands },
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(`[GUILD] Successfully reloaded ${data.length} application (/) commands.`);
|
console.log(`[GUILD] Successfully reloaded ${data.length} Guild (/) commands.`);
|
||||||
|
|
||||||
console.log(`[GLOBAL] Started refreshing ${globalCommands.length} application (/) commands.`);
|
console.log(`[GLOBAL] Started refreshing ${globalCommands.length} application (/) commands.`);
|
||||||
|
|
||||||
|
|||||||
46
events/receiptsMessageCreate.js
Normal file
46
events/receiptsMessageCreate.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -17,17 +17,29 @@ module.exports = {
|
|||||||
year: 'numeric'
|
year: 'numeric'
|
||||||
});
|
});
|
||||||
const channel = await client.channels.fetch(timer.data.channelId);
|
const channel = await client.channels.fetch(timer.data.channelId);
|
||||||
if (channel) await channel.send(`Today is ${currentDate}`);
|
await channel.send(`${currentDate}`);
|
||||||
|
if (currentDate.startsWith('Sunday')) {
|
||||||
|
const response = await fetch(`https://api.frankfurter.dev/v1/latest?amount=1&from=EUR&to=SEK`);
|
||||||
|
|
||||||
|
if (!response.ok) await channel.send(`Failed to fetch exchange rate:\n${response.statusText}`);
|
||||||
|
let db = await client.localDB;
|
||||||
|
const data = await response.json();
|
||||||
|
db.prepare(`UPDATE bot_config SET exchange_rate_eur_kr = ? WHERE id = 1`).run(data.rates.SEK);
|
||||||
|
let weeklyBudget = await (await db.prepare(`SELECT weekly_budget FROM bot_config`).get()).weekly_budget;
|
||||||
|
await channel.send(`Fetched new exchange rate:\n${JSON.stringify(data)}`);
|
||||||
|
db.prepare(`UPDATE grocery_budgets SET budget_spent = 0`).run();
|
||||||
|
await channel.send(`Reset weekly budget to ${weeklyBudget}EUR / ${weeklyBudget*data.rates.SEK}SEK`);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[TIMER] Error:', error);
|
console.error('[TIMER] Error:', error);
|
||||||
} finally {
|
} finally {
|
||||||
// 2. Schedule the NEXT tick manually
|
// 2. Schedule the NEXT tick manually
|
||||||
this.scheduleNext(client, timer);
|
await this.scheduleNext(client, timer);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
scheduleNext(client, timer) {
|
async scheduleNext(client, timer) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const next = new Date();
|
const next = new Date();
|
||||||
|
|
||||||
@@ -45,6 +57,8 @@ module.exports = {
|
|||||||
this.tick(client, timer);
|
this.tick(client, timer);
|
||||||
}, delay);
|
}, delay);
|
||||||
|
|
||||||
|
const channel = await client.channels.fetch(timer.data.channelId);
|
||||||
|
if (channel) await channel.send(`Next message scheduled for: ${next.toLocaleString()} with ms delta of ${delay} / ${delay/3600000}`);
|
||||||
console.log(`[TIMER] Next message scheduled for: ${next.toLocaleString()}`);
|
console.log(`[TIMER] Next message scheduled for: ${next.toLocaleString()}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user