Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion birthdays.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def userExistsJson(userId: str) -> bool:
return True
else:
return False

#this function sets a users birthday in the json file
def setBirthdayJson(userId: str, birthday: datetime):
with open('users.json', 'r') as f:
Expand All @@ -113,3 +113,15 @@ def getBirthdayJson(userId: str) -> datetime:
with open('users.json', 'r') as f:
users = json.load(f)
return datetime.strptime(users[userId], '%m/%d')

def getAllBirthdaysWithDate(date: datetime) -> list[str]:
return getAllBirthdaysWithDateJson(date)

def getAllBirthdaysWithDateJson(date: datetime) :
with open('users.json', 'r') as f:
users = json.load(f)
birthdays = []
for user in users:
if (users[user] == date.strftime('%m/%d')):
birthdays.append(user)
return birthdays
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"currentDate": "08/29"}
33 changes: 30 additions & 3 deletions loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@
import activity
import birthdays
import asyncio

import json

async def everyMinute():
while True:
print("looping")

await asyncio.sleep(60)
compareDates()
await asyncio.sleep(60)

# open data.json and get the current date
# compare the current date to datetime now
# if the dates are different, update the currentDate in data.json
# check birthdays
# if the dates are the same, do nothing
def compareDates():
print("comparing dates")
data = utils.openDataJSON()
currentDate = data["currentDate"]
now = utils.getCurrentDateTime()
nowDate = utils.convertDateTimeToDateString(now)
if (currentDate != nowDate):
print("updating stored date")
updateCurrentDate(nowDate)
todaysBirthdays = birthdays.getAllBirthdaysWithDate(now)
print("sending birthday messages")
sendBirthdayMessages(todaysBirthdays)

def updateCurrentDate(nowDate: str):
data = utils.openDataJSON()
data["currentDate"] = nowDate
utils.writeDataJSON(data)

def sendBirthdayMessages(todaysBirthdays):
for birthday in todaysBirthdays:
birthdayMessage = f"Happy Birthday {birthday}!"
14 changes: 9 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
import birthdays
import utils
import activity
import loop
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
import loop
from dotenv import load_dotenv
from azure.data.tables import TableServiceClient

load_dotenv()

client = discord.Client(command_prefix='?', intents=discord.Intents.all())

connect_str = os.getenv('AZURE_BLOB_CONNECTION_STRING')
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
storage_str = os.getenv('AZURE_STORAGE_ACCOUNT_NAME')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

users_table_name = os.getenv('USERS_TABLE_NAME')
globla_data_table_name = os.getenv('GLOBAL_DATA_TABLE_NAME')
timezone = os.getenv('TIMEZONE')

table_service_client = TableServiceClient.from_connection_string(connect_str)
global_data_client = table_service_client.get_table_client(globla_data_table_name)
users_client = table_service_client.get_table_client(users_table_name)

@client.event
async def on_ready(): # runs on startup
Expand Down
15 changes: 15 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def toStrID(id:int) -> str:

############################# DateTime ###########################################

def getCurrentDateTime() -> datetime:
return datetime.now()

def convertDateStringToDateTime(date: str) -> datetime:
return datetime.strptime(date, '%m/%d')
Expand Down Expand Up @@ -80,12 +82,25 @@ def openJSON() -> dict:
f.close()
return users

# opens JSON
def openDataJSON() -> dict:
f = open('data.json','r+')
data = json.load(f)
f.close()
return data

#writes a dict to a JSON
def writeJSON(dic):
f = open('users.json','r+')
json.dump(dic, f)
f.close()

#writes a dict to a JSON
def writeDataJSON(dic):
f = open('data.json','r+')
json.dump(dic, f)
f.close()

# checks if user is being tracked
def isTracked(id:str) -> bool:
jn = openJSON()
Expand Down