Lanbot_Music/commands/core/todoMP.js
2022-04-08 00:01:12 +02:00

80 lines
2.5 KiB
JavaScript

const ms = require("ms");
const uuid = require("uuid").v4;
const mongoose = require("mongoose");
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", "MP"],
utilisation: "{prefix}todo <time> <user or you> <message>",
async onStart(client) {
const remainingTodos = await TodoUtils.loadAll();
const currentTime = Date.now();
remainingTodos.forEach(async (reminder) => {
const delay = reminder.time - currentTime;
if (delay < 0) {
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}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, 1000);
} else {
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}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, delay);
}
});
},
async execute(client, message, args) {
let reveilTime = ms(args[0]);
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(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}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, reveilTime);
message.delete().catch(() => message.react("🤌"));
},
};
const TodoUtils = {
loadAll: async () => {
return await TodoMP.find();
},
findOne: async (uuid) => {
return await TodoMP.find({ uuid });
},
deleteOne: async (uuid) => {
return await TodoMP.deleteOne({ uuid });
},
};