1- import logging
21import re
32import threading
43import time
54from csv import reader
65from datetime import datetime , timedelta
76from io import StringIO
7+ from logging import getLogger
88from types import MappingProxyType
99from typing import Any , Dict , List , Optional , Set , Tuple , Union
1010
1111import csp
1212from bs4 import BeautifulSoup , Tag
1313from croniter import croniter
1414from csp import Outputs , ts
15- from csp_adapter_discord import DiscordAdapterManager , DiscordMessage as RawDiscordMessage
16- from csp_adapter_slack import SlackAdapterManager , SlackMessage as RawSlackMessage
17- from csp_adapter_symphony import SymphonyAdapter , SymphonyMessage as RawSymphonyMessage
18- from csp_adapter_symphony .adapter import Presence
1915from pydantic import PrivateAttr
2016
17+ from .backends import (
18+ DiscordAdapterManager ,
19+ DiscordMessage as RawDiscordMessage ,
20+ Presence ,
21+ SlackAdapterManager ,
22+ SlackMessage as RawSlackMessage ,
23+ SymphonyAdapter ,
24+ SymphonyMessage as RawSymphonyMessage ,
25+ )
2126from .bot_config import BotConfig
2227from .commands import (
2328 BaseCommand ,
2429 BaseCommandModel ,
2530 HelpCommand ,
2631 ScheduleCommand ,
32+ StatusCommand ,
2733)
2834from .gateway import GatewayChannels , GatewayModule
2935from .structs import (
3440)
3541from .utils import Backend
3642
37- log = logging . getLogger (__name__ )
43+ log = getLogger (__name__ )
3844
3945SLACK_ENTITY_REGEX = re .compile ("<@.+?>" )
4046DISCORD_ENTITY_REGEX = re .compile ("<@.+?>" )
@@ -59,12 +65,18 @@ class Bot(GatewayModule):
5965
6066 def connect (self , channels : GatewayChannels ) -> None :
6167 if self .config .discord_config :
68+ if DiscordAdapterManager is None :
69+ raise ImportError ("Discord adapter not installed. Please install csp-adapter-discord." )
6270 self ._configs ["discord" ] = self .config .discord_config
6371 self ._adapters ["discord" ] = DiscordAdapterManager (self .config .discord_config .adapter_config )
6472 if self .config .slack_config :
73+ if SlackAdapterManager is None :
74+ raise ImportError ("Slack adapter not installed. Please install csp-adapter-slack." )
6575 self ._configs ["slack" ] = self .config .slack_config
6676 self ._adapters ["slack" ] = SlackAdapterManager (self .config .slack_config .adapter_config )
6777 if self .config .symphony_config :
78+ if SymphonyAdapter is None :
79+ raise ImportError ("Symphony adapter not installed. Please install csp-adapter-symphony." )
6880 self ._configs ["symphony" ] = self .config .symphony_config
6981 self ._adapters ["symphony" ] = SymphonyAdapter (self .config .symphony_config .adapter_config )
7082
@@ -198,10 +210,10 @@ def message_to_bot_commands(self, message: ts[Message]) -> Outputs(bot_commands=
198210 log .info ("Ignoring message (not to bot)" )
199211 else :
200212 if not self .is_authorized (message ):
201- if self ._config [message .backend ].unauthorized_msg :
213+ if self ._configs [message .backend ].unauthorized_msg :
202214 channel = message .channel if message .channel != "IM" else message .user
203215 unauthorized_message = Message (
204- msg = self ._config [message .backend ].unauthorized_msg , channel = channel , source = message .backend
216+ msg = self ._configs [message .backend ].unauthorized_msg , channel = channel , source = message .backend
205217 )
206218 csp .output (unauthorized_message = unauthorized_message )
207219 else :
@@ -458,10 +470,14 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti
458470
459471 command_runner = self ._commands [command ]
460472
473+ # TODO generalize by interrogating the command signature
461474 if isinstance (command_runner , ScheduleCommand ):
462475 # Special command, gets access to schedule of commands
463476 return command_runner .preexecute (command_instance , self ._scheduled , self )
464- return self ._commands [command ].preexecute (command_instance )
477+ elif isinstance (command_runner , StatusCommand ):
478+ # Special command, gets access to status of commands
479+ return command_runner .preexecute (command_instance , self )
480+ return command_runner .preexecute (command_instance )
465481 else :
466482 log .info (f"Defaulting to help command from message to bot with no command: { message } " )
467483 command_runner = self ._commands ["help" ]
0 commit comments