stats.py 708 B

123456789101112131415161718
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. async def command_stats(room: MatrixRoom, event: RoomMessageText, bot):
  4. bot.logger.log("Showing stats...")
  5. if not bot.database:
  6. bot.logger.log("No database connection - cannot show stats")
  7. bot.send_message(room, "Sorry, I'm not connected to a database, so I don't have any statistics on your usage.", True)
  8. return
  9. with bot.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. bot.send_message(room, f"Total tokens used: {total_tokens}", True)