Lanbot_Music/commands/core/todo.js

98 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-04-03 04:34:37 +02:00
const ms = require("ms");
const fs = require("fs");
const path = require("path");
const uuid = require("uuid").v4;
const dbFileName = path.resolve(".", "utils", "todo.json");
let database = [];
module.exports = {
name: "todo",
aliases: ["todo"],
utilisation: "{prefix}todo <time> <salon> <message>",
async onStart(client) {
updateDatabase();
const currentTime = Date.now();
database.forEach((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.author}> ! tu m'as demandé de te rappeler\n\`\`\`${reminder.message}\`\`\` `
);
updateDatabase();
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
}, 1000);
} else {
setTimeout(() => {
const discordChannel = client.channels.resolve(reminder.channel);
discordChannel.send(
`<@${reminder.author}>, tu m'as demandé de te rappeler\n\`\`\`${reminder.message}\`\`\` `
);
updateDatabase();
database = database.filter((struct) => struct.uuid !== reminder.uuid);
saveDatabase();
}, delay);
}
});
console.log("-> ON START !!!!! ça marche putain!");
},
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 reminderStruct = {
uuid: uuid(),
author: message.author.id,
time: Date.now() + reveilTime,
channel: salon,
guild: message.guild.id,
message: reason,
};
setTimeout(function () {
const discordChannel = message.guild.channels.cache.get(salon);
discordChannel.send(
`<@${reminderStruct.author}>, tu m'as demandé de te rappeler\n\`\`\`${reminderStruct.message}\`\`\` `
);
updateDatabase();
database = database.filter(
(struct) => struct.uuid !== reminderStruct.uuid
);
saveDatabase();
}, reveilTime);
//check if file todo.json content is empty
updateDatabase();
database.push(reminderStruct);
saveDatabase();
// message.react("🤌");
// message.react("👌");
message.delete(10000).catch(console.error);
2022-04-03 04:34:37 +02:00
},
};
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));
};