Skip to content

Commit

Permalink
Merge pull request #3 from LEv145/dev
Browse files Browse the repository at this point in the history
v 0.1.5
  • Loading branch information
LEv145 authored May 3, 2022
2 parents b7dbb57 + d239b83 commit 947f2fd
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 30 deletions.
8 changes: 8 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[bot]
token = "..."


[boltalka]
client_name = "..."


[message_event]
channels_for_conversation = [
948183196439289866,
947954894990606447,
] # Optional
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/discoboltalka/api/__init__.py
Original file line number Diff line number Diff line change
@@ -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

32 changes: 20 additions & 12 deletions src/discoboltalka/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,42 @@
_logger = logging.getLogger(__name__)


class BoltalkaEvent():
class BoltalkaEvents():
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:
return

# If client was pinged
if 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


# 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
Expand All @@ -57,7 +65,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(
Expand Down
Empty file.
5 changes: 0 additions & 5 deletions src/discoboltalka/api/used_objects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
from .embeds import ErrorEmbed


__all__ = (
"ErrorEmbed",
)
18 changes: 9 additions & 9 deletions src/discoboltalka/application/config/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
11 changes: 11 additions & 0 deletions src/discoboltalka/application/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
MainConfig,
BotConfig,
BoltalkaConfig,
MessageEventConfig,
)

import toml
Expand Down Expand Up @@ -45,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,
)


Expand Down
5 changes: 5 additions & 0 deletions src/discoboltalka/application/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class MainConfig():
bot_config: BotConfig
boltalka_config: BoltalkaConfig
message_event_config: MessageEventConfig


@dataclass
Expand All @@ -18,3 +19,7 @@ class BotConfig():
class BoltalkaConfig():
client_name: str


@dataclass
class MessageEventConfig():
channels_for_conversation: list | None
5 changes: 3 additions & 2 deletions src/discoboltalka/application/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from discoboltalka.api import (
BoltalkaAPI,
BoltalkaEvent,
BoltalkaEvents,
DialogRepository,
)

Expand All @@ -22,9 +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.message_event_config.channels_for_conversation,
)

bot = hikari.GatewayBot(
Expand Down

0 comments on commit 947f2fd

Please sign in to comment.