2023-04-19 06:11:28 +00:00
|
|
|
from nio.events.room_events import RoomMessageText
|
|
|
|
from nio.rooms import MatrixRoom
|
|
|
|
|
2023-04-23 13:26:46 +00:00
|
|
|
|
2023-04-19 06:11:28 +00:00
|
|
|
async def command_stats(room: MatrixRoom, event: RoomMessageText, context: dict):
|
|
|
|
context["logger"]("Showing stats...")
|
|
|
|
|
|
|
|
if not (database := context.get("database")):
|
|
|
|
context["logger"]("No database connection - cannot show stats")
|
2023-04-23 13:26:46 +00:00
|
|
|
return room.room_id, "m.room.message", {"msgtype": "m.notice",
|
|
|
|
"body": "Sorry, I'm not connected to a database, so I don't have any statistics on your usage."}
|
2023-04-19 06:11:28 +00:00
|
|
|
|
|
|
|
with database.cursor() as cursor:
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT SUM(tokens) FROM token_usage WHERE room_id = ?", (room.room_id,))
|
|
|
|
total_tokens = cursor.fetchone()[0] or 0
|
|
|
|
|
2023-04-23 13:26:46 +00:00
|
|
|
return room.room_id, "m.room.message", {"msgtype": "m.notice",
|
|
|
|
"body": f"Total tokens used: {total_tokens}"}
|