From d993c9f09dc5b0c644b5bcff03ea71a5fafb01fb Mon Sep 17 00:00:00 2001 From: bswalia Date: Mon, 2 Oct 2023 18:57:25 -0400 Subject: [PATCH] feat: add event to calendar via command (Issue #2) * Added bot command to add an event to a calendar * Will need to add additional parameters in the future however --- cogs/calendar.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/cogs/calendar.py b/cogs/calendar.py index 03e1f2d19..e6f78cad1 100644 --- a/cogs/calendar.py +++ b/cogs/calendar.py @@ -12,14 +12,14 @@ class Calendar(commands.Cog): - + def __init__(self, bot): self.bot = bot + - @commands.command(name="listCalendarEvents") - async def listCalendarEvents(self, ctx): + def credsSetUp(self): # If modifying these scopes, delete the file token.json. - SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] + SCOPES = ['https://www.googleapis.com/auth/calendar'] creds = None # The file token.json stores the user's access and refresh tokens, and is @@ -38,7 +38,11 @@ async def listCalendarEvents(self, ctx): # Save the credentials for the next run with open('token.json', 'w') as token: token.write(creds.to_json()) + return creds + @commands.command(name="listCalendarEvents") + async def listCalendarEvents(self, ctx): + creds = self.credsSetUp() try: service = build('calendar', 'v3', credentials=creds) @@ -61,6 +65,30 @@ async def listCalendarEvents(self, ctx): except HttpError as error: print('An error occurred: %s' % error) + + @commands.command(name="addCalendarEvent") + async def addCalendarEvent(self, ctx, name, location, description): + creds = self.credsSetUp() + try: + service = build('calendar', 'v3', credentials=creds) + event = { + "summary" : name, + "location": location, + "description": description, + "colorId": 4, + 'start': { + 'dateTime': '2023-10-28T17:00:00-07:00', + 'timeZone': 'America/New_York', + }, + 'end': { + 'dateTime': '2023-10-28T17:00:00-07:00', + 'timeZone': 'America/New_York', + }, + } + event = service.events().insert(calendarId="primary", body=event).execute() + + except HttpError as error: + print('An error occurred: %s' % error) async def setup(bot): n = Calendar(bot)