stats.py 998 B

1234567891011121314151617181920212223
  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. context["client"].room_send(
  8. room.room_id, "m.room.message", {"msgtype": "m.notice",
  9. "body": "Sorry, I'm not connected to a database, so I don't have any statistics on your usage."}
  10. )
  11. return
  12. with database.cursor() as cursor:
  13. cursor.execute(
  14. "SELECT SUM(tokens) FROM token_usage WHERE room_id = ?", (room.room_id,))
  15. total_tokens = cursor.fetchone()[0] or 0
  16. await context["client"].room_send(
  17. room.room_id, "m.room.message", {"msgtype": "m.notice",
  18. "body": f"Total tokens used: {total_tokens}"}
  19. )