TodoMP
This commit is contained in:
parent
87860b4b6b
commit
1dce385e20
97
commands/core/todoMP.js
Normal file
97
commands/core/todoMP.js
Normal file
|
@ -0,0 +1,97 @@
|
|||
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.author}> ! 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));
|
||||
};
|
1
utils/todoMP.json
Normal file
1
utils/todoMP.json
Normal file
|
@ -0,0 +1 @@
|
|||
[]
|
Loading…
Reference in New Issue
Block a user