Skip to content

Commit

Permalink
feat: add event to calendar via command (Issue #2)
Browse files Browse the repository at this point in the history
* Added bot command to add an event to a calendar
* Will need to add additional parameters in the future however
  • Loading branch information
brwali committed Oct 2, 2023
1 parent 31a9efe commit d993c9f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions cogs/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit d993c9f

Please sign in to comment.