-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournalCommand.js
More file actions
162 lines (142 loc) · 5.09 KB
/
journalCommand.js
File metadata and controls
162 lines (142 loc) · 5.09 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// journalCommand.js
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
module.exports = {
name: "journal",
description: "Enter today's journal entry",
// Check if there is additional text after the command
async execute(message) {
const userId = message.author.id;
const userJournalPath = path.join(__dirname, "journals", `${userId}.json`);
const journalEntry = message.content.slice("!journal".length).trim();
if (!journalEntry) {
message.channel.send(
"Please provide your journal entry after the command."
);
return;
}
// Store the journal entry in a JSON file
const entryData = {
date: new Date().toLocaleDateString(),
time: new Date().toLocaleTimeString(),
content: journalEntry,
};
let journalData = [];
fs.readFile(userJournalPath, (err, data) => {
if (err) {
if (err.code === "ENOENT") {
// Create a new journal file if it doesn't exist
fs.writeFile(userJournalPath, JSON.stringify([entryData]), (err) => {
if (err) {
console.error(err);
} else {
console.log(`Journal entry saved for user: ${userId}`);
message.channel.send("Journal entry saved.");
}
});
} else {
console.error(err);
}
} else {
// Read existing journal data from the file
journalData = JSON.parse(data);
journalData.push(entryData);
}
fs.writeFile(userJournalPath, JSON.stringify(journalData), (err) => {
if (err) {
console.error(err);
} else {
console.log(`Journal entry saved for user: ${userId}`);
message.channel.send("Journal entry saved.");
// Ask the user if they want to perform grammar check
message.channel.send(
"Do you want to perform a grammar check on your journal entry? (yes/no)"
);
// Create a message collector to listen for the user's response
const filter = (m) => m.author.id === message.author.id;
const collector = message.channel.createMessageCollector(filter, {
max: 1,
});
collector.on("collect", (collected) => {
const response = collected.content.toLowerCase();
if (response === "yes") {
// Trigger the grammar check
grammarCheck(entryData.content, message);
}
});
}
});
});
},
};
async function grammarCheck(journalEntry, message) {
const url = "https://textgears-textgears-v1.p.rapidapi.com/detect";
const encodedParams = new URLSearchParams();
encodedParams.set("text", journalEntry);
const options = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"X-RapidAPI-Key": process.env.RAPIDAPI_KEY,
"X-RapidAPI-Host": "textgears-textgears-v1.p.rapidapi.com",
},
body: encodedParams,
};
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result);
// Check if grammar errors are detected
if (result.errors && result.errors.length > 0) {
// Grammar errors detected
const correctedText = result.errors.reduce((text, error) => {
return text.replace(error.bad, error.better);
}, journalEntry);
// Send the corrected text to the user
message.channel.send(`Corrected Text: \n\`\`\`${correctedText}\`\`\``);
// Ask the user to save the corrected text
message.channel.send("Do you want to save this corrected text? (yes/no)");
// Create a message collector to listen for the user's response
const filter = (m) => m.author.id === message.author.id;
const collector = message.channel.createMessageCollector(filter, {
max: 1,
});
collector.on("collect", (collected) => {
const response = collected.content.toLowerCase();
if (response === "yes") {
// Save the corrected text
const correctedEntryData = {
date: new Date().toLocaleDateString(),
time: new Date().toLocaleTimeString(),
content: correctedText,
};
fs.readFile(userJournalPath, (err, data) => {
if (err) {
console.error(err);
return;
}
const journalData = JSON.parse(data);
const lastIndex = journalData.length - 1;
if (lastIndex >= 0) {
// Replace the last entry with the corrected text
journalData[lastIndex] = correctedEntryData;
}
fs.writeFile(userJournalPath, JSON.stringify(journalData), (err) => {
if (err) {
console.error(err);
} else {
console.log("Corrected journal entry saved.");
}
});
});
}
});
} else {
// No grammar errors found
message.channel.send("No grammatical errors found in your journal entry.");
}
} catch (error) {
console.error(error);
}
}