Add Todo.js and TodoMP.js in MongoDB

and correction TodoMP !
This commit is contained in:
Lantium 2022-04-08 00:01:12 +02:00
parent 2129b00d1d
commit 634f6771df
4 changed files with 49 additions and 68 deletions

View File

@ -23,7 +23,7 @@ module.exports = {
remainingTodos.forEach(async reminder => { remainingTodos.forEach(async reminder => {
const delay = reminder.time - currentTime; const delay = reminder.time - currentTime;
if (delay < 0) { if (delay < 0) {
setTimeout(() => { setTimeout(async () => {
const discordChannel = client.channels.resolve(reminder.channel); const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send( 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}` `La SNCF vous présente ses excuses pour le retard <@${reminder.author}> ! tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
@ -31,7 +31,7 @@ module.exports = {
await TodoUtils.deleteOne(reminder.uuid); await TodoUtils.deleteOne(reminder.uuid);
}, 1000); }, 1000);
} else { } else {
setTimeout(() => { setTimeout(async () => {
const discordChannel = client.channels.resolve(reminder.channel); const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send( discordChannel.send(
`<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}` `<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
@ -58,7 +58,7 @@ module.exports = {
}); });
await reminder.save(); await reminder.save();
setTimeout(function () { setTimeout(async function () {
const discordChannel = client.channels.resolve(salon); const discordChannel = client.channels.resolve(salon);
discordChannel.send( discordChannel.send(
`<@${message.author.id}>, tu m'as demandé de te rappeler\n---------------------\n${reason}` `<@${message.author.id}>, tu m'as demandé de te rappeler\n---------------------\n${reason}`

View File

@ -1,97 +1,80 @@
const ms = require("ms"); const ms = require("ms");
const fs = require("fs");
const path = require("path");
const uuid = require("uuid").v4; const uuid = require("uuid").v4;
const mongoose = require("mongoose");
const dbFileName = path.resolve(".", "utils", "todoMP.json"); const todoSchema = new mongoose.Schema({
let database = []; uuid: String,
author: String,
time: String,
user: String,
message: String,
});
const TodoMP = mongoose.model("TodoMP", todoSchema);
module.exports = { module.exports = {
name: "todoMP", name: "todoMP",
aliases: ["todoMP"], aliases: ["todoMP", "MP"],
utilisation: "{prefix}todo <time> <user> <message>", utilisation: "{prefix}todo <time> <user or you> <message>",
async onStart(client) { async onStart(client) {
updateDatabase(); const remainingTodos = await TodoUtils.loadAll();
const currentTime = Date.now(); const currentTime = Date.now();
database.forEach((reminder) => { remainingTodos.forEach(async (reminder) => {
const delay = reminder.time - currentTime; const delay = reminder.time - currentTime;
if (delay < 0) { if (delay < 0) {
setTimeout(() => { setTimeout(async () => {
const discordChannel = client.channels.resolve(reminder.channel); const discordUserMP = client.users.resolve(reminder.user);
discordChannel.send( discordUserMP.send(
`La SNCF vous présente ses excuses pour le retard <@${reminder.user}> ! tu m'as demandé de te rappeler\n---------------------\n${reminder.message}` `Bonjour <@${reminder.user}>, La SNCF vous présente ses excuses pour le retard !\nJ'ai un message de la part de <@${reminder.author}> :\n---------------------\n${reminder.message}`
); );
updateDatabase(); await TodoUtils.deleteOne(reminder.uuid);
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
}, 1000); }, 1000);
} else { } else {
setTimeout(() => { setTimeout(async () => {
const discordChannel = client.channels.resolve(reminder.channel); const discordUserMP = client.users.resolve(reminder.user);
discordChannel.send( discordUserMP.send(
`<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}` `Bonjour <@${reminder.user}>, J'ai un message de la part de <@${reminder.author}> :\n---------------------\n${reminder.message}`
); );
updateDatabase(); await TodoUtils.deleteOne(reminder.uuid);
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
}, delay); }, delay);
} }
}); });
console.log("-> ON START !!!!! ça marche putain!");
}, },
async execute(client, message, args) { async execute(client, message, args) {
let reveilTime = ms(args[0]); let reveilTime = ms(args[0]);
const user = args[1]; const defaut = message.author.id;
// all after args[1] const userStand = args[1].replace(/[^0-9]/g, "");
const reason = args.splice(2).join(" "); const user = parseInt(userStand) ? userStand : defaut;
//const reason = args.splice(salon == defaut ? 1 : 2).join(" "); const reason = args.splice(user == defaut ? 1 : 2).join(" ");
const reminderStruct = {
const reminder = new TodoMP({
uuid: uuid(), uuid: uuid(),
author: message.author.id, author: message.author.id,
time: Date.now() + reveilTime, time: Date.now() + reveilTime,
user: user, user: user,
message: reason, message: reason,
}; });
await reminder.save();
setTimeout(function () { setTimeout(async function () {
const discordChannel = message.mentions.users.first(); const discordUserMP = client.users.resolve(reminder.user);
discordChannel.send( discordUserMP.send(
`<@${reminderStruct.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminderStruct.message}` `Bonjour <@${reminder.user}>, J'ai un message de la part de <@${reminder.author}> :\n---------------------\n${reminder.message}`
); );
updateDatabase(); await TodoUtils.deleteOne(reminder.uuid);
database = database.filter(
(struct) => struct.uuid !== reminderStruct.uuid
);
saveDatabase();
}, reveilTime); }, reveilTime);
//check if file todo.json content is empty
updateDatabase();
database.push(reminderStruct);
saveDatabase();
// message.react("🤌");
// message.react("👌");
message.delete().catch(() => message.react("🤌")); message.delete().catch(() => message.react("🤌"));
}, },
}; };
const updateDatabase = () => { const TodoUtils = {
if (fs.existsSync(dbFileName)) { loadAll: async () => {
const fileContent = fs.readFileSync(dbFileName); return await TodoMP.find();
database.push(...JSON.parse(fileContent)); },
} findOne: async (uuid) => {
}; return await TodoMP.find({ uuid });
const saveDatabase = () => { },
const foundUUIDs = []; deleteOne: async (uuid) => {
database = database.filter((struct) => { return await TodoMP.deleteOne({ uuid });
if (!foundUUIDs.includes(struct.uuid)) { },
foundUUIDs.push(struct.uuid);
return true;
}
return false;
});
fs.writeFileSync(dbFileName, JSON.stringify(database, null, 2));
}; };

View File

@ -1 +0,0 @@
[]

View File

@ -1 +0,0 @@
[]