Lanbot_Music/commands/core/todo.js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-04-03 04:34:37 +02:00
const ms = require("ms");
const uuid = require("uuid").v4;
2022-04-07 22:55:32 +02:00
const mongoose = require("mongoose");
2022-04-03 04:34:37 +02:00
2022-04-07 22:55:32 +02:00
const todoSchema = new mongoose.Schema({
uuid: String,
author: String,
time: String,
channel: String,
guild: String,
message: String,
});
const Todo = mongoose.model("Todo", todoSchema);
2022-04-03 04:34:37 +02:00
module.exports = {
name: "todo",
aliases: ["todo"],
utilisation: "{prefix}todo <time> <salon> <message>",
async onStart(client) {
2022-04-07 22:55:32 +02:00
const remainingTodos = await TodoUtils.loadAll();
2022-04-03 04:34:37 +02:00
const currentTime = Date.now();
2022-04-07 22:55:32 +02:00
remainingTodos.forEach(async reminder => {
2022-04-03 04:34:37 +02:00
const delay = reminder.time - currentTime;
if (delay < 0) {
setTimeout(async () => {
2022-04-03 04:34:37 +02:00
const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send(
2022-04-06 23:10:42 +02:00
`La SNCF vous présente ses excuses pour le retard <@${reminder.author}> ! tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
2022-04-03 04:34:37 +02:00
);
2022-04-07 22:55:32 +02:00
await TodoUtils.deleteOne(reminder.uuid);
2022-04-03 04:34:37 +02:00
}, 1000);
} else {
setTimeout(async () => {
2022-04-03 04:34:37 +02:00
const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send(
2022-04-06 23:10:42 +02:00
`<@${reminder.author}>, tu m'as demandé de te rappeler\n---------------------\n${reminder.message}`
2022-04-03 04:34:37 +02:00
);
2022-04-07 22:55:32 +02:00
await TodoUtils.deleteOne(reminder.uuid);
2022-04-03 04:34:37 +02:00
}, 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(" ");
2022-04-07 22:55:32 +02:00
const reminder = new Todo({
2022-04-03 04:34:37 +02:00
uuid: uuid(),
author: message.author.id,
time: Date.now() + reveilTime,
channel: salon,
guild: message.guild.id,
message: reason,
2022-04-07 22:55:32 +02:00
});
await reminder.save();
2022-04-03 04:34:37 +02:00
setTimeout(async function () {
2022-04-07 22:55:32 +02:00
const discordChannel = client.channels.resolve(salon);
2022-04-03 04:34:37 +02:00
discordChannel.send(
2022-04-07 22:55:32 +02:00
`<@${message.author.id}>, tu m'as demandé de te rappeler\n---------------------\n${reason}`
2022-04-03 04:34:37 +02:00
);
2022-04-07 22:55:32 +02:00
await TodoUtils.deleteOne(reminder.uuid);
2022-04-03 04:34:37 +02:00
}, reveilTime);
2022-04-03 20:30:52 +02:00
message.delete().catch(() => message.react("🤌"));
2022-04-03 04:34:37 +02:00
},
};
2022-04-07 22:55:32 +02:00
const TodoUtils = {
loadAll: async () => {
return await Todo.find();
},
findOne: async (uuid) => {
return await Todo.find({uuid});
},
deleteOne: async (uuid) => {
return await Todo.deleteOne({uuid});
},
2022-04-03 04:34:37 +02:00
};