From 7bf71bf0510d40c0c03512ca1cdb2fec3601f7c8 Mon Sep 17 00:00:00 2001 From: lev Date: Tue, 3 May 2022 18:21:25 +0200 Subject: [PATCH 1/3] v 0.1.5 Add channels settings --- config.example.toml | 5 ++++ pyproject.toml | 2 +- src/discoboltalka/api/events.py | 25 ++++++++++++------- .../application/config/loader.py | 1 + .../application/config/models.py | 1 + src/discoboltalka/application/logic.py | 1 + 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/config.example.toml b/config.example.toml index 3e798d0..c76f400 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,5 +1,10 @@ [bot] token = "..." +channels_for_conversation = [ + 948183196439289866, + 947954894990606447, +] # Optional + [boltalka] client_name = "..." diff --git a/pyproject.toml b/pyproject.toml index b203947..7f92bfe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "discoboltalka" -version = "0.1.4" +version = "0.1.5" description = "Discord bot for 'Demo Болталка'" authors = ["lev"] license = "MIT" diff --git a/src/discoboltalka/api/events.py b/src/discoboltalka/api/events.py index 0fc0989..00cfe40 100644 --- a/src/discoboltalka/api/events.py +++ b/src/discoboltalka/api/events.py @@ -24,29 +24,36 @@ def __init__( self, boltalka_api: BoltalkaAPI, dialog_repository: AbstractDialogRepository, + channels_for_conversation: list | None = None, ) -> None: self._boltalka_api = boltalka_api self._dialog_repository = dialog_repository + self._channels_for_conversation = channels_for_conversation async def on_guild_message_create(self, event: hikari.GuildMessageCreateEvent) -> None: - rest_client = event.app.rest - app = event.app - - if not isinstance(app, hikari.GatewayBot): + if not isinstance(event.app, hikari.GatewayBot): raise RuntimeError("App should be 'hikari.GatewayBot'") - content = event.message.content - if event.is_bot or event.content is None: + + if event.is_bot: return + if event.message.content is None: + return + + if self._channels_for_conversation is not None: + if int(event.message.channel_id) not in self._channels_for_conversation: + return + # If client was pinged - if app.cache.get_me().id not in event.message.mentions.users: + if event.app.cache.get_me().id not in event.message.mentions.users: return + # Prepare content user_request = await self._clean_content_from_guild_message_create_event( event=event, - content=content, + content=event.message.content, ) if not user_request: return @@ -57,7 +64,7 @@ async def on_guild_message_create(self, event: hikari.GuildMessageCreateEvent) - _logger.debug(f"Boltalka context: {context}") try: - async with rest_client.trigger_typing(event.message.channel_id): + async with event.app.rest.trigger_typing(event.message.channel_id): boltalka_phrases = await self._boltalka_api.predict([context]) except ValidationError: await event.message.respond( diff --git a/src/discoboltalka/application/config/loader.py b/src/discoboltalka/application/config/loader.py index da265b4..34f3579 100644 --- a/src/discoboltalka/application/config/loader.py +++ b/src/discoboltalka/application/config/loader.py @@ -33,6 +33,7 @@ def load(self) -> MainConfig: bot_raw_config = config_raw_data["bot"] bot_config = BotConfig( token=bot_raw_config["token"], + channels_for_conversation=bot_raw_config.get("channels_for_conversation"), ) except KeyError as exception: raise InvalidConfig() from exception diff --git a/src/discoboltalka/application/config/models.py b/src/discoboltalka/application/config/models.py index fdbdda0..cfea907 100644 --- a/src/discoboltalka/application/config/models.py +++ b/src/discoboltalka/application/config/models.py @@ -12,6 +12,7 @@ class MainConfig(): @dataclass class BotConfig(): token: str + channels_for_conversation: list | None @dataclass diff --git a/src/discoboltalka/application/logic.py b/src/discoboltalka/application/logic.py index 122448b..5f974cf 100644 --- a/src/discoboltalka/application/logic.py +++ b/src/discoboltalka/application/logic.py @@ -25,6 +25,7 @@ async def async_main() -> None: boltalka_event = BoltalkaEvent( boltalka_api=boltalka_api, dialog_repository=DialogRepository(), + channels_for_conversation=config.bot_config.channels_for_conversation, ) bot = hikari.GatewayBot( From 0b742cc3bdbcf59339f25dbabe89b41963880088 Mon Sep 17 00:00:00 2001 From: lev Date: Tue, 3 May 2022 19:08:21 +0200 Subject: [PATCH 2/3] v 0.1.5 Regroup config settings Fix conditions --- config.example.toml | 11 +++++--- src/discoboltalka/api/__init__.py | 2 +- src/discoboltalka/api/events.py | 27 ++++++++++--------- .../application/config/loader.py | 12 ++++++++- .../application/config/models.py | 6 ++++- src/discoboltalka/application/logic.py | 6 ++--- 6 files changed, 41 insertions(+), 23 deletions(-) diff --git a/config.example.toml b/config.example.toml index c76f400..9c3d251 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,10 +1,13 @@ [bot] token = "..." -channels_for_conversation = [ - 948183196439289866, - 947954894990606447, -] # Optional [boltalka] client_name = "..." + + +[message_event] +channels_for_conversation = [ + 948183196439289866, + 947954894990606447, +] # Optional diff --git a/src/discoboltalka/api/__init__.py b/src/discoboltalka/api/__init__.py index 1364c2c..ed62466 100644 --- a/src/discoboltalka/api/__init__.py +++ b/src/discoboltalka/api/__init__.py @@ -1,6 +1,6 @@ from .used_objects import ErrorEmbed from .modules.boltalka_api import BoltalkaAPI -from .events import BoltalkaEvent +from .events import BoltalkaEvents from .repositories import DialogRepository from .abstract_repositories import AbstractDialogRepository diff --git a/src/discoboltalka/api/events.py b/src/discoboltalka/api/events.py index 00cfe40..dd927fa 100644 --- a/src/discoboltalka/api/events.py +++ b/src/discoboltalka/api/events.py @@ -19,7 +19,7 @@ _logger = logging.getLogger(__name__) -class BoltalkaEvent(): +class BoltalkaEvents(): def __init__( self, boltalka_api: BoltalkaAPI, @@ -35,18 +35,19 @@ async def on_guild_message_create(self, event: hikari.GuildMessageCreateEvent) - raise RuntimeError("App should be 'hikari.GatewayBot'") - if event.is_bot: - return - - if event.message.content is None: - return - - if self._channels_for_conversation is not None: - if int(event.message.channel_id) not in self._channels_for_conversation: - return - - # If client was pinged - if event.app.cache.get_me().id not in event.message.mentions.users: + if ( + event.is_bot + or event.message.content is None + # Favorable conditions + or ( + # If client wasn't pinged + event.app.cache.get_me().id not in event.message.mentions.users + and ( + self._channels_for_conversation is not None + and int(event.message.channel_id) not in self._channels_for_conversation + ) + ) + ): return diff --git a/src/discoboltalka/application/config/loader.py b/src/discoboltalka/application/config/loader.py index 34f3579..4d00413 100644 --- a/src/discoboltalka/application/config/loader.py +++ b/src/discoboltalka/application/config/loader.py @@ -7,6 +7,7 @@ MainConfig, BotConfig, BoltalkaConfig, + MessageEventConfig, ) import toml @@ -33,7 +34,6 @@ def load(self) -> MainConfig: bot_raw_config = config_raw_data["bot"] bot_config = BotConfig( token=bot_raw_config["token"], - channels_for_conversation=bot_raw_config.get("channels_for_conversation"), ) except KeyError as exception: raise InvalidConfig() from exception @@ -46,9 +46,19 @@ def load(self) -> MainConfig: except KeyError as exception: raise InvalidConfig() from exception + try: + message_event_raw_config = config_raw_data["message_event"] + message_event_config = MessageEventConfig( + channels_for_conversation=message_event_raw_config.get("channels_for_conversation"), + ) + except KeyError as exception: + raise InvalidConfig() from exception + + return MainConfig( bot_config=bot_config, boltalka_config=boltalka_config, + message_event_config=message_event_config, ) diff --git a/src/discoboltalka/application/config/models.py b/src/discoboltalka/application/config/models.py index cfea907..bfc5bf4 100644 --- a/src/discoboltalka/application/config/models.py +++ b/src/discoboltalka/application/config/models.py @@ -7,15 +7,19 @@ class MainConfig(): bot_config: BotConfig boltalka_config: BoltalkaConfig + message_event_config: MessageEventConfig @dataclass class BotConfig(): token: str - channels_for_conversation: list | None @dataclass class BoltalkaConfig(): client_name: str + +@dataclass +class MessageEventConfig(): + channels_for_conversation: list | None diff --git a/src/discoboltalka/application/logic.py b/src/discoboltalka/application/logic.py index 5f974cf..bc6c137 100644 --- a/src/discoboltalka/application/logic.py +++ b/src/discoboltalka/application/logic.py @@ -6,7 +6,7 @@ from discoboltalka.api import ( BoltalkaAPI, - BoltalkaEvent, + BoltalkaEvents, DialogRepository, ) @@ -22,10 +22,10 @@ async def async_main() -> None: client_session=client_session, client_name=config.boltalka_config.client_name, ) - boltalka_event = BoltalkaEvent( + boltalka_event = BoltalkaEvents( boltalka_api=boltalka_api, dialog_repository=DialogRepository(), - channels_for_conversation=config.bot_config.channels_for_conversation, + channels_for_conversation=config.message_event_config.channels_for_conversation, ) bot = hikari.GatewayBot( From d239b83e4c44c23e867c5a5aa9e58437165423c1 Mon Sep 17 00:00:00 2001 From: lev Date: Tue, 3 May 2022 19:10:53 +0200 Subject: [PATCH 3/3] v 0.1.5 Update `__init__` files --- src/discoboltalka/api/modules/__init__.py | 0 src/discoboltalka/api/used_objects/__init__.py | 5 ----- .../application/config/__init__.py | 18 +++++++++--------- 3 files changed, 9 insertions(+), 14 deletions(-) delete mode 100644 src/discoboltalka/api/modules/__init__.py diff --git a/src/discoboltalka/api/modules/__init__.py b/src/discoboltalka/api/modules/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/discoboltalka/api/used_objects/__init__.py b/src/discoboltalka/api/used_objects/__init__.py index 540a968..a00e492 100644 --- a/src/discoboltalka/api/used_objects/__init__.py +++ b/src/discoboltalka/api/used_objects/__init__.py @@ -1,6 +1 @@ from .embeds import ErrorEmbed - - -__all__ = ( - "ErrorEmbed", -) diff --git a/src/discoboltalka/application/config/__init__.py b/src/discoboltalka/application/config/__init__.py index ff12c3f..e7162c4 100644 --- a/src/discoboltalka/application/config/__init__.py +++ b/src/discoboltalka/application/config/__init__.py @@ -1,10 +1,10 @@ -from .models import MainConfig, BotConfig, BoltalkaConfig -from .loader import TomlConfigLoader - - -__all__ = ( - "BoltalkaConfig", - "BotConfig", - "MainConfig", - "TomlConfigLoader", +from .models import ( + MainConfig, + BotConfig, + BoltalkaConfig, + MessageEventConfig, +) +from .loader import ( + ABCLoader, + TomlConfigLoader, )