Lanbot_Music/commands/core/todoMP.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-04-06 23:43:27 +02:00
const ms = require("ms");
const uuid = require("uuid").v4;
const mongoose = require("mongoose");
2022-04-06 23:43:27 +02:00
const todoSchema = new mongoose.Schema({
uuid: String,
author: String,
time: String,
user: String,
message: String,
});
const TodoMP = mongoose.model("TodoMP", todoSchema);
2022-04-06 23:43:27 +02:00
module.exports = {
name: "todoMP",
aliases: ["todoMP", "MP"],
utilisation: "{prefix}todo <time> <user or you> <message>",
2022-04-06 23:43:27 +02:00
async onStart(client) {
const remainingTodos = await TodoUtils.loadAll();
2022-04-06 23:43:27 +02:00
const currentTime = Date.now();
remainingTodos.forEach(async (reminder) => {
2022-04-06 23:43:27 +02:00
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}`
2022-04-06 23:43:27 +02:00
);
await TodoUtils.deleteOne(reminder.uuid);
2022-04-06 23:43:27 +02:00
}, 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}`
2022-04-06 23:43:27 +02:00
);
await TodoUtils.deleteOne(reminder.uuid);
2022-04-06 23:43:27 +02:00
}, 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({
2022-04-06 23:43:27 +02:00
uuid: uuid(),
author: message.author.id,
time: Date.now() + reveilTime,
user: user,
message: reason,
});
await reminder.save();
2022-04-06 23:43:27 +02:00
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}`
2022-04-06 23:43:27 +02:00
);
await TodoUtils.deleteOne(reminder.uuid);
2022-04-06 23:43:27 +02:00
}, 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 });
},
2022-04-06 23:43:27 +02:00
};