module.exports = {} module.exports.definition = { name: 'edit', description: 'Edit a glucose reading', integration_types: [0, 1], contexts: [0, 1, 2], options: [ { name: 'id', description: 'ID of the glucose reading to edit', type: 3, // STRING required: true }, { name: 'value', description: 'New glucose reading value', type: 4, // INTEGER required: false }, { name: 'datetime', description: 'New date/time in format mm/dd/yyyy hh:MM', type: 3, // STRING required: false }, { name: 'notes', description: 'New notes for the reading', type: 3, // STRING required: false } ], } module.exports.execute = async (interaction, client, db) => { const id = interaction.options.getString('id'); if (!id) { return interaction.reply({ content: 'Please provide a valid glucose reading ID.', ephemeral: true }); } db.get(`SELECT * FROM glucose_log WHERE id = ? AND user_id = ?`, [id, interaction.user.id], async (err, row) => { if (err) { console.error('Database error:', err); return interaction.reply({ content: 'An error occurred while accessing the database.', ephemeral: true }); } if (!row) { return interaction.reply({ content: 'No glucose reading found with the provided ID.', ephemeral: true }); } const [ channel_id, message_id ] = row.msg_id.split('/'); //Get channel and msg id for update const ogMessage = await async function() { const chan = await client.channels.fetch(channel_id); if (!chan) { return null; } return await chan.messages.fetch(message_id).catch(() => null); }(); if (!ogMessage) { return interaction.reply({ content: 'Original message not found. Cannot edit reading.', ephemeral: true }); } const datetime = interaction.options.getString('datetime'); 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', ' ') || row.timestamp; const value = interaction.options.getInteger('value') || row.reading; const notes = interaction.options.getString('notes') || row.notes || ''; if (!db) { return interaction.reply({ content: 'Database connection is not available.', ephemeral: true }); } try { await db.run( `UPDATE glucose_log SET reading = ?, timestamp = ?, notes = ? WHERE id = ? AND user_id = ?`, [value, formattedDate, notes, id, interaction.user.id] ); await ogMessage.edit({ content: `[${id}] ${formattedDate}; ${value}; ${notes ? notes : ''}` }); return interaction.reply({ content: `Glucose reading with ID \`${id}\` has been updated successfully.`, ephemeral: true }); } catch (updateErr) { console.error('Update error:', updateErr); return interaction.reply({ content: 'An error occurred while updating the glucose reading.', ephemeral: true }); } }); }