@@ -45,6 +45,8 @@ const TEST_DATE = undefined;
45
45
const today = new Date ( TEST_DATE ?? new Date ( ) . toLocaleDateString ( ) ) ;
46
46
const nextWeek = new Date ( today ) ;
47
47
nextWeek . setDate ( today . getDate ( ) + 8 ) ;
48
+ console . log ( "Today: " , today , "\nNext Week: " , nextWeek ) ;
49
+
48
50
const url = `https://www.googleapis.com/calendar/v3/calendars/${
49
51
config . GOOGLE_CALENDAR_ID
50
52
} /events?key=${
@@ -102,6 +104,14 @@ function removeHTMLTags(str: string) {
102
104
}
103
105
}
104
106
107
+ // date obj to string
108
+ function dateToString ( date : Date ) {
109
+ const day = date . getDate ( ) ;
110
+ const month = date . getMonth ( ) + 1 ;
111
+ const year = date . getFullYear ( ) ;
112
+ return month + "/" + day + "/" + year ;
113
+ }
114
+
105
115
// Date Interface
106
116
interface DateProps {
107
117
start : string ;
@@ -125,15 +135,11 @@ function getDateProps(
125
135
// if the root cause of this issue is found, you can take steps to fix it
126
136
const newDate = new Date ( date1 ) ;
127
137
newDate . setHours ( newDate . getHours ( ) ) ;
128
- const month = ( newDate . getMonth ( ) + 1 ) . toString ( ) ;
129
- const day = newDate . getDate ( ) . toString ( ) ;
130
- const year = newDate . getFullYear ( ) . toString ( ) ;
138
+ const startDateString = dateToString ( newDate ) ;
131
139
132
140
const newDate2 = new Date ( date2 ) ;
133
141
newDate2 . setHours ( newDate2 . getHours ( ) ) ;
134
- const month2 = ( newDate2 . getMonth ( ) + 1 ) . toString ( ) ;
135
- const day2 = newDate2 . getDate ( ) . toString ( ) ;
136
- const year2 = newDate2 . getFullYear ( ) . toString ( ) ;
142
+ const endDateString = dateToString ( newDate2 ) ;
137
143
138
144
console . log ( "CHECKING DATE PROPS:" , event , newDate , newDate2 ) ;
139
145
@@ -155,20 +161,28 @@ function getDateProps(
155
161
156
162
dateObject . start = formattedHours1 + ":" + minutes1 + " " + period1 ;
157
163
dateObject . end = formattedHours2 + ":" + minutes2 + " " + period2 ;
158
- dateObject . date = month + "/" + day + "/" + year ;
164
+ dateObject . date = startDateString ;
159
165
} else {
160
166
// Set start and end to dates
161
- dateObject . start = month + "/" + day + "/" + year ;
162
- dateObject . end = month2 + "/" + day2 + "/" + year2 ;
167
+ dateObject . start = startDateString ;
168
+ dateObject . end = endDateString ;
163
169
}
164
170
165
171
return dateObject ;
166
172
}
167
173
168
174
// Function to create discord event
169
- async function createDiscordEvents ( events : Messages [ ] , client : Client ) {
175
+ async function createDiscordEvents (
176
+ events : GoogleCalendarDataProps [ ] ,
177
+ client : Client ) {
178
+
179
+ // filter out operations meetings, as they mess with event creation
180
+ const filteredEvents = events
181
+ . filter ( ( event ) => event . summary !== "Operations Meeting" ) ;
182
+
170
183
// Iterate through the next week events
171
- events . map ( async ( event ) => {
184
+ filteredEvents . map ( async ( event ) => {
185
+ console . log ( event . summary ) ;
172
186
// Create a guild event for each event using the discord client
173
187
for ( const guild of client . guilds . cache . values ( ) ) {
174
188
try {
@@ -209,9 +223,7 @@ async function createDiscordEvents(events: Messages[], client: Client) {
209
223
}
210
224
211
225
// Grab all events that are today, tomorrow, or in a week
212
- async function getValidEvents ( ) {
213
- const data = await fetchEvents ( url ) ;
214
-
226
+ async function getValidEvents ( data : GoogleCalendarDataProps [ ] ) {
215
227
// sets all consts to a new date with the local timezone.
216
228
const today = new Date ( TEST_DATE ?? new Date ( ) . toLocaleDateString ( ) ) ;
217
229
const tomorrow = new Date ( today ) ;
@@ -223,13 +235,9 @@ async function getValidEvents() {
223
235
224
236
const validEvents : Messages [ ] = [ ] ;
225
237
// filters out "cancelled" events from the initial data and objects that are recurring, and obj.recurrence is not null
226
- const allEvents = data . items
227
- . filter ( ( event ) => event . status !== "cancelled" )
228
- . filter ( ( event ) => event . recurrence !== null )
229
- . filter ( ( event ) => event . summary !== "Kickstart Meeting" )
230
- // remove elements with the same summary, keeping the greatest date
231
- // this is to prevent duplicate events from being displayed
232
- . reduce (
238
+ // remove elements with the same summary, keeping the greatest date
239
+ // this is to prevent duplicate events from being displayed
240
+ const allEvents = data . reduce (
233
241
(
234
242
acc : GoogleCalendarDataProps [ ] ,
235
243
event : GoogleCalendarDataProps
@@ -296,24 +304,27 @@ async function getValidEvents() {
296
304
}
297
305
298
306
async function hookLogic ( client : Client , webhook : WebhookClient ) {
299
- console . log ( "Checking for events..." ) ;
300
- const events = await getValidEvents ( ) ;
307
+ const data = await fetchEvents ( url ) ;
308
+
309
+ const allEvents = data . items
310
+ . filter ( ( event ) => event . status !== "cancelled" )
311
+ . filter ( ( event ) => event . recurrence !== null )
312
+ . filter ( ( event ) => event . summary !== "Kickstart Meeting" ) ;
301
313
302
- const todayDate = new Date ( ) ;
303
- const todayDay = todayDate . getDate ( ) ;
304
- const todayMonth = todayDate . getMonth ( ) + 1 ;
305
- const todayYear = todayDate . getFullYear ( ) ;
306
- const todayDateString = todayMonth + "/" + todayDay + "/" + todayYear ;
314
+ // Create event on Discord for the upcoming event
315
+ createDiscordEvents ( allEvents , client ) ;
316
+
317
+ console . log ( "Checking for events..." ) ;
318
+ const events = await getValidEvents ( allEvents ) ;
307
319
308
320
if ( events . length === 0 ) {
309
321
return ;
310
322
} else if ( events . length >= 1 ) {
323
+ const messageDay = new Date ( today ) ;
324
+ messageDay . setDate ( today . getDate ( ) + 1 ) ;
311
325
webhook . send (
312
- `Hey @everyone, it's ${ todayDateString } , here are some reminders about our upcoming events!\n`
326
+ `Hey @everyone, it's ${ dateToString ( messageDay ) } , here are some reminders about our upcoming events!\n`
313
327
) ;
314
-
315
- // Create event on Discord for the upcoming event
316
- createDiscordEvents ( events , client ) ;
317
328
}
318
329
319
330
// Sort events by today, then tomorrow, then next week
0 commit comments