Skip to content

Commit

Permalink
v 0.1.3
Browse files Browse the repository at this point in the history
Update version
New architecture
Shorten for long bot phrases (width=2000)
Update message respond:
    Reply tactic
    disable `role`, `user`, `@everyone`, `@here` mentions
    Add mentions on reply for better visibility
  • Loading branch information
LEv145 committed Apr 26, 2022
1 parent 8083e86 commit 415dc21
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 42 deletions.
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.2"
version = "0.1.3"
description = "Discord bot for 'Demo Болталка'"
authors = ["lev"]
license = "MIT"
Expand Down
38 changes: 1 addition & 37 deletions src/discoboltalka/__main__.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
10 changes: 10 additions & 0 deletions src/discoboltalka/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .used_objects import ErrorEmbed
from .boltalka_api import BoltalkaAPI
from .boltalka_gateway_bot import BoltalkaGatewayBot


__all__ = (
"BoltalkaAPI",
"BoltalkaGatewayBot",
"ErrorEmbed",
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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("Я не смогла понять ваш текст"),
Expand All @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions src/discoboltalka/application/logic.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 415dc21

Please sign in to comment.