@@ -17,7 +17,10 @@ class Bot {
1717 bot . on ( "guildDelete" , this . guildDelete ) ;
1818 bot . setInterval ( this . sendStats , 1000 * 60 * 60 ) ; //Stats toutes les 3h
1919 }
20-
20+ /**
21+ * Handler triggered when the bot is ready and connected
22+ * @param {Bot } self
23+ */
2124 ready ( self ) {
2225 console . info ( `Logged in as ${ bot . user . tag } !\n` ) ;
2326 //On attend le passage à la prochaine minute pour être le plus syncro possible
@@ -27,36 +30,49 @@ class Bot {
2730 const intervalId = setInterval ( ( self ) => {
2831 if ( Math . floor ( ( new Date ( ) . getTime ( ) / 1000 ) / 60 ) * 60 > oldMinute ) {
2932 console . log ( `\n\nNew minute detected, Starting cron Watcher at minute ${ new Date ( ) . getMinutes ( ) } ` ) ;
33+ this . cronWatcher ( this ) ;
3034 bot . setInterval ( ( ) => self . cronWatcher ( self ) , 1000 * 60 ) ; //Si on est passé à une nouvelle minute on lance le cronWatcher
3135 clearInterval ( intervalId ) ;
3236 }
3337 } , 10 , self ) ;
3438 }
39+ /**
40+ * Handler for event when the bot is removed from a guild
41+ * @param {Discord.Guild } guild
42+ */
3543 guildDelete ( guild ) {
3644 fs . rmdirSync ( __dirname + "/.." + process . env . DB_GUILDS + "/" + guild . id + "/" , { recursive : true } ) ;
3745 }
46+ /**
47+ * Handler for event when the bot is added to a guild
48+ * @param {Discord.Guild } guild
49+ */
3850 guildCreate ( guild ) {
3951 try {
4052 guild . systemChannel . send ( `Hey ! I'm Automate, to give orders you need to go on this website : https://automatebot.app.\nI can send your messages at anytime of the day event when you're not here to supervise me ;)` ) ;
4153 } catch ( e ) {
4254 console . log ( "Added bot but no systemChannel has been specified..." ) ;
4355 }
4456 }
57+ /**
58+ * Send all messages supposed to be sended, every minutes
59+ * @param {Bot } self actual instance of the bot
60+ */
4561 cronWatcher ( self ) {
46- const timestamp = Math . floor ( ( Date . now ( ) / 1000 ) / 60 ) ;
62+ const date = new Date ( ) ;
4763 fs . readdir ( __dirname + "/.." + process . env . DB_GUILDS + "/" , { } , ( err , files ) => {
4864 let i = 0 ;
4965 files . forEach ( guildId => fs . readFile ( __dirname + "/.." + process . env . DB_GUILDS + "/" + guildId + "/data.json" , ( err , file ) => {
5066 //Pour chaque guild on regarde si on doit envoyer un message
5167 const guildData = JSON . parse ( file ) ;
5268 let indexToDeletePonctual = [ ] ;
69+ const timestamp = Math . floor ( Date . now ( ) / 1000 / 60 ) ;
5370
5471 guildData . ponctual . forEach ( ( ponctualEvent , index ) => {
5572 if ( ponctualEvent . timestamp == timestamp ) {
56- const date = new Date ( ) ;
5773 try {
5874 bot . channels . cache . get ( ponctualEvent . channel_id ) . send ( ponctualEvent . sys_content || ponctualEvent . message ) . then ( message => {
59- console . log ( `New punctual message sent at ${ date . getDate ( ) } /${ date . getUTCMonth ( ) } /${ date . getFullYear ( ) } ${ date . getHours ( ) } :${ date . getMinutes ( ) } ` ) ;
75+ console . log ( `New punctual message sent at ${ date . getUTCDate ( ) } /${ date . getUTCMonth ( ) } /${ date . getUTCFullYear ( ) } ${ date . getUTCHours ( ) } :${ date . getUTCMinutes ( ) } ` ) ;
6076 i ++ ;
6177 } ) . catch ( e => {
6278 console . log ( `Error sending message (probably admin rights) to channel : ${ ponctualEvent . channel_id } ` ) ;
@@ -69,13 +85,11 @@ class Bot {
6985 } ) ;
7086
7187 guildData . freq . forEach ( ( freqEvent ) => {
72- const cronInstance = new Cron ( ) ;
88+ const cronInstance = new Cron ( { timezone : guildData . timezone_code } ) ;
7389 cronInstance . fromString ( freqEvent . cron ) ;
7490 const scheduler = cronInstance . schedule ( ) ;
7591 const timestampToExec = Math . floor ( scheduler . next ( ) . unix ( ) / 60 ) ;
76- // console.log(`freq next : ${timestampToExec}`);
77- // console.log(`Actual : ${timestamp}`);
78-
92+
7993 if ( timestampToExec == timestamp ) {
8094 try {
8195 bot . channels . cache . get ( freqEvent . channel_id ) . send ( freqEvent . sys_content || freqEvent . message ) . then ( message => {
@@ -108,7 +122,10 @@ class Bot {
108122
109123 } ) ;
110124 }
111-
125+ /**
126+ * Send stats to logs channel function
127+ * @return {Promise }
128+ */
112129 async sendStats ( ) {
113130 const channel = bot . channels . cache . get ( STAT_CHANNEL ) ;
114131 const lengthServer = fs . readdirSync ( __dirname + "/.." + process . env . DB_GUILDS ) . length ;
@@ -119,6 +136,10 @@ class Bot {
119136 messageSent = 0 ;
120137 }
121138
139+ /**
140+ * @param {string } guildId
141+ * @param {string } channelId
142+ */
122143 removeDeletedChannels ( guildId , channelId ) {
123144 console . log ( `Removing channel : ${ channelId } in guild : ${ guildId } ` ) ;
124145 const data = JSON . parse ( fs . readFileSync ( __dirname + "/.." + process . env . DB_GUILDS + "/" + guildId + "/data.json" , ) ) ;
@@ -131,7 +152,11 @@ class Bot {
131152 fs . writeFileSync ( __dirname + "/.." + process . env . DB_GUILDS + "/" + guildId + "/data.json" , JSON . stringify ( data ) ) ;
132153 }
133154
134- //Return channels collection or false
155+ /**
156+ * Get channels from a guild id
157+ * @param {string } id
158+ * @returns {Discord.Collection<string, Discord.GuildChannel>|boolean } Channels collection
159+ */
135160 getChannels ( id ) {
136161 if ( ! id ) return ;
137162 try {
@@ -142,6 +167,11 @@ class Bot {
142167 return false ;
143168 }
144169 }
170+ /**
171+ * Get the information of a guild
172+ * @param {string } id
173+ * @returns {Discord.Guild|boolean } guildCollection
174+ */
145175 getGuild ( id ) {
146176 if ( ! id ) return ;
147177 try {
@@ -152,6 +182,11 @@ class Bot {
152182 return false ;
153183 }
154184 }
185+ /**
186+ * Get people from a guild id
187+ * @param {string } id
188+ * @returns {Discord.Collection<string, Discord.GuildMember>|boolean } GuildMember collection
189+ */
155190 getPeople ( id ) {
156191 if ( ! id ) return ;
157192 try {
@@ -162,6 +197,21 @@ class Bot {
162197 return false ;
163198 }
164199 }
200+ /**
201+ * Get roles from a guild id
202+ * @param {string } id
203+ * @returns {Discord.Collection<string, Discord.Role> }
204+ */
205+ getRoles ( id ) {
206+ if ( ! id ) return ;
207+ try {
208+ const dataToSend = bot . guilds . cache . get ( id ) . roles . cache ;
209+ return dataToSend ;
210+ } catch ( e ) {
211+ console . log ( e ) ;
212+ return false ;
213+ }
214+ }
165215}
166216
167217module . exports = Bot ;
0 commit comments