message d'erreur personnalisable
This commit is contained in:
@@ -43,3 +43,4 @@ sudo systemctl enable --now tchap-beta.service
|
|||||||
````
|
````
|
||||||
|
|
||||||
(L'emplacement et le nom d'utilisateur sont libres tant que c'est cohérent avec le service.)
|
(L'emplacement et le nom d'utilisateur sont libres tant que c'est cohérent avec le service.)
|
||||||
|
|
||||||
|
|||||||
34
callbacks.py
34
callbacks.py
@@ -6,23 +6,25 @@ from matrix_bot.config import logger
|
|||||||
from matrix_bot.client import MatrixClient
|
from matrix_bot.client import MatrixClient
|
||||||
|
|
||||||
|
|
||||||
def properly_fail(function):
|
def properly_fail(error_message="failed to answer", style="m.notice"):
|
||||||
"""use this decorator so that your async callback never crash, log the error and return a message to the room"""
|
def custom_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)
|
@wraps(function)
|
||||||
def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||||
function_instance = function(room, message, matrix_client)
|
function_instance = function(room, message, matrix_client)
|
||||||
|
|
||||||
async def inner():
|
async def inner():
|
||||||
try:
|
try:
|
||||||
return await function_instance
|
return await function_instance
|
||||||
except Exception as unexpected_exception: # noqa
|
except Exception as unexpected_exception: # noqa
|
||||||
await matrix_client.send_text_message(room.room_id, "Oups, j'ai buggué 😿", "m.notice")
|
await matrix_client.send_text_message(room.room_id, error_message, style)
|
||||||
logger.warning(f"command failed with exception : {unexpected_exception}")
|
logger.warning(f"command failed with exception : {unexpected_exception}")
|
||||||
exit()
|
exit()
|
||||||
finally:
|
finally:
|
||||||
await matrix_client.room_typing(room.room_id, typing_state=False)
|
await matrix_client.room_typing(room.room_id, typing_state=False)
|
||||||
|
|
||||||
return inner()
|
return inner()
|
||||||
|
|
||||||
return decorated
|
return decorated
|
||||||
|
return custom_properly_fail
|
||||||
@@ -16,7 +16,7 @@ from markov import parle
|
|||||||
# d'envoyer le message que le bot n'est plus en train d'écrire
|
# d'envoyer le message que le bot n'est plus en train d'écrire
|
||||||
# la fonction va être appelée dans tous les cas, le décorateur @ignore_when_not_concerned
|
# la fonction va être appelée dans tous les cas, le décorateur @ignore_when_not_concerned
|
||||||
# permet de laisser event_parser gérer le cas où la commande n'est pas concernée
|
# permet de laisser event_parser gérer le cas où la commande n'est pas concernée
|
||||||
@properly_fail
|
@properly_fail("Oups, j'ai buggué 😿")
|
||||||
@ignore_when_not_concerned
|
@ignore_when_not_concerned
|
||||||
async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
||||||
# on initialise un event_parser pour décider à quel message cette commande va répondre
|
# on initialise un event_parser pour décider à quel message cette commande va répondre
|
||||||
@@ -33,6 +33,7 @@ async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
|
|||||||
await matrix_client.room_typing(room.room_id)
|
await matrix_client.room_typing(room.room_id)
|
||||||
await asyncio.sleep(len(reponse) / 50)
|
await asyncio.sleep(len(reponse) / 50)
|
||||||
# il envoie le message
|
# il envoie le message
|
||||||
|
await matrix_client.room_typing(room.room_id, typing_state=False)
|
||||||
await matrix_client.send_html_message(room.room_id, reponse, "m.notice")
|
await matrix_client.send_html_message(room.room_id, reponse, "m.notice")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user