97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
const ms = require("ms");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const uuid = require("uuid").v4;
|
|
|
|
const dbFileName = path.resolve(".", "utils", "todoMP.json");
|
|
let database = [];
|
|
|
|
module.exports = {
|
|
name: "todoMP",
|
|
aliases: ["todoMP"],
|
|
utilisation: "{prefix}todo <time> <user> <message>",
|
|
|
|
async onStart(client) {
|
|
updateDatabase();
|
|
const currentTime = Date.now();
|
|
database.forEach((reminder) => {
|
|
const delay = reminder.time - currentTime;
|
|
if (delay < 0) {
|
|
setTimeout(() => {
|
|
const discordChannel = client.channels.resolve(reminder.channel);
|
|
discordChannel.send(
|
|
`La SNCF vous présente ses excuses pour le retard <@${reminder.user}> ! tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
|
|
);
|
|
updateDatabase();
|
|
database = database.filter((struct) => struct.uuid !== reminder.uuid);
|
|
saveDatabase();
|
|
}, 1000);
|
|
} else {
|
|
setTimeout(() => {
|
|
const discordChannel = client.channels.resolve(reminder.channel);
|
|
discordChannel.send(
|
|
`<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
|
|
);
|
|
updateDatabase();
|
|
database = database.filter((struct) => struct.uuid !== reminder.uuid);
|
|
saveDatabase();
|
|
}, delay);
|
|
}
|
|
});
|
|
console.log("-> ON START !!!!! ça marche putain!");
|
|
},
|
|
async execute(client, message, args) {
|
|
let reveilTime = ms(args[0]);
|
|
const user = args[1];
|
|
// all after args[1]
|
|
const reason = args.splice(2).join(" ");
|
|
//const reason = args.splice(salon == defaut ? 1 : 2).join(" ");
|
|
const reminderStruct = {
|
|
uuid: uuid(),
|
|
author: message.author.id,
|
|
time: Date.now() + reveilTime,
|
|
user: user,
|
|
message: reason,
|
|
};
|
|
|
|
setTimeout(function () {
|
|
const discordChannel = message.mentions.users.first();
|
|
discordChannel.send(
|
|
`<@${reminderStruct.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminderStruct.message}`
|
|
);
|
|
updateDatabase();
|
|
database = database.filter(
|
|
(struct) => struct.uuid !== reminderStruct.uuid
|
|
);
|
|
saveDatabase();
|
|
}, reveilTime);
|
|
|
|
//check if file todo.json content is empty
|
|
updateDatabase();
|
|
database.push(reminderStruct);
|
|
saveDatabase();
|
|
|
|
// message.react("🤌");
|
|
// message.react("👌");
|
|
message.delete().catch(() => message.react("🤌"));
|
|
|
|
},
|
|
};
|
|
|
|
const updateDatabase = () => {
|
|
if (fs.existsSync(dbFileName)) {
|
|
const fileContent = fs.readFileSync(dbFileName);
|
|
database.push(...JSON.parse(fileContent));
|
|
}
|
|
};
|
|
const saveDatabase = () => {
|
|
const foundUUIDs = [];
|
|
database = database.filter((struct) => {
|
|
if (!foundUUIDs.includes(struct.uuid)) {
|
|
foundUUIDs.push(struct.uuid);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
fs.writeFileSync(dbFileName, JSON.stringify(database, null, 2));
|
|
}; |