choco-bot
Version:
Whatsapp-bot
57 lines (48 loc) • 2.19 kB
JavaScript
const fs = require('fs');
function reminder(remainingMessage, msg, client, reminders) {
const messageParts = remainingMessage.split(' ');
const chat = msg.getChat();
const chatId = chat.id;
if (messageParts.length < 2) {
const usageMessage = "Usage: `Your reminder message /time` (e.g., `Wish me a happy birthday /30`)";
client.sendMessage(chatId, usageMessage);
} else {
const reminderMessage = messageParts.slice(0, -1).join(' ');
const timeInterval = messageParts[messageParts.length - 1];
if (timeInterval.startsWith('/')) {
const timeInMinutes = parseInt(timeInterval.slice(1));
if (!isNaN(timeInMinutes)) {
// Store the reminder in the JSON array
reminders.push({
chatId: chatId,
reminderMessage: reminderMessage,
timeInMinutes: timeInMinutes,
setBy: msg.from,
});
// Save the updated reminders array to the JSON file
const remindersData = '../reminders.json';
fs.writeFileSync(remindersData, JSON.stringify(reminders, null, 2));
client.sendMessage(chatId, "*Reminder set*");
setTimeout(() => {
// Find the reminder and send it when the time is up
const reminderIndex = reminders.findIndex((r) => r.chatId === chatId && r.reminderMessage === reminderMessage);
if (reminderIndex !== -1) {
const reminder = reminders[reminderIndex];
client.sendMessage(reminder.chatId, `Reminder set by ${reminder.setBy}: ${reminder.reminderMessage}`);
// Remove the reminder from the array
reminders.splice(reminderIndex, 1);
// Update the JSON file
fs.writeFileSync(remindersData, JSON.stringify(reminders, null, 2));
}
}, timeInMinutes * 60 * 1000);
} else {
client.sendMessage(chatId, "Invalid time format. Please specify a valid time (e.g., `/30` for 30 minutes).");
}
} else {
client.sendMessage(chatId, "Please specify a valid time using `/time` (e.g., `/30` for 30 minutes).");
}
}
}
module.exports = {
reminder,
};