Skip to content

Commit

Permalink
fix: Fix several small bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
BURG3R5 authored Apr 17, 2022
2 parents b3b57d3 + bfcb096 commit 8267b1b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def test_post() -> str:
try:
name: str = request.json["name"]
except KeyError:
name: str = "unknown"
name: str = "(empty JSON)"
except TypeError:
name: str = "(invalid JSON)"
return (
f"This server is working, and to prove it to you, "
f"I'll guess your name!\nYour name is... {name}!"
Expand Down
2 changes: 1 addition & 1 deletion models/github/event_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def convert_str_to_event_type(event_keyword: str) -> EventType | None:
:return: `EventType` member corresponding to the keyword.
"""
for event_type in EventType:
if event_type.value == event_keyword:
if event_type.keyword == event_keyword:
return event_type
print("Event not in enum")
return None
44 changes: 34 additions & 10 deletions slack_bot/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ def run(self, raw_json: MultiDict) -> dict[str, Any] | None:
current_channel: str = "#" + json["channel_name"]
command: str = json["command"]
args: list[str] = str(json["text"]).split()
result: dict[str, Any] | None = None
if command == "/subscribe" and len(args) > 0:
self.run_subscribe_command(current_channel=current_channel, args=args)
result = self.run_subscribe_command(
current_channel=current_channel,
args=args,
)
elif command == "/unsubscribe" and len(args) > 0:
self.run_unsubscribe_command(current_channel=current_channel, args=args)
result = self.run_unsubscribe_command(
current_channel=current_channel,
args=args,
)
elif command == "/list":
return self.run_list_command(current_channel=current_channel)
result = self.run_list_command(current_channel=current_channel)
elif command == "/help":
return self.run_help_command()
result = self.run_help_command()
Storage.export_subscriptions(self.subscriptions)
return None
return result

def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
def run_subscribe_command(
self,
current_channel: str,
args: list[str],
) -> dict[str, Any]:
"""
Triggered by "/subscribe". Adds the passed events to the channel's subscriptions.
:param current_channel: Name of the current channel.
Expand Down Expand Up @@ -93,8 +104,13 @@ def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
events=new_events,
)
}
return self.run_list_command(current_channel=current_channel, ephemeral=True)

def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None:
def run_unsubscribe_command(
self,
current_channel: str,
args: list[str],
) -> dict[str, Any]:
"""
Triggered by "/unsubscribe". Removes the passed events from the channel's subscriptions.
:param current_channel: Name of the current channel.
Expand Down Expand Up @@ -130,11 +146,17 @@ def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None
events=events,
)
)
return self.run_list_command(current_channel=current_channel, ephemeral=True)

def run_list_command(self, current_channel: str) -> dict[str, Any]:
def run_list_command(
self,
current_channel: str,
ephemeral: bool = False,
) -> dict[str, Any]:
"""
Triggered by "/list". Sends a message listing the current channel's subscriptions.
:param current_channel: Name of the current channel.
:param ephemeral: Whether message should be ephemeral or not.
:return: Message containing subscriptions for the passed channel.
"""
blocks: list[dict] = []
Expand All @@ -149,7 +171,9 @@ def run_list_command(self, current_channel: str) -> dict[str, Any]:
)
if channel is None:
continue
events_string = ", ".join(event.keyword for event in channel.events)
events_string = ", ".join(
f"`{event.name.lower()}`" for event in channel.events
)
blocks += [
{
"type": "section",
Expand All @@ -163,7 +187,7 @@ def run_list_command(self, current_channel: str) -> dict[str, Any]:
},
]
return {
"response_type": "in_channel",
"response_type": "ephemeral" if ephemeral else "in_channel",
"blocks": blocks,
}

Expand Down
2 changes: 1 addition & 1 deletion utils/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def export_subscriptions(subscriptions: dict[str, set[Channel]]) -> None:
with open(".data", mode="w", encoding="utf-8") as file:
exportable_dict: dict[str, dict[str, list[str]]] = {
repo: {
channel.name: [event.value for event in channel.events]
channel.name: [event.keyword for event in channel.events]
for channel in channels
}
for repo, channels in subscriptions.items()
Expand Down

0 comments on commit 8267b1b

Please sign in to comment.