diff --git a/pyproject.toml b/pyproject.toml index 9e5088f..be61444 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "discoboltalka" -version = "0.1.2" +version = "0.1.3" description = "Discord bot for 'Demo Болталка'" authors = ["lev"] license = "MIT" diff --git a/src/discoboltalka/__main__.py b/src/discoboltalka/__main__.py index 5af5121..2de2fe6 100644 --- a/src/discoboltalka/__main__.py +++ b/src/discoboltalka/__main__.py @@ -1,40 +1,4 @@ -import asyncio -from pathlib import Path - -import aiohttp -import hikari - -from discoboltalka.config import TomlConfigLoader -from discoboltalka.boltalka_api import BoltalkaAPI -from discoboltalka.boltalka_gateway_bot import BoltalkaGatewayBot - - -async def async_main() -> None: - config_loader = TomlConfigLoader(Path("config.toml")) - config = config_loader.load() - - client_session = aiohttp.ClientSession() - boltalka_api = BoltalkaAPI( - client_session=client_session, - client_name=config.boltalka_config.client_name, - ) - - bot = BoltalkaGatewayBot( - token=config.bot_config.token, - boltalka_api=boltalka_api, - intents=hikari.Intents.GUILD_MESSAGES, - ) - - try: - await bot.start() - await bot.join() - finally: - await bot.close() - await client_session.close() - - -def main() -> None: - asyncio.run(async_main()) +from discoboltalka.application.logic import main if __name__ == "__main__": diff --git a/src/discoboltalka/api/__init__.py b/src/discoboltalka/api/__init__.py new file mode 100644 index 0000000..c4e709d --- /dev/null +++ b/src/discoboltalka/api/__init__.py @@ -0,0 +1,10 @@ +from .used_objects import ErrorEmbed +from .boltalka_api import BoltalkaAPI +from .boltalka_gateway_bot import BoltalkaGatewayBot + + +__all__ = ( + "BoltalkaAPI", + "BoltalkaGatewayBot", + "ErrorEmbed", +) diff --git a/src/discoboltalka/boltalka_api.py b/src/discoboltalka/api/boltalka_api.py similarity index 100% rename from src/discoboltalka/boltalka_api.py rename to src/discoboltalka/api/boltalka_api.py diff --git a/src/discoboltalka/boltalka_gateway_bot.py b/src/discoboltalka/api/boltalka_gateway_bot.py similarity index 84% rename from src/discoboltalka/boltalka_gateway_bot.py rename to src/discoboltalka/api/boltalka_gateway_bot.py index da4776b..6dd0e4f 100644 --- a/src/discoboltalka/boltalka_gateway_bot.py +++ b/src/discoboltalka/api/boltalka_gateway_bot.py @@ -2,17 +2,18 @@ import re import logging +import textwrap import typing as t from datetime import datetime import hikari -from discoboltalka.boltalka_api import ( +from .boltalka_api import ( BoltalkaAPI, ValidationError, ClientResponseError, ) -from discoboltalka.models import ErrorEmbed +from .used_objects import ErrorEmbed _logger = logging.getLogger("discoboltalka.boltalka_gateway_bot") @@ -29,6 +30,8 @@ def subscribe_listens(self): self.subscribe(hikari.GuildMessageCreateEvent, self.message_listen) async def message_listen(self, event: hikari.GuildMessageCreateEvent) -> None: + rest_client = event.app.rest + content = event.message.content if event.is_bot or event.content is None: return @@ -46,7 +49,8 @@ async def message_listen(self, event: hikari.GuildMessageCreateEvent) -> None: return try: - boltalka_phrases = await self._boltalka_api.predict([[clean_content]]) + async with rest_client.trigger_typing(event.message.channel_id): + boltalka_phrases = await self._boltalka_api.predict([[clean_content]]) except ValidationError: await event.message.respond( embed=ErrorEmbed("Я не смогла понять ваш текст"), @@ -62,7 +66,16 @@ async def message_listen(self, event: hikari.GuildMessageCreateEvent) -> None: _logger.info(f"Boltalka response: {content!r} -> {boltalka_phrase!r}") - await event.message.respond(boltalka_phrases[0]) + clean_boltalka_phrase = textwrap.shorten(boltalka_phrase, width=2000) + + await event.message.respond( + clean_boltalka_phrase, + reply=True, + mentions_reply=True, + mentions_everyone=False, + role_mentions=False, + user_mentions=False, + ) async def _get_clean_content_from_guild_message_create_event( self, diff --git a/src/discoboltalka/models/__init__.py b/src/discoboltalka/api/used_objects/__init__.py similarity index 100% rename from src/discoboltalka/models/__init__.py rename to src/discoboltalka/api/used_objects/__init__.py diff --git a/src/discoboltalka/models/embeds.py b/src/discoboltalka/api/used_objects/embeds.py similarity index 100% rename from src/discoboltalka/models/embeds.py rename to src/discoboltalka/api/used_objects/embeds.py diff --git a/src/discoboltalka/application/__init__.py b/src/discoboltalka/application/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/discoboltalka/config/__init__.py b/src/discoboltalka/application/config/__init__.py similarity index 100% rename from src/discoboltalka/config/__init__.py rename to src/discoboltalka/application/config/__init__.py diff --git a/src/discoboltalka/config/loader.py b/src/discoboltalka/application/config/loader.py similarity index 100% rename from src/discoboltalka/config/loader.py rename to src/discoboltalka/application/config/loader.py diff --git a/src/discoboltalka/config/models.py b/src/discoboltalka/application/config/models.py similarity index 100% rename from src/discoboltalka/config/models.py rename to src/discoboltalka/application/config/models.py diff --git a/src/discoboltalka/application/logic.py b/src/discoboltalka/application/logic.py new file mode 100644 index 0000000..27eae98 --- /dev/null +++ b/src/discoboltalka/application/logic.py @@ -0,0 +1,38 @@ +import asyncio +from pathlib import Path + +import aiohttp +import hikari + +from .config import TomlConfigLoader +from discoboltalka.api.boltalka_api import BoltalkaAPI +from discoboltalka.api.boltalka_gateway_bot import BoltalkaGatewayBot + + +async def async_main() -> None: + config_loader = TomlConfigLoader(Path("config.toml")) + config = config_loader.load() + + client_session = aiohttp.ClientSession() + boltalka_api = BoltalkaAPI( + client_session=client_session, + client_name=config.boltalka_config.client_name, + ) + + bot = BoltalkaGatewayBot( + token=config.bot_config.token, + boltalka_api=boltalka_api, + intents=hikari.Intents.GUILD_MESSAGES, + ) + + try: + await bot.start() + await bot.join() + finally: + await bot.close() + await client_session.close() + + +def main() -> None: + asyncio.run(async_main()) +