refactor
This commit is contained in:
38
tchap-beta.py
Normal file → Executable file
38
tchap-beta.py
Normal file → Executable file
@@ -4,44 +4,13 @@ from functools import wraps
|
||||
from nio import MatrixRoom, Event
|
||||
from nio.crypto import ENCRYPTION_ENABLED
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from matrix_bot.bot import MatrixBot
|
||||
from matrix_bot.client import MatrixClient
|
||||
from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned
|
||||
from matrix_bot.config import logger
|
||||
from tchap_bot.config import Config
|
||||
|
||||
|
||||
from markov import parle
|
||||
|
||||
|
||||
class ConfigProxy(Config):
|
||||
https_proxy: str = Field("", description="Proxy URL")
|
||||
|
||||
env_config = ConfigProxy()
|
||||
|
||||
|
||||
def properly_fail(function):
|
||||
"""use this decorator so that your async callback never crash, log the error and return a message to the room"""
|
||||
|
||||
@wraps(function)
|
||||
def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||
function_instance = function(room, message, matrix_client)
|
||||
|
||||
async def inner():
|
||||
try:
|
||||
return await function_instance
|
||||
except Exception as unexpected_exception: # noqa
|
||||
await matrix_client.send_text_message(room.room_id, "Oups, j'ai buggué 😿", "m.notice")
|
||||
logger.warning(f"command failed with exception : {unexpected_exception}")
|
||||
exit()
|
||||
finally:
|
||||
await matrix_client.room_typing(room.room_id, typing_state=False)
|
||||
|
||||
return inner()
|
||||
|
||||
return decorated
|
||||
from .config import env_config
|
||||
from .callbacks import properly_fail
|
||||
from .markov import parle
|
||||
|
||||
|
||||
# le décorateur @properly_fail va permettre à la commande de laisser un message d'erreur si la commande plante et
|
||||
@@ -67,6 +36,7 @@ async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||
# il envoie le message
|
||||
await matrix_client.send_text_message(room.room_id, reponse, "m.notice")
|
||||
|
||||
|
||||
tchap_bot = MatrixBot(
|
||||
env_config.matrix_home_server,
|
||||
env_config.matrix_bot_username,
|
||||
|
||||
Reference in New Issue
Block a user