Add calendarCog

This commit is contained in:
rzmk 2021-07-28 15:39:11 -04:00
parent 1d96f83124
commit bcfd065132
3 changed files with 64 additions and 14 deletions

18
bot.py
View file

@ -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]}')

59
cogs/info/calendarCog.py Normal file
View file

@ -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))

View file

@ -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