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

83 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,
channel: String,
guild: String,
message: String,
});
const Todo = mongoose.model("Todo", todoSchema);
module.exports = {
name: "todo",
aliases: ["todo"],
utilisation: "{prefix}todo <time> <salon> <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 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}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, 1000);
} else {
setTimeout(async () => {
const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send(
`<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, delay);
}
});
},
async execute(client, message, args) {
let reveilTime = ms(args[0]);
const defaut = message.channel.id;
const channel = args[1].replace(/[^0-9]/g, "");
let salon = parseInt(channel) ? channel : defaut;
const reason = args.splice(salon == defaut ? 1 : 2).join(" ");
const reminder = new Todo({
uuid: uuid(),
author: message.author.id,
time: Date.now() + reveilTime,
channel: salon,
guild: message.guild.id,
message: reason,
});
await reminder.save();
setTimeout(async function () {
const discordChannel = client.channels.resolve(salon);
discordChannel.send(
`<@${message.author.id}>, tu m'as demandé de te rappeler\n---------------------\n${reason}`
);
await TodoUtils.deleteOne(reminder.uuid);
}, reveilTime);
message.delete().catch(() => message.react("🤌"));
},
};
const TodoUtils = {
loadAll: async () => {
return await Todo.find();
},
findOne: async (uuid) => {
return await Todo.find({uuid});
},
deleteOne: async (uuid) => {
return await Todo.deleteOne({uuid});
},
};