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

View File

@ -1,97 +1,80 @@
const ms = require("ms");
const fs = require("fs");
const path = require("path");
const uuid = require("uuid").v4;
const mongoose = require("mongoose");
const dbFileName = path.resolve(".", "utils", "todoMP.json");
let database = [];
const todoSchema = new mongoose.Schema({
uuid: String,
author: String,
time: String,
user: String,
message: String,
});
const TodoMP = mongoose.model("TodoMP", todoSchema);
module.exports = {
name: "todoMP",
aliases: ["todoMP"],
utilisation: "{prefix}todo <time> <user> <message>",
aliases: ["todoMP", "MP"],
utilisation: "{prefix}todo <time> <user or you> <message>",
async onStart(client) {
updateDatabase();
const remainingTodos = await TodoUtils.loadAll();
const currentTime = Date.now();
database.forEach((reminder) => {
remainingTodos.forEach(async (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}`
setTimeout(async () => {
const discordUserMP = client.users.resolve(reminder.user);
discordUserMP.send(
`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();
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
await TodoUtils.deleteOne(reminder.uuid);
}, 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}`
setTimeout(async () => {
const discordUserMP = client.users.resolve(reminder.user);
discordUserMP.send(
`Bonjour <@${reminder.user}>, J'ai un message de la part de <@${reminder.author}> :\n---------------------\n${reminder.message}`
);
updateDatabase();
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
await TodoUtils.deleteOne(reminder.uuid);
}, 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 = {
const defaut = message.author.id;
const userStand = args[1].replace(/[^0-9]/g, "");
const user = parseInt(userStand) ? userStand : defaut;
const reason = args.splice(user == defaut ? 1 : 2).join(" ");
const reminder = new TodoMP({
uuid: uuid(),
author: message.author.id,
time: Date.now() + reveilTime,
user: user,
message: reason,
};
});
await reminder.save();
setTimeout(function () {
const discordChannel = message.mentions.users.first();
discordChannel.send(
`<@${reminderStruct.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminderStruct.message}`
setTimeout(async function () {
const discordUserMP = client.users.resolve(reminder.user);
discordUserMP.send(
`Bonjour <@${reminder.user}>, J'ai un message de la part de <@${reminder.author}> :\n---------------------\n${reminder.message}`
);
updateDatabase();
database = database.filter(
(struct) => struct.uuid !== reminderStruct.uuid
);
saveDatabase();
await TodoUtils.deleteOne(reminder.uuid);
}, 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));
const TodoUtils = {
loadAll: async () => {
return await TodoMP.find();
},
findOne: async (uuid) => {
return await TodoMP.find({ uuid });
},
deleteOne: async (uuid) => {
return await TodoMP.deleteOne({ uuid });
},
};

View File

@ -1 +0,0 @@
[]

View File

@ -1 +0,0 @@
[]