Skip to content

Commit

Permalink
refactor: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Dec 10, 2023
1 parent c45eb40 commit c74f1f7
Show file tree
Hide file tree
Showing 58 changed files with 318 additions and 210 deletions.
2 changes: 2 additions & 0 deletions crenata/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
if __name__ == "__main__":
parser = ArgumentParser("crenata")
args = parse_args(parser, argv[1:])

client = create_app(args, intents=Intents.default())

client.run()
4 changes: 4 additions & 0 deletions crenata/application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@

def create_app(args: Namespace, intents: Intents) -> Crenata:
crenata = Crenata(intents=intents)

crenata.config.update_with_args(args)

crenata.tree.set_error_handler(error_handler)

crenata.tree.add_command(register)
crenata.tree.add_command(profile)
crenata.tree.add_command(exit)
crenata.tree.add_command(school)
crenata.tree.add_command(preferences)

return crenata
6 changes: 6 additions & 0 deletions crenata/application/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class Crenata(Client):
def __init__(self, intents: Intents, *args: Any, **kwargs: Any) -> None:
super().__init__(intents=intents, *args, **kwargs)

self.tree = CrenataCommandTree(self)
self.config = CrenataConfig()

Expand All @@ -23,21 +24,25 @@ async def startup(self) -> None:
async def closeup(self) -> None:
if self.neispy.session and not self.neispy.session.closed:
await self.neispy.session.close()

if getattr(self.database, "database", None):
await self.database.engine.dispose()

async def setup_hook(self) -> None:
if self.config.PRODUCTION:
await self.tree.sync()

else:
await self.tree.sync(guild=Object(self.config.TEST_GUILD_ID))

async def close(self) -> None:
await self.closeup()

return await super().close()

async def start(self, token: str, *, reconnect: bool = True) -> None:
await self.startup()

return await super().start(token, reconnect=reconnect)

def run(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -47,4 +52,5 @@ def run(self, *args: Any, **kwargs: Any) -> None:
토큰은 Config에서 로드하기 때문에 인자로 줄 필요가 없습니다.
"""
kwargs.update({"token": self.config.TOKEN})

return super().run(*args, **kwargs)
10 changes: 8 additions & 2 deletions crenata/application/commands/exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
async def exit(interaction: Interaction[Crenata]) -> None:
async with InteractionLock(interaction):
user_repository = UserRepositoryImpl(interaction.client.database)
get_user_usecase = GetUserUseCase(user_repository)

user = await GetUserUseCase(user_repository).execute(interaction.user.id)
user = await get_user_usecase.execute(interaction.user.id)

embed = exit_embed_builder()

view = Confirm(interaction.user.id)

await interaction.response.send_message(embed=embed, view=view, ephemeral=True)

if not await view.wait():
if view.is_confirm:
await DeleteUserUseCase(user_repository).execute(user)
delete_user_usecase = DeleteUserUseCase(user_repository)

await delete_user_usecase.execute(user)

await interaction.edit_original_response(
content=ApplicationStrings.UNREGISTER_COMPLETED,
embed=None,
view=None,
)

return

await interaction.edit_original_response(
Expand Down
11 changes: 5 additions & 6 deletions crenata/application/commands/preferences/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
@app_commands.describe(private="학교 이름을 비공개로 할지 여부입니다.")
@app_commands.describe(ephemeral="자기 자신에게만 보이게 할지 여부입니다.")
async def edit(
interaction: Interaction[Crenata],
private: bool,
ephemeral: bool,
interaction: Interaction[Crenata], private: bool, ephemeral: bool
) -> None:
async with InteractionLock(interaction):
preferences_repository = PreferencesRepositoryImpl(interaction.client.database)
await UpdatePreferencesUseCase(preferences_repository).execute(
update_preferences_usecase = UpdatePreferencesUseCase(preferences_repository)

await update_preferences_usecase.execute(
interaction.user.id,
Preferences(
private,
Expand All @@ -30,6 +30,5 @@ async def edit(
)

await interaction.response.send_message(
content=ApplicationStrings.PREFERENCE_EDITED,
ephemeral=True,
content=ApplicationStrings.PREFERENCE_EDITED, ephemeral=True
)
3 changes: 2 additions & 1 deletion crenata/application/commands/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
@app_commands.command(name="프로필", description="내 프로필을 확인합니다.")
async def profile(interaction: Interaction[Crenata]) -> None:
user_repository = UserRepositoryImpl(interaction.client.database)
get_user_usecase = GetUserUseCase(user_repository)

user = await GetUserUseCase(user_repository).execute(interaction.user.id)
user = await get_user_usecase.execute(interaction.user.id)

is_private = user.preferences.private
is_empheral = user.preferences.ephemeral
Expand Down
7 changes: 4 additions & 3 deletions crenata/application/commands/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

@app_commands.command(name="가입", description="가입합니다.")
async def register(interaction: Interaction[Crenata]) -> None:
repository = UserRepositoryImpl(interaction.client.database)
usecase = CreateUserUseCase(repository)
await usecase.execute(User.default(interaction.user.id))
user_repository = UserRepositoryImpl(interaction.client.database)
create_user_usecase = CreateUserUseCase(user_repository)

await create_user_usecase.execute(User.default(interaction.user.id))

embed = Embed(title=ApplicationStrings.REGISTER_COMPLETED)

Expand Down
28 changes: 20 additions & 8 deletions crenata/application/commands/school/meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,41 @@ async def meal(
) -> None:
if not date:
date = datetime.now(tz=KST)
if school_name is None:

if not school_name:
user_repository = UserRepositoryImpl(interaction.client.database)
user = await GetUserUseCase(user_repository).execute(interaction.user.id)
school_info = user.school_info
if school_info is None:
get_user_usecase = GetUserUseCase(user_repository)

user = await get_user_usecase.execute(interaction.user.id)

if not user.school_info:
raise SchoolInfoNotFound

school_info = user.school_info
is_private = user.preferences.private

else:
school_repository = SchoolRepositoryImpl(interaction.client.neispy)
school_infos = await GetSchoolUseCase(school_repository).execute(school_name)
get_school_usecase = GetSchoolUseCase(school_repository)

school_infos = await get_school_usecase.execute(school_name)

school_info = await school_page(interaction, school_infos)

is_private = True

meal_repository = MealRepositoryImpl(interaction.client.neispy)
get_meal_use_case = GetMealUseCase(meal_repository)
meal = await get_meal_use_case.execute(
get_meal_usecase = GetMealUseCase(meal_repository)

meal = await get_meal_usecase.execute(
school_info.edu_office_code, school_info.standard_school_code, date, meal_time
)

if not meal:
raise MealNameNotFound

embed = meal_embed_builder(meal, is_private=is_private)
embed = meal_embed_builder(meal, is_private)

view = ui.View()
select_allergy_ui = AllergyUI(interaction.user.id)
view.add_item(select_allergy_ui)
Expand Down
12 changes: 8 additions & 4 deletions crenata/application/commands/school/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
@app_commands.describe(school_name="학교 이름")
async def search(interaction: Interaction[Crenata], school_name: str) -> None:
school_repository = SchoolRepositoryImpl(interaction.client.neispy)
school_infos = await GetSchoolUseCase(school_repository).execute(school_name)
get_school_usecase = GetSchoolUseCase(school_repository)

school_infos = await get_school_usecase.execute(school_name)

school_info = await school_page(interaction, school_infos)
await interaction.edit_original_response(
view=None, embed=detail_school_school_embed_builder(school_info)
)

embed = detail_school_school_embed_builder(school_info)

await interaction.edit_original_response(view=None, embed=embed)
30 changes: 19 additions & 11 deletions crenata/application/commands/school/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ async def setup(
raise MustBeGreaterThanZero

user_repository = UserRepositoryImpl(interaction.client.database)
school_info_repository = SchoolInfoRepositoryImpl(interaction.client.database)
get_user_usecase = GetUserUseCase(user_repository)

user = await GetUserUseCase(user_repository).execute(interaction.user.id)
user = await get_user_usecase.execute(interaction.user.id)

school_repository = SchoolRepositoryImpl(interaction.client.neispy)
get_school_usecase = GetSchoolUseCase(school_repository)

schools = await get_school_usecase.execute(school_name)

school = await school_page(interaction, schools, ephemeral=True)

school_info = SchoolInfo(
name=school.name,
grade=grade,
Expand All @@ -51,32 +54,37 @@ async def setup(
department=None,
major=None,
)

if (
school.highschool_category in ["특목고", "특성화고"]
and school.highschool_general_or_business != "일반계"
):
major_info_repository = MajorInfoRepositoryImpl(interaction.client.neispy)
major_info_usecase = GetMajorInfoUseCase(major_info_repository)
major_infos = await major_info_usecase.execute(
get_major_info_usecase = GetMajorInfoUseCase(major_info_repository)

major_infos = await get_major_info_usecase.execute(
school.edu_office_code, school.standard_school_code
)

major_info = await major_info_selector(interaction, major_infos)

school_info.department = major_info.department
school_info.major = major_info.major

if user.school_info is None:
school_info_repository = SchoolInfoRepositoryImpl(interaction.client.database)

if not user.school_info:
create_school_info_usecase = CreateSchoolInfoUseCase(school_info_repository)
await create_school_info_usecase.execute(interaction.user.id, school_info)

await interaction.edit_original_response(
content=ApplicationStrings.SUCCESSFUL_EDIT,
embed=None,
view=None,
content=ApplicationStrings.SUCCESSFUL_EDIT, embed=None, view=None
)

else:
update_school_info_usecase = UpdateSchoolInfoUseCase(school_info_repository)
await update_school_info_usecase.execute(interaction.user.id, school_info)

await interaction.edit_original_response(
content=ApplicationStrings.SUCCESSFUL_EDIT,
embed=None,
view=None,
content=ApplicationStrings.SUCCESSFUL_EDIT, embed=None, view=None
)
48 changes: 29 additions & 19 deletions crenata/application/commands/school/timetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,42 @@ async def timetable(
department: Optional[str] = None,
date: Optional[app_commands.Transform[datetime, ToDatetime]] = None,
) -> None:
if date is None:
if not date:
date = datetime.now(KST)

if school_name is None:
if not school_name:
user_repository = UserRepositoryImpl(interaction.client.database)
user = await GetUserUseCase(user_repository).execute(interaction.user.id)
school_info = user.school_info
if school_info is None:
get_user_usecase = GetUserUseCase(user_repository)

user = await get_user_usecase.execute(interaction.user.id)

if not user.school_info:
raise SchoolInfoNotFound

school_info = user.school_info
is_private = user.preferences.private
ephemeral = user.preferences.ephemeral
major = school_info.major
department = school_info.department

if grade is not None and room is not None:
if grade < 1 or room < 1:
raise MustBeGreaterThanZero
else:
grade = school_info.grade
room = school_info.room
grade = grade or school_info.grade
room = room or school_info.room

if grade < 1 or room < 1:
raise MustBeGreaterThanZero

else:
if grade is None or room is None:
if grade is None or room is None: # 0 is falsy
raise NeedGradeAndRoomArgument

if grade < 1 or room < 1:
raise MustBeGreaterThanZero

school_repository = SchoolRepositoryImpl(interaction.client.neispy)
school_infos = await GetSchoolUseCase(school_repository).execute(school_name)
get_school_usecase = GetSchoolUseCase(school_repository)

school_infos = await get_school_usecase.execute(school_name)

school_info = await school_page(interaction, school_infos)

is_private = True
Expand All @@ -86,11 +91,14 @@ async def timetable(
and school_info.highschool_general_or_business != "일반계"
):
major_info_repository = MajorInfoRepositoryImpl(interaction.client.neispy)
major_info_usecase = GetMajorInfoUseCase(major_info_repository)
major_infos = await major_info_usecase.execute(
get_major_info_usecase = GetMajorInfoUseCase(major_info_repository)

major_infos = await get_major_info_usecase.execute(
school_info.edu_office_code, school_info.standard_school_code
)

major_info = await major_info_selector(interaction, major_infos)

major = major_info.major
department = major_info.department

Expand All @@ -99,7 +107,9 @@ async def timetable(
]

timetable_repository = TimetableRepositoryImpl(interaction.client.neispy)
timetable_info = await GetWeekTimetableUseCase(timetable_repository).execute(
get_week_timetable_usecase = GetWeekTimetableUseCase(timetable_repository)

timetable_info = await get_week_timetable_usecase.execute(
school_info.edu_office_code,
school_info.standard_school_code,
school_info.name,
Expand All @@ -116,6 +126,7 @@ async def timetable(
content=CoreStrings.TIMETABLE_NOT_FOUND,
edit_arg={"embed": None, "view": None},
)

return

await respond(
Expand All @@ -125,12 +136,11 @@ async def timetable(
)

image = await make_timetable_image(timetable_info, date)

embed = timetable_embed_builder(school_info.name, dates, is_private)

await interaction.followup.send(
file=File(image, filename="timetable.png"),
embed=embed,
ephemeral=ephemeral,
file=File(image, filename="timetable.png"), embed=embed, ephemeral=ephemeral
)

await interaction.edit_original_response(
Expand Down
Loading

0 comments on commit c74f1f7

Please sign in to comment.