Skip to content

Commit dcae2bb

Browse files
authored
Merge pull request #28 from yomcube/dmhandler-experiment
Enhance DM handler API
2 parents 3b925b8 + c56914a commit dcae2bb

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

api/dm_handlers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
handlers_dict = {}
2+
def add_dm_handler(comp, func):
3+
handlers_dict[comp] = func
4+
5+
def init_dm_handlers():
6+
from api.mkwii.mkwii_file_handling import handle_mkwii_files
7+
add_dm_handler("mkw", handle_mkwii_files)
8+
9+
from api.nsmbwii.nsmbwii_file_handling import handle_nsmbwii_files
10+
add_dm_handler("nsmbw", handle_nsmbwii_files)

api/submissions.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from api.db_classes import SubmissionChannel, Userbase, get_session, Submissions, LogChannel, SeekingChannel, Teams
66
from sqlalchemy import insert, select, or_
77
from api.utils import get_file_types, get_leader, get_team_size, is_in_team, get_submitter_role, is_task_currently_running
8+
from api.dm_handlers import handlers_dict, init_dm_handlers
89

910
load_dotenv()
1011
DEFAULT = os.getenv('DEFAULT')
@@ -257,15 +258,11 @@ async def handle_dms(message, self):
257258
if len(attachments) > 0:
258259
file_dict = get_file_types(attachments)
259260
try:
260-
# TODO: use a class here?
261-
if DEFAULT == "mkw":
262-
from api.mkwii.mkwii_file_handling import handle_mkwii_files
263-
await handle_mkwii_files(message, attachments, file_dict, self)
264-
elif DEFAULT == "nsmbw":
265-
from api.nsmbwii.nsmbwii_file_handling import handle_nsmbwii_files
266-
await handle_nsmbwii_files(message, attachments, file_dict, self)
267-
else:
268-
pass
269-
261+
await handlers_dict[DEFAULT](message, attachments, file_dict, self)
262+
263+
except KeyError:
264+
print(f"Could not find DM handler for '{DEFAULT}'.")
270265
except TimeoutError:
271266
await channel.send("Could not process Files!")
267+
268+
init_dm_handlers()

0 commit comments

Comments
 (0)