This commit is contained in:
Lantium 2022-04-06 23:43:27 +02:00
parent 87860b4b6b
commit 1dce385e20
2 changed files with 98 additions and 0 deletions

97
commands/core/todoMP.js Normal file
View 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
View File

@ -0,0 +1 @@
[]