-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteCommand.js
More file actions
60 lines (51 loc) · 1.88 KB
/
Copy pathdeleteCommand.js
File metadata and controls
60 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// deletecommand.js
const fs = require('fs');
const path = require('path');
module.exports = {
name: 'delete',
description: 'Delete the last journal entry',
execute(message, bot) {
const userId = message.author.id;
const userJournalPath = path.join(__dirname, 'journals', `${userId}.json`);
fs.readFile(userJournalPath, (err, data) => {
if (err) {
console.error(err);
message.channel.send('An error occurred while accessing your journal.');
return;
}
const journalData = JSON.parse(data);
if (journalData.length === 0) {
message.channel.send('No journal entries found to delete.');
return;
}
const lastEntryIndex = journalData.length - 1;
const lastEntry = journalData[lastEntryIndex];
const journalEmbed = {
color: 0x0099ff,
title: 'Last Journal Entry',
description: `Date: ${lastEntry.date}\nTime: ${lastEntry.time}\n\n${lastEntry.content}`,
footer: {
text: 'React with ✅ to delete this entry',
},
};
message.channel.send({ embeds: [journalEmbed] })
.then((sentMessage) => {
sentMessage.react('✅');
const filter = (reaction, user) => reaction.emoji.name === '✅' && user.id === userId;
const collector = sentMessage.createReactionCollector({ filter, max: 1 });
collector.on('collect', () => {
journalData.pop();
fs.writeFile(userJournalPath, JSON.stringify(journalData), (err) => {
if (err) {
console.error(err);
message.channel.send('An error occurred while deleting the journal entry.');
} else {
console.log(`Journal entry deleted for user: ${userId}`);
message.channel.send('Journal entry deleted.');
}
});
});
});
});
},
};