stats.py 905 B

12345678910111213141516171819
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. async def command_stats(room: MatrixRoom, event: RoomMessageText, context: dict):
  4. context["logger"]("Showing stats...")
  5. if not (database := context.get("database")):
  6. context["logger"]("No database connection - cannot show stats")
  7. return room.room_id, "m.room.message", {"msgtype": "m.notice",
  8. "body": "Sorry, I'm not connected to a database, so I don't have any statistics on your usage."}
  9. with database.cursor() as cursor:
  10. cursor.execute(
  11. "SELECT SUM(tokens) FROM token_usage WHERE room_id = ?", (room.room_id,))
  12. total_tokens = cursor.fetchone()[0] or 0
  13. return room.room_id, "m.room.message", {"msgtype": "m.notice",
  14. "body": f"Total tokens used: {total_tokens}"}