Skip to content

Commit

Permalink
Miscellaneous Cleanup (#55)
Browse files Browse the repository at this point in the history
* Fix typing

* Reorder import order

* Whitespace in front of keywords

* Return int instead of float

* Change f-strings to regular strings when possible

* Rename a function which was called another name by accident

* Add clone_cr help command into the help group

- Without this the command would not be a recognized option to get help for

* Add specific versions for all packages used

- It's been a long time since the last deploy and there may be many new breaking changes of packages which have not been slowly tested
- So tie every package to the exact version it was at from the last deploy
  • Loading branch information
VishalRamesh50 authored Jan 23, 2023
1 parent c0fe7c3 commit 4f6279e
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 51 deletions.
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discord.py[voice]==1.7.3
pytz
pymongo[srv]
python-dotenv
requests
sentry-sdk
pytz==2021.3
pymongo[srv]==4.0.1
python-dotenv==0.19.2
requests==2.27.1
sentry-sdk==1.5.1
2 changes: 1 addition & 1 deletion src/client/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class Bot(commands.Bot):
def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.channel_config: Dict[int, Dict[str, int]] = defaultdict(dict)
self.db = DBClient(os.environ["DB_CONNECTION_URL"])
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/aoun.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def set_a_cooldown(self, ctx: commands.Context, cooldown: int) -> None:

if cooldown < 0:
await ctx.send(
f"You want negative time? There is such a thing as too much "
"You want negative time? There is such a thing as too much "
"Aoun you know. You need a `positive integer`.",
delete_after=5,
)
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def check(reaction: discord.Reaction, user: discord.Member) -> bool:
for _, setup_func in SETUP_MAP.values():
await setup_func(ctx, all_modules=True)
else:
setup_func: Callable = SETUP_MAP[str(reaction)][1]
setup_func = SETUP_MAP[str(reaction)][1]
await setup_func(ctx)
await ctx.send(
embed=discord.Embed(
Expand Down
3 changes: 2 additions & 1 deletion src/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ async def auto_course_reactions(self, ctx: commands.Context) -> None:
await ctx.send(embed=embed)

@is_admin()
async def auto_course_reactions(self, ctx: commands.Context) -> None:
@help.group(aliases=["cloneCr"])
async def clone_cr(self, ctx: commands.Context) -> None:
embed = self._get_embed("clone_cr")
embed.add_field(name="Command", value="`.clone_cr`", inline=False)
embed.add_field(name="Example", value="`.clone_cr`", inline=False)
Expand Down
58 changes: 29 additions & 29 deletions src/cogs/hours/hours_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class HoursModel:
get_available_location() -> str
Returns a string of all the possible locations in alphabetical order
"""
def __init__(self):
def __init__(self) -> None:
# ------------------------------ VARIABLES --------------------------------
self.todays_locations: Dict[Tuple[str], Dict[str, Union[str, List[int]]]] = nu_dining.NORMAL_LOCATIONS
self.current_location: Dict[str, Union[str, List[int]]] = None
self.current_location: Optional[Dict[str, Union[str, List[int]]]] = None
self.current_date_range: Optional[str] = None
self.date_name: str = ""
# ------------------------------ CONSTANTS --------------------------------
Expand All @@ -89,16 +89,16 @@ def __init__(self):
self.today: str = self.est.strftime("%A").upper()
# -------------------------------------------------------------------------

def __reset_time(self):
self.est: datetime = datetime.now(timezone('US/Eastern'))
self.today: str = self.est.strftime("%A").upper()
def __reset_time(self) -> None:
self.est = datetime.now(timezone('US/Eastern'))
self.today = self.est.strftime("%A").upper()

# TODO: Find a way to keep this from being duplicated since it's common in many classes
# Possibly create a decorator or make it a function in a Utils class
def __clean_input(self, input: str) -> str:
return input.strip().upper()

def __set_todays_location(self, today_datetime: datetime = None) -> None:
def __set_todays_location(self, today_datetime: Optional[datetime] = None) -> None:
"""
Sets todays_location, current_date_range, and date_name based on the given datetime.
Will use today's datetime if None is provided.
Expand All @@ -114,7 +114,7 @@ def __set_todays_location(self, today_datetime: datetime = None) -> None:
if today_datetime is None:
today_datetime = self.est
# create a new datetime object ignoring time and only using date
today_datetime: datetime = datetime(today_datetime.year, today_datetime.month, today_datetime.day)
today_datetime = datetime(today_datetime.year, today_datetime.month, today_datetime.day)
for date_range, location in nu_dining.DATES_TO_LOCATIONS.items():
start_datetime, end_datetime = self.__get_datetime_range(date_range)
# if today's date is between a datetime_range for special hours
Expand All @@ -136,7 +136,7 @@ def __set_todays_location(self, today_datetime: datetime = None) -> None:
self.current_date_range = None
self.date_name = ""

def valid_location(self, location_name: str, today_datetime: datetime = None) -> bool:
def valid_location(self, location_name: str, today_datetime: Optional[datetime] = None) -> bool:
"""
Determines whether the given location name is recognized
as a valid location or an alias of that location from todays_locations
Expand All @@ -162,7 +162,7 @@ def valid_location(self, location_name: str, today_datetime: datetime = None) ->
True if the given location_name is a valid location else False
"""
self.__set_todays_location(today_datetime)
location_name: str = self.__clean_input(location_name)
location_name = self.__clean_input(location_name)
for aliases, location in self.todays_locations.items():
if location_name in aliases:
self.current_location = location
Expand Down Expand Up @@ -242,7 +242,7 @@ def __get_full_day(self, day: str) -> str:
----------
`AssertionError`: If the given day is not a valid day
"""
assert(self.valid_day(day))
assert self.valid_day(day)
day = self.__clean_input(day)
if day == 'TOMORROW':
return self.__get_tomorrow(self.get_today())
Expand Down Expand Up @@ -278,7 +278,7 @@ def __get_datetime_range(self, date_range: str) -> Tuple[datetime, datetime]:
except ValueError:
assert False, 'Given date range is not in the format mm/dd/yy-mm/dd/yy'

def __get_num_days_in_range(self, day: str, today_datetime: datetime = None) -> int:
def __get_num_days_in_range(self, day: str, today_datetime: Optional[datetime] = None) -> int:
"""
Determines how many of the given day are present between
the ranges of the current date range.
Expand Down Expand Up @@ -312,15 +312,15 @@ def __get_num_days_in_range(self, day: str, today_datetime: datetime = None) ->
# get the start and end datetimes given the date_range
working_datetime, end_datetime = self.__get_datetime_range(self.current_date_range)
# while the working datetime is not past the end_range
while(working_datetime <= end_datetime):
while (working_datetime <= end_datetime):
# if the working datetime's day is the same as the given day
if working_datetime.strftime('%A').upper() == day:
return (end_datetime - working_datetime).days // 7 + 1
working_datetime += td(days=1)
# if there was no day in the range which had the same day
return 0

def __which_day_num(self, day: str, current_datetime: datetime = None) -> int:
def __which_day_num(self, day: str, current_datetime: Optional[datetime] = None) -> int:
"""
Finds which day number to select when a location has multiple of the same
day with different times.
Expand Down Expand Up @@ -349,12 +349,12 @@ def __which_day_num(self, day: str, current_datetime: datetime = None) -> int:
if current_datetime is None:
current_datetime = self.est
# get a datetime using just the date and ignoring the time
current_datetime: datetime = datetime(current_datetime.year, current_datetime.month, current_datetime.day)
current_datetime = datetime(current_datetime.year, current_datetime.month, current_datetime.day)
# get the start and end ranges as datetime objects
working_datetime, end_datetime = self.__get_datetime_range(self.current_date_range)
# the result to return of which version of the day comes immediately after the current_datetime
count: int = 0
while(working_datetime <= end_datetime):
while (working_datetime <= end_datetime):
# if the working datetime's day matches the given day
if working_datetime.strftime('%A').upper() == day:
count += 1
Expand All @@ -367,7 +367,7 @@ def __which_day_num(self, day: str, current_datetime: datetime = None) -> int:
working_datetime += td(days=1)
return count

def __obtain_hours_key_value(self, location: str, day: str, today_datetime: datetime = None) -> Tuple[str, List[int]]:
def __obtain_hours_key_value(self, location: str, day: str, today_datetime: Optional[datetime] = None) -> Tuple[str, List[int]]:
"""
Searches through the location dictionaries for the current_location
and then returns the key value pair for the given location which contains
Expand Down Expand Up @@ -400,10 +400,10 @@ def __obtain_hours_key_value(self, location: str, day: str, today_datetime: date
if today_datetime is None:
today_datetime = self.est
# move the date to a point where the it's the same day as the given day
while(today_datetime.strftime('%A').upper() != day):
while (today_datetime.strftime('%A').upper() != day):
today_datetime += td(days=1)
# confirm that the given location is a valid and sets the current_location
assert(self.valid_location(location, today_datetime))
assert self.valid_location(location, today_datetime)
day_acronym: str = self.DAYS_TO_ACRONYMS[day]

# if there is holiday data being used
Expand Down Expand Up @@ -465,7 +465,7 @@ def __obtain_day_range(self, day_range: str, day: str) -> str:
day_range = ''.join([i for i in day_range if not i.isdigit()])
day_acronym: str = self.DAYS_TO_ACRONYMS[day]
# make sure that the day value has at least 1 character
assert(len(day_range) > 0)
assert len(day_range) > 0
# make sure the given day is in the range. Otherwise the range makes not sense.
assert day_acronym in day_range, 'The given day is not within the given range'
# if the day range is only one character
Expand Down Expand Up @@ -539,7 +539,7 @@ def location_hours_msg(self, location: str, day: str) -> str:
----------
`AssertionError`: If the given location/day is not a valid location/day
"""
assert(self.valid_location(location))
assert self.valid_location(location)
location = self.__clean_input(location)
# gets the full day and asserts that the day is valid
day = self.__get_full_day(day)
Expand Down Expand Up @@ -580,7 +580,7 @@ def get_hours_of_operation(self, location: str, day: str) -> str:
----------
`AssertionError`: If the given location/day is not a valid location/day
"""
assert(self.valid_location(location))
assert self.valid_location(location)
location = self.__clean_input(location)
# gets the full day and asserts that the day is valid
day = self.__get_full_day(day)
Expand Down Expand Up @@ -647,15 +647,15 @@ def __convert_to_datetime(self, hours: int, mins: int, day: str) -> datetime:
`AssertionError`: if hours is negative, mins is not within 0 to 60 [0,60),
or day is not valid
"""
assert(hours >= 0)
assert(0 <= mins < 60)
assert hours >= 0
assert 0 <= mins < 60
# gets the full day and asserts that it is valid
day = self.__get_full_day(day)
self.__reset_time()
diff_of_days, remaining_hours = divmod(hours, 24)

index_of_curr_day: int = self.ORDERED_DAYS.index(self.today)
while(True):
while (True):
# if yesterday is the given day
if self.__get_yesterday(self.ORDERED_DAYS[index_of_curr_day]) == day:
diff_of_days -= 1
Expand Down Expand Up @@ -726,7 +726,7 @@ def time_till_open(self, location: str, day: str) -> int:
"""
# if the location is closed the entire day
if self.closed_all_day(location, day):
return -1.
return -1
# if the location is currently still open
if self.is_open(location, day):
return 0
Expand Down Expand Up @@ -791,7 +791,7 @@ def get_today(self) -> str:
self.__reset_time()
return self.today

def get_link(self, location: str, day: str = None) -> str:
def get_link(self, location: str, day: Optional[str] = None) -> str:
"""
Gets the link associated with the given location
relative to the given day. Will use today's day if
Expand Down Expand Up @@ -820,9 +820,9 @@ def get_link(self, location: str, day: str = None) -> str:
day = self.__get_full_day(day)
current_datetime: datetime = datetime(self.est.year, self.est.month, self.est.day)
# moves the datetime to a place where it's day is the same as the given day
while(current_datetime.strftime('%A').upper() != day):
while (current_datetime.strftime('%A').upper() != day):
current_datetime += td(days=1)
assert(self.valid_location(location, current_datetime))
assert self.valid_location(location, current_datetime)
return self.current_location['LINK']

def closed_all_day(self, location: str, day: str) -> bool:
Expand All @@ -845,7 +845,7 @@ def closed_all_day(self, location: str, day: str) -> bool:
----------
`AssertionError`: If the given location/day is not a valid location/day.
"""
assert(self.valid_location(location))
assert self.valid_location(location)
# gets the full day and asserts that it is valid
day = self.__get_full_day(day)
# tuple containing day acronym and time range as a list of 4 integers
Expand Down
18 changes: 9 additions & 9 deletions src/cogs/reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ async def reminder(self, ctx: commands.Context, *args) -> None:
time: float = float(original_time)
except ValueError:
await ctx.send(
f"You have to use a number for the 2nd to last term.\n"
f"Incorrect: `.reminder Husky Bot is cool in five secs`\n"
f"Correct: `.reminder Husky Bot is cool in 5 secs`"
"You have to use a number for the 2nd to last term.\n"
"Incorrect: `.reminder Husky Bot is cool in five secs`\n"
"Correct: `.reminder Husky Bot is cool in 5 secs`"
)
return

Expand Down Expand Up @@ -85,18 +85,18 @@ async def reminder(self, ctx: commands.Context, *args) -> None:
# if the given unit of time was not recognized
if not valid_unit:
await ctx.send(
f"Your unit of measurement must be a second, minute, hour, day, or week.\n"
f"Incorrect: `.reminder Husky Bot is cool in 1 month`\n"
f"Correct: `.reminder Husky Bot is cool in 4 weeks`"
"Your unit of measurement must be a second, minute, hour, day, or week.\n"
"Incorrect: `.reminder Husky Bot is cool in 1 month`\n"
"Correct: `.reminder Husky Bot is cool in 4 weeks`"
)
return

# if the user didn't separate their reminder and unit of time with "in"
if "in" != args[len(args) - 3]:
await ctx.send(
f"You must include the word `in` between your reminder and the time.\n"
f"Incorrect: `.reminder Husky Bot is cool 5 secs`\n"
f"Correct: `.reminder Husky Bot is cool in 5 secs`"
"You must include the word `in` between your reminder and the time.\n"
"Incorrect: `.reminder Husky Bot is cool 5 secs`\n"
"Correct: `.reminder Husky Bot is cool in 5 secs`"
)
return

Expand Down
2 changes: 1 addition & 1 deletion src/cogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def ordered_list_members(
@commands.command(aliases=["whoam"])
@commands.guild_only()
@commands.check_any(in_channel(BOT_SPAM_CHANNEL_ID), is_admin(), is_mod())
async def whois(self, ctx: commands.Context, *, member_name: str = None) -> None:
async def whois(self, ctx: commands.Context, *, member_name: Optional[str] = None) -> None:
"""
Sends an embedded message containing information about the given user.
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from client.setup import client, COGS_DIRECTORY
from client import misc, error_handlers
from client import error_handlers, misc

logger = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from client.bot import Bot, ChannelType


def required_configs(*channel_types: Tuple[ChannelType]):
def required_configs(*channel_types: ChannelType):
"""A decorator which checks that the method should be run by checking if the guild
has configured the required channels for this function to be executed.
Expand All @@ -30,7 +30,7 @@ def required_configs(*channel_types: Tuple[ChannelType]):
"""

def decorator(function):
async def wrapper(*args, **kwargs):
async def wrapper(*args, **kwargs) -> None:
client: Bot = args[0].client
for arg in args[1:]:
# Try to get `.guild_id` directly
Expand Down

0 comments on commit 4f6279e

Please sign in to comment.