From bcfd0651327e975bc7aeffc62e5e15aa64830c7e Mon Sep 17 00:00:00 2001 From: rzmk Date: Wed, 28 Jul 2021 15:39:11 -0400 Subject: [PATCH] Add calendarCog --- bot.py | 18 +++--------- cogs/info/calendarCog.py | 59 ++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 cogs/info/calendarCog.py diff --git a/bot.py b/bot.py index 2c6f89f..c3587cc 100644 --- a/bot.py +++ b/bot.py @@ -1,3 +1,4 @@ +# Import all required packages and variables import os import discord from discord.ext import commands @@ -6,8 +7,9 @@ from dotenv import load_dotenv load_dotenv() TOKEN = os.environ.get("TOKEN") -client = commands.Bot(command_prefix= 'd!') +client = commands.Bot(command_prefix='d!') +# Bot initialized @client.event async def on_ready(): print(f'{client.user.name} is ready.') @@ -25,15 +27,7 @@ async def unload(ctx, extension): """Unloads a cog""" client.unload_extension(f'cogs.{extension}') -# Subfolders -# for filename in os.listdir('./cogs/fun'): -# if filename.endswith('.py'): -# client.load_extension(f'cogs.fun.{filename[:-3]}') - -# for filename in os.listdir('./cogs/general'): -# if filename.endswith('.py'): -# client.load_extension(f'cogs.general.{filename[:-3]}') - +# Cogs for filename in os.listdir('./cogs/info'): if filename.endswith('.py'): client.load_extension(f'cogs.info.{filename[:-3]}') @@ -46,10 +40,6 @@ for filename in os.listdir('./cogs/music'): if filename.endswith('.py'): client.load_extension(f'cogs.music.{filename[:-3]}') -# for filename in os.listdir('./cogs/owner'): -# if filename.endswith('.py'): -# client.load_extension(f'cogs.owner.{filename[:-3]}') - for filename in os.listdir('./cogs/inventory'): if filename.endswith('.py'): client.load_extension(f'cogs.inventory.{filename[:-3]}') diff --git a/cogs/info/calendarCog.py b/cogs/info/calendarCog.py new file mode 100644 index 0000000..3f47268 --- /dev/null +++ b/cogs/info/calendarCog.py @@ -0,0 +1,59 @@ +import os +import discord +import DiscordUtils +import datetime +from dateutil.parser import parse +import requests +from discord.ext import commands + +class CalendarCog(commands.Cog): + + def __init__(self, client): + self.client = client + + # Commands + @commands.command() + async def events(self, ctx): + """Gets all upcoming events from Google Calendar""" + # Check if environment variable exists + if not os.environ.get("GOOGLE_CALENDAR_ENDPOINT"): + return await ctx.send("No Google Calendar endpoint specified!") + + # Get upcoming events data from calendar + current_time = datetime.datetime.utcnow() + formatted_time = current_time.isoformat("T") + "Z" + calendar = os.environ.get("GOOGLE_CALENDAR_ENDPOINT") + "&timeMin=" + formatted_time + data = requests.get(calendar).json() + + # Check if there are any events + if not data["items"]: + return await ctx.send("There are no upcoming events!") + + # Get all upcoming events as a list + list_of_events = data["items"] + embeds = [] + + for event in list_of_events: + # Create data set + title = event["summary"] + start_time = event["start"]["dateTime"] + description = "No description." + if "description" in event: + description = event["description"] + formatted_start_time = datetime.datetime.strftime(parse(start_time), format="%B %d, %Y") + + # Create embed for single event and add to embeds list + embed = discord.Embed(color=ctx.author.color, title=title, description=description) + embed.add_field(name="Starts On", value=formatted_start_time, inline=True) + embeds.append(embed) + + # Create paginator + paginator = DiscordUtils.Pagination.CustomEmbedPaginator(ctx) + paginator.add_reaction('⏮️', "first") + paginator.add_reaction('⏪', "back") + paginator.add_reaction('⏩', "next") + paginator.add_reaction('⏭️', "last") + await paginator.run(embeds) + +def setup(client): + client.add_cog(CalendarCog(client)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1fabda0..3eb5a37 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,6 +30,7 @@ pycparser==2.20 pycryptodome==3.10.1 PyNaCl==1.4.0 pyparsing==2.4.7 +python-dateutil==2.8.2 python-dotenv==0.17.1 python-jwt==2.0.1 pytz==2021.1