2022-04-06 23:43:27 +02:00
const ms = require ( "ms" ) ;
const uuid = require ( "uuid" ) . v4 ;
2022-04-08 00:01:12 +02:00
const mongoose = require ( "mongoose" ) ;
2022-04-06 23:43:27 +02:00
2022-04-08 00:01:12 +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" ,
2022-04-08 00:01:12 +02:00
aliases : [ "todoMP" , "MP" ] ,
utilisation : "{prefix}todo <time> <user or you> <message>" ,
2022-04-06 23:43:27 +02:00
async onStart ( client ) {
2022-04-08 00:01:12 +02:00
const remainingTodos = await TodoUtils . loadAll ( ) ;
2022-04-06 23:43:27 +02:00
const currentTime = Date . now ( ) ;
2022-04-08 00:01:12 +02:00
remainingTodos . forEach ( async ( reminder ) => {
2022-04-06 23:43:27 +02:00
const delay = reminder . time - currentTime ;
if ( delay < 0 ) {
2022-04-08 00:01:12 +02:00
setTimeout ( async ( ) => {
const discordUserMP = client . users . resolve ( reminder . user ) ;
discordUserMP . send (
` Bonjour <@ ${ reminder . user } >, La SNCF vous présente ses excuses pour le retard ! \n J'ai un message de la part de <@ ${ reminder . author } > : \n --------------------- \n ${ reminder . message } `
2022-04-06 23:43:27 +02:00
) ;
2022-04-08 00:01:12 +02:00
await TodoUtils . deleteOne ( reminder . uuid ) ;
2022-04-06 23:43:27 +02:00
} , 1000 ) ;
} else {
2022-04-08 00:01:12 +02:00
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
) ;
2022-04-08 00:01:12 +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 ] ) ;
2022-04-08 00:01:12 +02:00
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 ,
2022-04-08 00:01:12 +02:00
} ) ;
await reminder . save ( ) ;
2022-04-06 23:43:27 +02:00
2022-04-08 00:01:12 +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
) ;
2022-04-08 00:01:12 +02:00
await TodoUtils . deleteOne ( reminder . uuid ) ;
2022-04-06 23:43:27 +02:00
} , reveilTime ) ;
message . delete ( ) . catch ( ( ) => message . react ( "🤌" ) ) ;
} ,
} ;
2022-04-08 00:01:12 +02:00
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
} ;