71 lines
2.4 KiB
JavaScript
71 lines
2.4 KiB
JavaScript
const { v4: uuidv4 } = require('uuid');
|
|
|
|
module.exports = {}
|
|
|
|
module.exports.definition = {
|
|
name: 'log',
|
|
description: 'Log a glucose reading',
|
|
integration_types: [0,1],
|
|
contexts: [0, 1, 2],
|
|
options: [
|
|
{
|
|
name: 'value',
|
|
description: 'Glucose reading value',
|
|
type: 4, // INTEGER
|
|
required: true
|
|
},
|
|
{
|
|
name: 'datetime',
|
|
description: 'Optional date/time in format mm/dd/yyyy hh:MM',
|
|
type: 3, // STRING
|
|
required: false
|
|
},
|
|
{
|
|
name: 'notes',
|
|
description: 'Optional notes for the reading',
|
|
type: 3, // STRING
|
|
required: false
|
|
}
|
|
],
|
|
}
|
|
|
|
module.exports.execute = async (interaction, client, db) => {
|
|
const value = interaction.options.getInteger('value');
|
|
const datetime = interaction.options.getString('datetime');
|
|
const notes = interaction.options.getString('notes') || '';
|
|
|
|
if (isNaN(value) || value < 0) {
|
|
return interaction.reply({ content: 'Please provide a valid glucose reading value.', ephemeral: true });
|
|
}
|
|
|
|
let dateTimeObj;
|
|
if (datetime) {
|
|
dateTimeObj = new Date(datetime);
|
|
if (isNaN(dateTimeObj.getTime())) {
|
|
return interaction.reply({ content: 'Invalid date/time format. Use mm/dd/yyyy hh:MM.', ephemeral: true });
|
|
}
|
|
} else {
|
|
dateTimeObj = new Date();
|
|
}
|
|
|
|
const formattedDate = dateTimeObj.toISOString().slice(0, 19).replace('T', ' ');
|
|
|
|
if (!db) {
|
|
return interaction.reply({ content: 'Database connection is not available.', ephemeral: true });
|
|
}
|
|
|
|
try {
|
|
const id = uuidv4();
|
|
await db.run(
|
|
`INSERT INTO glucose_log (id, user_id, timestamp, reading, notes) VALUES (?, ?, ?, ?, ?)`,
|
|
[id, interaction.user.id, formattedDate, value, notes]
|
|
);
|
|
await interaction.reply({ content: `Glucose reading logged: ${value} at ${formattedDate}. Notes: ${notes}`, ephemeral: true });
|
|
await interaction.user.send({ content: `[${id}] ${formattedDate}; ${value}; ${notes ? notes : ''}`, ephemeral: false }).then((msg) => {
|
|
db.run(`UPDATE glucose_log SET msg_id = ? WHERE id = ?`, [`${msg.channel.id}/${msg.id}`, id]);
|
|
});
|
|
} catch (err) {
|
|
console.error('Database error:', err);
|
|
await interaction.reply({ content: 'Failed to log glucose reading. Please try again later.', ephemeral: true });
|
|
}
|
|
} |